diff --git a/readme.md b/readme.md index 49d4b3c..d99f657 100644 --- a/readme.md +++ b/readme.md @@ -2,6 +2,14 @@ Ndiff is an educational Rust rewrite of the [diff](https://linux.die.net/man/1/diff) program. -Of course, it's no way near as featured as -the [diff](https://linux.die.net/man/1/diff) command, -since it's utility is to learn Rust concepts by completing a simple project. \ No newline at end of file +Of course, it's no way near as featured as +the [diff](https://linux.die.net/man/1/diff) command, +since it's utility is to learn Rust concepts by completing a simple project. + +## Installation + +You may install and play around with the program using the command line: + +```bash +cargo install --git https://github.com/dnutiu/ndiff +``` \ No newline at end of file diff --git a/src/line.rs b/src/line.rs index 771da6f..0082c33 100644 --- a/src/line.rs +++ b/src/line.rs @@ -1,16 +1,17 @@ -use std::{fmt}; +use itertools::Itertools; +use std::fmt; use std::fmt::{Debug, Formatter}; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; #[derive(Debug)] pub(crate) struct MissingLineIndicator { - pub value: String + pub value: String, } impl Default for MissingLineIndicator { fn default() -> Self { - MissingLineIndicator{ - value: String::from("") + MissingLineIndicator { + value: String::from(""), } } } @@ -28,7 +29,6 @@ pub(crate) enum Line { } impl Line { - /// Prints the line to stdout pub fn print(&self) { match self { @@ -72,4 +72,30 @@ impl fmt::Display for Line { } } } -} \ No newline at end of file +} + +/// Compares the lines of two texts. +/// +/// It returns a [`Vec`] +pub fn compare_lines(left_text: &str, right_text: &str) -> Vec { + left_text + .lines() + .map(String::from) + .zip_longest(right_text.lines().map(String::from)) + .enumerate() + .map(|(line_number, item)| -> Line { + let missing_line_indicator: MissingLineIndicator = Default::default(); + let left = item + .clone() + .left() + .unwrap_or(missing_line_indicator.value.clone()); + let right = item.right().unwrap_or(missing_line_indicator.value); + + if left != right { + Line::DifferingLine(line_number as i32 + 1, left, right) + } else { + Line::MatchedLine(line_number as i32 + 1, left) + } + }) + .collect() +} diff --git a/src/main.rs b/src/main.rs index fb2a3a1..799fe28 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,46 +1,20 @@ -use crate::line::{Line, MissingLineIndicator}; -use itertools::Itertools; +use crate::line::Line; use std::fs::read_to_string; mod line; -/// Gets the lines of two given texts -/// -/// 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() - .map(|(line_number, item)| -> Line { - let missing_line_indicator: MissingLineIndicator = Default::default(); - let left = item.clone().left().unwrap_or(missing_line_indicator.value.clone()); - let right = item.right().unwrap_or(missing_line_indicator.value); - - if left != right { - Line::DifferingLine(line_number as i32 + 1, left, right) - } else { - Line::MatchedLine(line_number 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 lines: Vec = get_lines(&left_file, &right_file); + let lines: Vec = line::compare_lines(&left_file, &right_file); let mut differences_counter: i32 = 0; for line in lines.iter() { line.print(); match line { Line::MatchedLine(_, _) => {} - Line::DifferingLine(_, _, _) => { - differences_counter += 1 - } + Line::DifferingLine(_, _, _) => differences_counter += 1, } } println!();