Merge pull request #8 from teohhanhui/riir

Clean up build.rs
This commit is contained in:
Teoh Han Hui 2024-06-29 17:39:12 +08:00 committed by GitHub
commit 4f1efbc699
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 78 additions and 76 deletions

View file

@ -49,116 +49,118 @@ where
variants.insert(variant, distro); variants.insert(variant, distro);
} }
let mut buf = " let mut buf = r###"
#[derive(Clone, Eq, PartialEq, Hash, Debug)] #[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub enum Distro { pub enum Distro {
" "###
.to_owned(); .to_owned();
for (variant, distro) in &variants { for (variant, AsciiDistro { pattern, .. }) in &variants {
buf.push_str(&format!( buf.push_str(&format!(
" r###"
// {}) // {pattern})
{variant}, {variant},
", "###,
distro.pattern
)); ));
} }
buf.push_str( buf.push_str(
" r###"
} }
",
);
buf.push_str(
"
impl Distro { impl Distro {
pub fn ascii_art(&self) -> &str { pub fn detect<S>(name: S) -> Option<Self>
let art = match self { where
", S: AsRef<str>,
{
let name = name.as_ref().to_lowercase();
"###,
); );
let quotes = "#".repeat(80); for (variant, AsciiDistro { pattern, .. }) in &variants {
for (variant, distro) in &variants { let patterns = pattern.split('|').map(|s| s.trim());
buf.push_str(&format!( let mut conds = vec![];
"
Self::{variant} => r{quotes}\" for m in patterns {
{} let stripped = m.trim_matches(['*', '\'', '"']).to_lowercase();
\"{quotes},
", if stripped.contains(['*', '"']) {
distro.art 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}");
buf.push_str(
"
};
&art[1..(art.len() - 1)]
}
}
",
);
buf.push_str(
"
impl Distro {
pub fn detect(name: &str) -> Option<Self> {
",
);
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);
} }
// Exact matches // Exact matches
if m.trim_matches('*') == m { if m.trim_matches('*') == m {
condition.push(format!("name == r#\"{}\"#", stripped)); conds.push(format!(r###"name == "{stripped}""###));
continue; continue;
} }
// Both sides are * // Both sides are *
if m.starts_with('*') && m.ends_with('*') { 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; continue;
} }
// Ends with * // Ends with *
if m.ends_with('*') { if m.ends_with('*') {
condition.push(format!("name.starts_with(r#\"{}\"#)", stripped)); conds.push(format!(r###"name.starts_with("{stripped}")"###));
continue; continue;
} }
// Starts with * // Starts with *
if m.starts_with('*') { if m.starts_with('*') {
condition.push(format!("name.ends_with(r#\"{}\"#)", stripped)); conds.push(format!(r###"name.ends_with("{stripped}")"###));
continue; continue;
} }
} }
let condition = condition.join(" || "); let condition = conds.join(" || ");
buf.push_str(&format!(" buf.push_str(&format!(
r###"
if {condition} {{ if {condition} {{
return Some(Self::{variant}); 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"); fs::write(out_path.join("distros.rs"), buf).expect("couldn't write distros.rs");
} }

View file

@ -82,21 +82,21 @@ fn init_tracing_subsriber(debug: bool) -> Result<()> {
let targets = match env::var("RUST_LOG") { let targets = match env::var("RUST_LOG") {
Ok(var) => Targets::from_str(&var) Ok(var) => Targets::from_str(&var)
.map_err(|e| { .map_err(|e| {
eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e); eprintln!("Ignoring `RUST_LOG={var:?}`: {e}");
}) })
.unwrap_or_default(), .unwrap_or_default(),
Err(env::VarError::NotPresent) => { Err(env::VarError::NotPresent) => {
let targets = Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL); Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL)
if debug { },
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) targets.with_target(env!("CARGO_CRATE_NAME"), Level::DEBUG)
} else { } else {
targets targets
}
},
Err(e) => {
eprintln!("Ignoring `RUST_LOG`: {}", e);
Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL)
},
}; };
subscriber.with(targets) subscriber.with(targets)
}; };