From e5ecc46c40f4715f96a85c37ebf476b31826826b Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Thu, 24 Oct 2024 19:04:48 +0300 Subject: [PATCH] refactor code --- src/{difference.rs => line.rs} | 67 +++++++++++++++++++++++++++------- src/main.rs | 37 +++++++++++-------- 2 files changed, 75 insertions(+), 29 deletions(-) rename src/{difference.rs => line.rs} (50%) diff --git a/src/difference.rs b/src/line.rs similarity index 50% rename from src/difference.rs rename to src/line.rs index dd69c92..224370c 100644 --- a/src/difference.rs +++ b/src/line.rs @@ -2,18 +2,55 @@ use std::fmt; use std::fmt::Formatter; use std::ops::Deref; +pub trait Line: fmt::Display { + fn is_difference(&self) -> bool; +} + +/// Line represents a non-differing line. +pub(crate) struct MatchedLine { + line_number: i32, + line: String, +} + +impl MatchedLine { + /// Constructs a new Line instance + pub fn new(line_number: i32, line: String) -> MatchedLine { + MatchedLine { line_number, line } + } + + /// get_line returns the line. + pub fn get_line(&self) -> &String { + &self.line + } +} + +impl fmt::Display for MatchedLine { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, r#"{}. {}"#, self.line_number, self.get_line(),) + .expect("Failed to write MatchedLine"); + writeln!(f) + } +} + +impl Line for MatchedLine { + fn is_difference(&self) -> bool { + false + } +} + /// A line difference is represented by the Difference struct. #[derive(Debug)] -pub(crate) struct Difference { +pub(crate) struct DifferingLine { line_number: i32, missing_line_indicator: String, left_line: String, right_line: String, } -impl Difference { - pub fn new(line_number: i32, left_line: String, right_line: String) -> Difference { - Difference { +impl DifferingLine { + /// Constructs a new difference instance. + pub fn new(line_number: i32, left_line: String, right_line: String) -> DifferingLine { + DifferingLine { line_number, missing_line_indicator: String::from(""), left_line: left_line.into(), @@ -40,20 +77,24 @@ impl Difference { } } -/// Implements the display trait. -impl fmt::Display for Difference { +impl fmt::Display for DifferingLine { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!( f, - r#"Line: {}. -< {} ----- -> {} - -"#, - self.line_number, + r#" - {} +{}. ---- + + {}"#, self.left_line(), + self.line_number, self.right_line() ) + .expect("Failed to write DifferingLines"); + writeln!(f) + } +} + +impl Line for DifferingLine { + fn is_difference(&self) -> bool { + true } } diff --git a/src/main.rs b/src/main.rs index 593192b..76af8f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,41 +1,46 @@ -use crate::difference::Difference; +use crate::line::{DifferingLine, Line, MatchedLine}; use itertools::Itertools; use std::fs::read_to_string; +use std::ops::Deref; -mod difference; +mod line; -/// Gets the differences of lines that differ in the given text. +/// Gets the lines of two given texts /// -/// It returns a [`Vec`] -fn get_differences(left_text: &String, right_text: &String) -> Vec { +/// It returns a [`Vec>`] +fn get_lines(left_text: &String, right_text: &String) -> Vec> { left_text .lines() .map(String::from) .zip_longest(right_text.lines().map(String::from)) .enumerate() - .filter_map(|(line, item)| -> Option { + .map(|(line, item)| -> Box { let left = item.clone().left().unwrap_or(String::from("")); let right = item.right().unwrap_or(String::from("")); if left != right { - Some(Difference::new(line as i32 + 1, left, right)) + Box::new(DifferingLine::new(line as i32 + 1, left, right)) } else { - return None; + Box::new(MatchedLine::new(line as i32 + 1, left)) } }) .collect() } fn main() { - let left_file = - read_to_string("a.txt").expect("Left file not found"); - let right_file = - read_to_string("b.txt").expect("Right file not found"); + let left_file = read_to_string("a.txt").expect("Left file not found"); + let right_file = read_to_string("b.txt").expect("Right file not found"); - let res: Vec = get_differences(&left_file, &right_file); + let lines: Vec> = get_lines(&left_file, &right_file); - for diff in res.iter() { - print!("{diff}") + let mut differences_counter: i32 = 0; + for line in lines.iter() { + let line = line.deref(); + print!("{}", line); + if line.is_difference() { + differences_counter += 1; + } } - println!("Found {} differences.", res.len()) + println!(); + println!("Found {} differences.", differences_counter); }