Skip to main content

brokk_bifrost_python/
distribution.rs

1//! Distribution metadata helpers. Import module identities are separate.
2
3/// PEP 503 normalization collapses every run of '-', '_' and '.'.
4pub fn normalize_distribution_name(name: &str) -> String {
5    let mut normalized = String::with_capacity(name.len());
6    for ch in name.trim().chars() {
7        if matches!(ch, '-' | '_' | '.') {
8            if !normalized.ends_with('-') {
9                normalized.push('-');
10            }
11        } else {
12            normalized.push(ch.to_ascii_lowercase());
13        }
14    }
15    normalized
16}
17
18pub fn metadata_header(metadata: &str, name: &str) -> Option<String> {
19    metadata
20        .lines()
21        .take_while(|line| !line.is_empty())
22        .find_map(|line| {
23            let (key, value) = line.split_once(':')?;
24            (key.eq_ignore_ascii_case(name) && !value.trim().is_empty())
25                .then(|| value.trim().to_owned())
26        })
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn distribution_normalization_and_metadata_body_boundary() {
35        assert_eq!(normalize_distribution_name("Foo__...--Bar"), "foo-bar");
36        assert_eq!(
37            metadata_header("Name: Foo\nVersion: 1\n\nName: body\n", "name").as_deref(),
38            Some("Foo")
39        );
40        assert!(metadata_header("Description: text\n\nVersion: 9\n", "Version").is_none());
41    }
42}