use rpm_spec_analyzer::profile::{MacroValue, Profile, Provenance};
pub(super) const MAX_MACRO_LABEL_WIDTH: usize = 48;
pub(super) const MAX_PROFILE_NAME_WIDTH: usize = 40;
pub(super) const COMPACT_VALUE_MAX_LEN: usize = 80;
pub(super) fn format_macro_value_inline(value: &MacroValue) -> String {
match value {
MacroValue::Literal(s) => s.clone(),
MacroValue::Builtin => "<builtin>".to_string(),
MacroValue::Raw { body, multiline } => {
if *multiline {
format!("<multiline {} chars>", body.len())
} else {
body.clone()
}
}
}
}
pub(super) fn format_opts(opts: Option<&str>) -> String {
opts.map(|o| format!("({o})")).unwrap_or_default()
}
pub(super) fn format_provenance(prov: &Provenance) -> String {
match prov {
Provenance::Builtin { profile } => format!("builtin:{profile}"),
Provenance::Showrc { level, path: _ } => format!("showrc:{level}"),
Provenance::Override => "override".to_string(),
}
}
pub(super) fn compact_value(value: &MacroValue) -> String {
let rendered = format_macro_value_inline(value);
if rendered.len() <= COMPACT_VALUE_MAX_LEN {
rendered
} else {
let mut truncated: String = rendered.chars().take(COMPACT_VALUE_MAX_LEN - 1).collect();
truncated.push('…');
truncated
}
}
pub(super) fn family_label(p: &Profile) -> String {
p.identity
.family
.map(|f| format!("{f:?}"))
.unwrap_or_else(|| "-".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
use rpm_spec_analyzer::profile::Provenance;
#[test]
fn format_macro_value_literal() {
assert_eq!(
format_macro_value_inline(&MacroValue::Literal(".el9".into())),
".el9"
);
}
#[test]
fn format_macro_value_builtin() {
assert_eq!(format_macro_value_inline(&MacroValue::Builtin), "<builtin>");
}
#[test]
fn format_macro_value_raw_single_and_multiline() {
let one = MacroValue::Raw {
body: "%{?_with_foo:1}".into(),
multiline: false,
};
assert_eq!(format_macro_value_inline(&one), "%{?_with_foo:1}");
let many = MacroValue::Raw {
body: "line1\nline2\nline3".into(),
multiline: true,
};
let rendered = format_macro_value_inline(&many);
assert!(rendered.starts_with("<multiline "));
assert!(rendered.ends_with(" chars>"));
}
#[test]
fn format_provenance_all_variants() {
assert_eq!(
format_provenance(&Provenance::Builtin {
profile: "generic".into()
}),
"builtin:generic"
);
assert_eq!(
format_provenance(&Provenance::Showrc {
level: -13,
path: None,
}),
"showrc:-13"
);
assert_eq!(format_provenance(&Provenance::Override), "override");
}
#[test]
fn compact_value_truncates_long_values() {
let long = "a".repeat(200);
let v = MacroValue::Raw {
body: long,
multiline: false,
};
let s = compact_value(&v);
assert!(s.chars().count() == COMPACT_VALUE_MAX_LEN);
assert!(s.ends_with('…'));
}
}