sys-compare/src/comparemode.rs

125 lines
3.7 KiB
Rust
Raw Normal View History

2024-03-26 17:14:24 +00:00
use Fasching::snapshot::{Snapshot, SnapshotChangeType, SnapshotCompareResult};
use Fasching::{compare_snapshots, import_snapshot};
2024-03-29 04:18:38 +00:00
use crate::options::Arguments;
2024-03-18 22:20:57 +00:00
pub struct CompareMode {
left: Snapshot,
right: Snapshot,
2024-03-29 04:18:38 +00:00
selection: Option<String>,
2024-03-29 20:38:58 +00:00
count_only: Option<bool>,
2024-03-29 04:18:38 +00:00
options: Arguments,
2024-03-18 22:35:19 +00:00
result_type: SnapshotChangeType,
results: SnapshotCompareResult,
2024-03-18 22:20:57 +00:00
}
impl CompareMode {
2024-03-29 20:38:58 +00:00
pub fn new(options: Arguments, left: String, right: String, selection: Option<String>, count_only: Option<bool>) -> CompareMode {
2024-03-30 04:29:29 +00:00
let left = import_snapshot(left).unwrap_or_default();
let right = import_snapshot(right).unwrap_or_default();
2024-03-18 22:20:57 +00:00
2024-03-18 22:28:33 +00:00
CompareMode {
2024-03-18 22:31:45 +00:00
left,
right,
2024-03-29 04:18:38 +00:00
selection,
2024-03-29 20:38:58 +00:00
count_only,
2024-03-29 04:18:38 +00:00
options,
2024-03-18 22:35:19 +00:00
result_type: SnapshotChangeType::None,
2024-03-18 22:28:33 +00:00
results: SnapshotCompareResult {
created: vec![],
deleted: vec![],
changed: vec![],
},
}
2024-03-18 22:20:57 +00:00
}
}
2024-03-29 04:21:51 +00:00
impl CompareMode {
pub(crate) fn run(&mut self) {
2024-03-29 04:18:38 +00:00
let selector = match &self.selection {
2024-03-26 17:14:24 +00:00
None => "none",
2024-03-29 04:18:38 +00:00
Some(r) => {
r.as_str()
},
2024-03-18 22:47:38 +00:00
};
2024-03-25 20:51:52 +00:00
let results = match compare_snapshots(self.left.clone(), self.right.clone()) {
Some(x) => x,
None => panic!("Error Comparing Snapshot"),
};
2024-03-18 22:35:19 +00:00
self.results = results.1;
self.result_type = results.0;
2024-03-18 22:47:38 +00:00
2024-03-29 20:38:58 +00:00
macro_rules! print_if_not_empty {
($ret:expr, $co:expr) => {if let Some(count_only) = $co {
if count_only {
println!("{}", $ret.len());
} else {
$ret.iter().for_each(|e| println!("{e}"));
println!("Created: {:?}", $ret.len());
}
} else {
$ret.iter().for_each(|e| println!("{e}"));
println!("Created: {:?}", $ret.len());
}
};
}
2024-03-18 22:47:38 +00:00
match selector {
"created" => {
2024-03-29 20:38:58 +00:00
print_if_not_empty!(self.results.created, self.count_only);
2024-03-26 17:14:24 +00:00
}
2024-03-18 22:47:38 +00:00
"deleted" => {
2024-03-29 20:38:58 +00:00
print_if_not_empty!(self.results.deleted, self.count_only);
2024-03-26 17:14:24 +00:00
}
2024-03-18 22:47:38 +00:00
"changed" => {
2024-03-29 20:38:58 +00:00
print_if_not_empty!(self.results.changed, self.count_only);
2024-03-18 22:47:38 +00:00
}
2024-03-18 22:54:57 +00:00
"none" => {
2024-03-18 22:47:38 +00:00
println!("Created: {:?}", self.results.created.len());
println!("Deleted: {:?}", self.results.deleted.len());
println!("Changed: {:?}", self.results.changed.len());
2024-03-26 17:14:24 +00:00
}
2024-03-18 22:54:57 +00:00
_ => {
// println!("Created: {:?}", self.results.created.len());
// println!("Deleted: {:?}", self.results.deleted.len());
// println!("Changed: {:?}", self.results.changed.len());
2024-03-18 22:47:38 +00:00
}
}
2024-03-18 22:20:57 +00:00
}
2024-03-29 20:38:58 +00:00
fn return_ret(&self) {
}
2024-03-18 22:20:57 +00:00
}
2024-03-21 21:24:08 +00:00
2024-03-29 20:38:58 +00:00
2024-03-21 21:24:08 +00:00
#[cfg(test)]
mod tests {
use crate::comparemode::CompareMode;
2024-03-21 22:51:45 +00:00
use crate::createmode::CreateMode;
2024-03-26 17:14:24 +00:00
use std::env;
use std::fmt::format;
2024-03-21 21:24:08 +00:00
#[test]
fn compare_mode() {
2024-03-21 22:50:27 +00:00
let user = whoami::username();
2024-03-25 16:36:46 +00:00
println!("{user}");
2024-03-21 22:50:27 +00:00
let left = format!("/home/{}/test1", user);
2024-03-25 17:00:00 +00:00
let left_dir = format!("/home/{}/Documents/", user);
2024-03-25 16:36:46 +00:00
println!("{left}");
2024-03-21 22:50:27 +00:00
let right = format!("/home/{}/test2", user);
2024-03-25 17:00:00 +00:00
let right_dir = format!("/home/{}/Documents/", user);
2024-03-25 16:36:46 +00:00
println!("{right}");
2024-03-29 04:18:38 +00:00
let mut n1 = CreateMode::new(vec![], left.clone());
2024-03-25 16:36:46 +00:00
n1.run();
2024-03-29 04:18:38 +00:00
let mut n2 = CreateMode::new(vec![], right.clone());
2024-03-25 16:36:46 +00:00
n2.run();
2024-03-21 22:51:45 +00:00
2024-03-29 04:18:38 +00:00
let cm = CompareMode::new(vec![], left.clone(), right.clone(), );
2024-03-21 21:24:08 +00:00
}
}