49 lines
1.1 KiB
Rust
49 lines
1.1 KiB
Rust
use color_print::cformat;
|
|
|
|
pub struct Compare {
|
|
pub right: String,
|
|
pub left: String,
|
|
}
|
|
pub struct Compared {
|
|
pub right: String,
|
|
pub left: String,
|
|
}
|
|
|
|
pub struct Comparer {
|
|
formatted_right: String,
|
|
formatted_left: String
|
|
}
|
|
impl Comparer {
|
|
pub const fn new() -> Self {
|
|
Self {
|
|
formatted_right: String::new(),
|
|
formatted_left: String::new(),
|
|
}
|
|
}
|
|
|
|
fn format_compare(&mut self, left_char: char, right_char: char) {
|
|
if left_char == right_char {
|
|
self.formatted_left.push(left_char);
|
|
self.formatted_right.push(right_char);
|
|
} else {
|
|
self.formatted_left.push_str(&cformat!("<r>{left_char}</>"));
|
|
self.formatted_right.push_str(&cformat!("<r>{right_char}</>"));
|
|
};
|
|
}
|
|
|
|
pub fn compare(&mut self, compare: Compare) -> Compared {
|
|
let right_chars: Vec<char> = compare.right.chars().collect();
|
|
|
|
for (i, left_char) in compare.left.chars().enumerate() {
|
|
match right_chars.get(i) {
|
|
Some(right_char) => self.format_compare(left_char, *right_char),
|
|
None => break,
|
|
}
|
|
}
|
|
Compared {
|
|
right: self.formatted_right.clone(),
|
|
left: self.formatted_left.clone()
|
|
}
|
|
}
|
|
}
|