diff view

This commit is contained in:
rhpidfyre 2025-04-09 19:43:23 -03:00
parent 1049f9bcf6
commit 96f5213264
3 changed files with 38 additions and 33 deletions

View File

@ -1,31 +1,33 @@
use color_print::cformat;
pub struct Compare {
pub right: String,
pub left: String,
pub eval: String
}
type Compared = Compare;
pub struct Compared {
pub right: String,
pub left: String,
}
pub struct Comparer {
formatted_right: Vec<String>,
formatted_left: Vec<String>,
formatted_eval: Vec<String>
formatted_right: String,
formatted_left: String
}
impl Comparer {
pub const fn new() -> Self {
Self {
formatted_right: Vec::new(),
formatted_left: Vec::new(),
formatted_eval: Vec::new(),
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.to_string());
self.formatted_right.push(right_char.to_string());
self.formatted_left.push(left_char);
self.formatted_right.push(right_char);
} else {
self.formatted_left.push(color_print::cformat!("<r>{left_char}</>"));
self.formatted_right.push(color_print::cformat!("<r>{right_char}</>"));
self.formatted_left.push_str(&cformat!("<r>{left_char}</>"));
self.formatted_right.push_str(&cformat!("<r>{right_char}</>"));
};
}
@ -35,18 +37,12 @@ impl Comparer {
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 => {
compare.eval.chars().for_each(|char| {
// self.formatted_eval
});
break
},
None => break,
}
}
Compared {
right: self.formatted_right.concat(),
left: self.formatted_left.concat(),
eval: self.formatted_eval.concat()
right: self.formatted_right.clone(),
left: self.formatted_left.clone()
}
}
}
}

View File

@ -1,5 +1,6 @@
use std::num::ParseIntError;
#[allow(dead_code)]
pub enum EvalError {
LeftMissing,
RightMissing,
@ -44,4 +45,4 @@ pub fn eval(expression: Vec<&str>) -> Result<EvalResult, EvalError> {
"^" | "xor" => Ok(wrap_result(left ^ right)),
str => Err(EvalError::InvalidOperator(str.to_owned()))
}
}
}

View File

@ -29,26 +29,34 @@ impl Out {
match eval_error {
EvalError::I64Conversion(parse_int_error) => println!("{parse_int_error}"),
EvalError::InvalidOperator(invalid_op) => println!("The operator {:?} is invalid.", invalid_op),
EvalError::LeftMissing => println!("Left side expression missing."),
EvalError::RightMissing => println!("Right side expression missing."),
}
}
fn padded(&self, diffed_left: &str, diffed_right: &str, diffed_eval: String) -> String {
let diffed_eval_chars = diffed_eval.chars().count();
let mut char_counts = [diffed_left.chars().count(), diffed_right.chars().count(), diffed_eval_chars];
char_counts.sort();
let subtracted_count = char_counts.last().map_or(0, |biggest_len| {
*biggest_len-diffed_eval_chars
});
format!("{}{diffed_eval}", "0".repeat(subtracted_count))
}
pub fn evaled(&self, eval_out: Result<EvalResult, EvalError>) {
eval_out.map_or_else(|e| self.error(e), |result| {
let eval_base2 = base::binary(result.eval);
let left = base::binary(result.input.left);
let right = base::binary(result.input.right);
let mut differ = diff::Comparer::new();
let diffed = differ.compare(Compare {
right: right.clone(),
left: left.clone(),
eval: eval_base2.clone(),
right: base::binary(result.input.right),
left: base::binary(result.input.left),
});
let padded_eval = self.padded(&diffed.left, &diffed.right, base::binary(result.eval));
println!("{}", diffed.left);
println!("{}", diffed.right);
println!("{}", "-".repeat(eval_base2.chars().count()));
println!("{}", diffed.eval)
println!("{}", "-".repeat(padded_eval.chars().count()));
println!("{}", padded_eval)
})
}
}
@ -88,4 +96,4 @@ impl Bitfyre {
}
}
}
}
}