refactor code

This commit is contained in:
Denis-Cosmin Nutiu 2024-10-24 19:04:48 +03:00
parent 9e335ab0a7
commit e5ecc46c40
2 changed files with 75 additions and 29 deletions

View file

@ -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("<missing line>"),
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
}
}

View file

@ -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<Difference>`]
fn get_differences(left_text: &String, right_text: &String) -> Vec<Difference> {
/// It returns a [`Vec<Box<dyn Line>>`]
fn get_lines(left_text: &String, right_text: &String) -> Vec<Box<dyn Line>> {
left_text
.lines()
.map(String::from)
.zip_longest(right_text.lines().map(String::from))
.enumerate()
.filter_map(|(line, item)| -> Option<Difference> {
.map(|(line, item)| -> Box<dyn Line> {
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<Difference> = get_differences(&left_file, &right_file);
let lines: Vec<Box<dyn Line>> = 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);
}