From 1c9aa393e012e8dd1586053b288e948545a7fc86 Mon Sep 17 00:00:00 2001 From: Leike Date: Fri, 25 Feb 2022 14:18:35 +0800 Subject: [PATCH] =?UTF-8?q?specdiff=20=E4=BD=BF=E7=94=A8TOML=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=20=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=A4=9A=E7=BB=84SPEC=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- specdiff/Cargo.toml | 6 +- specdiff/src/lib.rs | 127 +++++++++++++++++++++++++++++ specdiff/src/main.rs | 107 ++++-------------------- specdiff/tests/common/mod.rs | 6 ++ specdiff/tests/integration_test.rs | 26 ++++++ 5 files changed, 177 insertions(+), 95 deletions(-) create mode 100644 specdiff/src/lib.rs create mode 100644 specdiff/tests/common/mod.rs create mode 100644 specdiff/tests/integration_test.rs diff --git a/specdiff/Cargo.toml b/specdiff/Cargo.toml index d5888e89..6a3630c6 100644 --- a/specdiff/Cargo.toml +++ b/specdiff/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "specdiff" -version = "0.1.0" +version = "0.1.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -12,4 +12,6 @@ reqwest = "0.11.9" similar = {version = "2.1.0", features = ["bytes", "inline"] } markdown-gen = "1.2.1" colored = "2.0.0" -console = "0.15.0" \ No newline at end of file +console = "0.15.0" +toml = "0.5.8" +serde = { version = "1.0", features = ["derive"] } \ No newline at end of file diff --git a/specdiff/src/lib.rs b/specdiff/src/lib.rs new file mode 100644 index 00000000..276d069d --- /dev/null +++ b/specdiff/src/lib.rs @@ -0,0 +1,127 @@ + +#![allow(unused_imports)] +#![allow(dead_code)] +use clap::Parser; +use similar::{ChangeTag, TextDiff}; +use markdown_gen::markdown::Markdown; +use console::{style, Style}; +pub use tokio::try_join; +use serde::Deserialize; +use toml; + +pub use std::collections::HashMap; +pub use std::fmt; +pub use std::fs::File; +pub use std::io::{Write, Read}; + +struct Line(Option); + +impl fmt::Display for Line { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.0 { + None => write!(f, " "), + Some(idx) => write!(f, "{:<4}", idx + 1), + } + } +} + +#[derive(Debug, Deserialize)] +pub struct Config { + pub path: Option, + pub addresses: Option>, +} +#[derive(Debug, Deserialize, Eq, PartialEq)] +pub struct Address{ + pub name: String, + pub x: String, + pub y: String +} + +#[derive(Parser)] +pub struct Cli { + path_config: String, +} + +impl Cli { + pub async fn get_address_list_from_cli() -> Result, Box> { + let path = Cli::parse().path_config; + get_address_list(path) + } +} +fn get_address_list(path: String) -> Result, Box> { + let mut input = String::new(); + File::open(&path) + .and_then(|mut f| f.read_to_string(&mut input))?; + let config:Config = toml::from_str(&input[..]).unwrap(); + Ok(config.addresses.unwrap()) +} + + + + + +pub async fn get_diff(name: String, specs: Vec) -> Result<(), Box> { + let diff = TextDiff::from_lines(&specs[0], &specs[1]); + + let path = name + "-diffreport.md"; + let file = File::create(&path[..]).expect("create failed"); + let mut mdfile = Markdown::new(file); + + for group in diff.grouped_ops(3).iter(){ + for op in group { + for change in diff.iter_inline_changes(op) { + let (sign, s) = match change.tag() { + ChangeTag::Delete => ("-", Style::new().red()), + ChangeTag::Insert => ("+", Style::new().green()), + ChangeTag::Equal => (" ", Style::new().dim()), + }; + print!( + "{}{} |{}", + style(Line(change.old_index())).dim(), + style(Line(change.new_index())).dim(), + s.apply_to(sign).bold(), + ); + + let mut line = format!("{:<4}|{}", Line(change.old_index()), sign); + // mdfile.write(&format!("{}{} | {}", change.old_index().unwrap(), change.new_index().unwrap(), sign)[..])?; + for (emphasized, value) in change.iter_strings_lossy() { + let st = value.clone(); + line.push_str(&format!("{}",st)); + if emphasized { + print!("{}", s.apply_to(value).underlined().on_black()); + } else { + print!("{}", s.apply_to(value)); + } + } + mdfile.write(&line[..])?; + + if change.missing_newline() { + println!(); + } + } + } + } + + + println!("data written successfully in {}", path); + println!("diff ratio: {}", diff.ratio()); + + Ok(()) +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn toml_should_word() { + + } + + #[test] + fn default_strategy_should_work() { + + } +} + diff --git a/specdiff/src/main.rs b/specdiff/src/main.rs index 16e4a861..e551b247 100644 --- a/specdiff/src/main.rs +++ b/specdiff/src/main.rs @@ -1,101 +1,22 @@ -#![allow(unused_imports)] -#![allow(dead_code)] -use clap::Parser; -use similar::{ChangeTag, TextDiff}; -use markdown_gen::markdown::Markdown; -use console::{style, Style}; - -use std::fmt; -use std::fs::File; -use std::io::{Write}; - -struct Line(Option); - -impl fmt::Display for Line { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self.0 { - None => write!(f, " "), - Some(idx) => write!(f, "{:<4}", idx + 1), - } - } -} - -#[derive(Parser)] -struct Cli { - - path1: String, - - path2: String, -} - -impl Cli { - async fn get_spec_from_html() -> Result, Box> { - let args = Cli::parse(); - let p1 = args.path1; - let p2 = args.path2; - - - let res1 = reqwest::get(p1).await?; - let res2 = reqwest::get(p2).await?; - let body = vec![res1.text().await?, res2.text().await?]; - - Ok(body) - } - - -} +use specdiff::*; #[tokio::main] async fn main() -> Result<(), Box> { - let specs = Cli::get_spec_from_html().await?; - let diff = TextDiff::from_lines(&specs[0], &specs[1]); - - let file = File::create("diffreport.md").expect("create failed"); - - let mut mdfile = Markdown::new(file); - - - for group in diff.grouped_ops(3).iter(){ - for op in group { - for change in diff.iter_inline_changes(op) { - let (sign, s) = match change.tag() { - ChangeTag::Delete => ("-", Style::new().red()), - ChangeTag::Insert => ("+", Style::new().green()), - ChangeTag::Equal => (" ", Style::new().dim()), - }; - print!( - "{}{} |{}", - style(Line(change.old_index())).dim(), - style(Line(change.new_index())).dim(), - s.apply_to(sign).bold(), - ); - - let mut line = format!("{:<4}|{}", Line(change.old_index()), sign); - // mdfile.write(&format!("{}{} | {}", change.old_index().unwrap(), change.new_index().unwrap(), sign)[..])?; - for (emphasized, value) in change.iter_strings_lossy() { - let st = value.clone(); - line.push_str(&format!("{}",st)); - if emphasized { - print!("{}", s.apply_to(value).underlined().on_black()); - } else { - print!("{}", s.apply_to(value)); - } - } - mdfile.write(&line[..])?; - - if change.missing_newline() { - println!(); - } - } - } + let addresses: Vec
= Cli::get_address_list_from_cli().await?; + let mut specs_list: HashMap> = HashMap::new(); + for address in addresses { + let f1 = reqwest::get(address.x); + let f2 = reqwest::get(address.y); + let (res1, res2) = try_join!(f1, f2)?; + let body = vec![res1.text().await?, res2.text().await?]; + specs_list.insert(address.name, body); } - - println!("data written successfully"); - println!("diff ratio: {}", diff.ratio()); - + for (name, specs) in specs_list { + get_diff(name, specs).await?; + } + Ok(()) - -} +} \ No newline at end of file diff --git a/specdiff/tests/common/mod.rs b/specdiff/tests/common/mod.rs new file mode 100644 index 00000000..137ae66a --- /dev/null +++ b/specdiff/tests/common/mod.rs @@ -0,0 +1,6 @@ +use specdiff::*; + +pub fn get_address_list(toml_str: &str) -> Result, Box> { + let decoded:Config = toml::from_str(toml_str).unwrap(); + Ok(decoded.addresses.unwrap()) +} \ No newline at end of file diff --git a/specdiff/tests/integration_test.rs b/specdiff/tests/integration_test.rs new file mode 100644 index 00000000..7a05f9ac --- /dev/null +++ b/specdiff/tests/integration_test.rs @@ -0,0 +1,26 @@ +use specdiff::*; + +mod common; + + +#[test] +fn test_toml_should_work() { + let toml_str = r#" + path = "." + + [[addresses]] + name = "fpaste_f34" + x = "https://src.fedoraproject.org/rpms/fpaste/raw/rawhide/f/fpaste.spec" + y = "https://src.fedoraproject.org/rpms/fpaste/raw/f34/f/fpaste.spec" + + [[addresses]] + name = "fpaste_epel8" + x = "https://src.fedoraproject.org/rpms/fpaste/raw/rawhide/f/fpaste.spec" + y = "https://src.fedoraproject.org/rpms/fpaste/raw/epel8/f/fpaste.spec" + "#; + + let result = common::get_address_list(toml_str).unwrap(); + let expected = vec![Address{name : "fpaste_f34".to_string(), x : "https://src.fedoraproject.org/rpms/fpaste/raw/rawhide/f/fpaste.spec".to_string(), y : "https://src.fedoraproject.org/rpms/fpaste/raw/f34/f/fpaste.spec".to_string()}, + Address{name : "fpaste_epel8".to_string(), x :"https://src.fedoraproject.org/rpms/fpaste/raw/rawhide/f/fpaste.spec".to_string(), y : "https://src.fedoraproject.org/rpms/fpaste/raw/epel8/f/fpaste.spec".to_string()}]; + assert_eq!(result, expected); +} -- Gitee