From c1b30918e985608bf90476654a842f077444d31e Mon Sep 17 00:00:00 2001 From: Teoh Han Hui Date: Sat, 29 Jun 2024 17:37:22 +0800 Subject: [PATCH] Clean up build.rs --- crates/hyfetch/build.rs | 134 +++++++++++++++--------------- crates/hyfetch/src/bin/hyfetch.rs | 20 ++--- 2 files changed, 78 insertions(+), 76 deletions(-) diff --git a/crates/hyfetch/build.rs b/crates/hyfetch/build.rs index 71a9156d..f52c335b 100644 --- a/crates/hyfetch/build.rs +++ b/crates/hyfetch/build.rs @@ -49,116 +49,118 @@ where variants.insert(variant, distro); } - let mut buf = " + let mut buf = r###" #[derive(Clone, Eq, PartialEq, Hash, Debug)] pub enum Distro { -" +"### .to_owned(); - for (variant, distro) in &variants { + for (variant, AsciiDistro { pattern, .. }) in &variants { buf.push_str(&format!( - " - // {}) + r###" + // {pattern}) {variant}, -", - distro.pattern +"###, )); } buf.push_str( - " + r###" } -", - ); - buf.push_str( - " impl Distro { - pub fn ascii_art(&self) -> &str { - let art = match self { -", + pub fn detect(name: S) -> Option + where + S: AsRef, + { + let name = name.as_ref().to_lowercase(); +"###, ); - let quotes = "#".repeat(80); - for (variant, distro) in &variants { - buf.push_str(&format!( - " - Self::{variant} => r{quotes}\" -{} -\"{quotes}, -", - distro.art - )); - } + for (variant, AsciiDistro { pattern, .. }) in &variants { + let patterns = pattern.split('|').map(|s| s.trim()); + let mut conds = vec![]; - buf.push_str( - " - }; - &art[1..(art.len() - 1)] - } -} -", - ); + for m in patterns { + let stripped = m.trim_matches(['*', '\'', '"']).to_lowercase(); - buf.push_str( - " -impl Distro { - pub fn detect(name: &str) -> Option { -", - ); - for (variant, distro) in &variants { - let distro_pattern = &distro.pattern; - let matches: Vec<&str> = distro_pattern.split('|').map(|s| s.trim()).collect(); - let mut condition = Vec::new(); - - for m in matches { - let stripped = m.trim_matches(|c| c == '*' || c == '\'' || c == '"').to_lowercase(); - - if stripped.contains('*') || stripped.contains('"') { - println!("TODO: Cannot properly parse: {}", m); + if stripped.contains(['*', '"']) { + if let Some((prefix, suffix)) = stripped.split_once(r#""*""#) { + conds.push(format!( + r###"name.starts_with("{prefix}") && name.ends_with("{suffix}")"### + )); + continue; + } + println!("cargo:warning=TODO: Cannot properly parse: {m}"); } // Exact matches if m.trim_matches('*') == m { - condition.push(format!("name == r#\"{}\"#", stripped)); + conds.push(format!(r###"name == "{stripped}""###)); continue; } // Both sides are * if m.starts_with('*') && m.ends_with('*') { - condition.push(format!("(name.starts_with(r#\"{}\"#) || name.ends_with(r#\"{}\"#))", stripped, stripped)); + conds.push(format!( + r###"name.starts_with("{stripped}") || name.ends_with("{stripped}")"### + )); continue; } // Ends with * if m.ends_with('*') { - condition.push(format!("name.starts_with(r#\"{}\"#)", stripped)); + conds.push(format!(r###"name.starts_with("{stripped}")"###)); continue; } // Starts with * if m.starts_with('*') { - condition.push(format!("name.ends_with(r#\"{}\"#)", stripped)); + conds.push(format!(r###"name.ends_with("{stripped}")"###)); continue; } } - let condition = condition.join(" || "); - - buf.push_str(&format!(" + let condition = conds.join(" || "); + + buf.push_str(&format!( + r###" if {condition} {{ return Some(Self::{variant}); - }}" + }} +"### )); - }; - buf.push_str(&format!(" - None -" - )); - - buf.push_str(" } -}"); + + buf.push_str( + r###" + None + } + + pub fn ascii_art(&self) -> &str { + let art = match self { +"###, + ); + + let quotes = "#".repeat(80); + for (variant, AsciiDistro { art, .. }) in &variants { + buf.push_str(&format!( + r###" + Self::{variant} => r{quotes}" +{art} +"{quotes}, +"###, + )); + } + + buf.push_str( + r###" + }; + &art[1..(art.len() - 1)] + } +} +"###, + ); fs::write(out_path.join("distros.rs"), buf).expect("couldn't write distros.rs"); } diff --git a/crates/hyfetch/src/bin/hyfetch.rs b/crates/hyfetch/src/bin/hyfetch.rs index 429a1103..951c4890 100644 --- a/crates/hyfetch/src/bin/hyfetch.rs +++ b/crates/hyfetch/src/bin/hyfetch.rs @@ -82,21 +82,21 @@ fn init_tracing_subsriber(debug: bool) -> Result<()> { let targets = match env::var("RUST_LOG") { Ok(var) => Targets::from_str(&var) .map_err(|e| { - eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e); + eprintln!("Ignoring `RUST_LOG={var:?}`: {e}"); }) .unwrap_or_default(), Err(env::VarError::NotPresent) => { - let targets = Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL); - if debug { - targets.with_target(env!("CARGO_CRATE_NAME"), Level::DEBUG) - } else { - targets - } - }, - Err(e) => { - eprintln!("Ignoring `RUST_LOG`: {}", e); Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL) }, + Err(e) => { + eprintln!("Ignoring `RUST_LOG`: {e}"); + Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL) + }, + }; + let targets = if debug { + targets.with_target(env!("CARGO_CRATE_NAME"), Level::DEBUG) + } else { + targets }; subscriber.with(targets) };