clew 0.1.0

A lightweight, local, git-native project tracker for humans and agents.
Documentation
use std::collections::BTreeMap;

/// Extract `#NNNN` references from a path.md document in document order.
///
/// The parser is permissive by design: it ignores prose and malformed hashes,
/// and returns every well-formed four-digit reference it sees.
pub fn references(text: &str) -> Vec<u32> {
    text.lines().filter_map(reference_in_line).collect()
}

fn reference_in_line(line: &str) -> Option<u32> {
    let hash = line.find('#')?;
    let id_start = hash + 1;
    let id_end = id_start + 4;
    let id_text = line.get(id_start..id_end)?;
    if !id_text.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }
    id_text.parse().ok()
}

/// Remove entries for `id` from a path.md document.
///
/// The parser is intentionally permissive: if a line contains a `#NNNN`
/// reference matching `id`, that whole line is removed. Everything else is
/// preserved verbatim.
///
/// TODO: tighten this if `path.md` grows richer prose comments; today path is
/// defined as a priority list, so dropping the whole matching line is correct.
pub fn remove(text: &str, id: u32) -> String {
    let needle = format!("#{id:04}");
    text.lines()
        .filter(|line| !line.contains(&needle))
        .collect::<Vec<_>>()
        .join("\n")
        + if text.ends_with('\n') { "\n" } else { "" }
}

/// Normalize known `#NNNN[-slug]` references to `#NNNN-current-slug`.
/// Unknown references and surrounding annotations are preserved.
///
/// TODO: this only rewrites the first reference per line. That matches the
/// current path list format; don't generalize until path needs it.
pub fn normalize(text: &str, slugs_by_id: &BTreeMap<u32, String>) -> String {
    text.lines()
        .map(|line| normalize_line(line, slugs_by_id))
        .collect::<Vec<_>>()
        .join("\n")
        + if text.ends_with('\n') { "\n" } else { "" }
}

fn normalize_line(line: &str, slugs_by_id: &BTreeMap<u32, String>) -> String {
    let Some(hash) = line.find('#') else {
        return line.to_string();
    };
    let id_start = hash + 1;
    let id_end = id_start + 4;
    let Some(id_text) = line.get(id_start..id_end) else {
        return line.to_string();
    };
    if !id_text.chars().all(|c| c.is_ascii_digit()) {
        return line.to_string();
    }
    let Ok(id) = id_text.parse::<u32>() else {
        return line.to_string();
    };
    let Some(slug) = slugs_by_id.get(&id) else {
        return line.to_string();
    };

    let mut ref_end = id_end;
    if line[id_end..].starts_with('-') {
        ref_end += 1;
        for ch in line[ref_end..].chars() {
            if ch.is_ascii_alphanumeric() || ch == '-' {
                ref_end += ch.len_utf8();
            } else {
                break;
            }
        }
    }

    format!("{}#{id:04}-{}{}", &line[..hash], slug, &line[ref_end..])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn references_extracts_ids_and_ignores_prose() {
        let input = "# Path\n\nnotes\n- #0002-b\n- not #abcd\n- #0042\n";
        assert_eq!(references(input), vec![2, 42]);
    }

    #[test]
    fn remove_drops_matching_reference_lines() {
        let input = "# Path\n\n- #0001-a\n- #0002-b\n";
        assert_eq!(remove(input, 1), "# Path\n\n- #0002-b\n");
    }

    #[test]
    fn remove_preserves_non_matching_text() {
        let input = "# Path\n\nnotes\n- #0002-b\n";
        assert_eq!(remove(input, 1), input);
    }

    #[test]
    fn normalize_rewrites_known_references_and_preserves_annotations() {
        let mut slugs = BTreeMap::new();
        slugs.insert(2, "current-slug".to_string());

        assert_eq!(
            normalize("- #0002-old-slug // note\n- #0003-missing\n", &slugs),
            "- #0002-current-slug // note\n- #0003-missing\n"
        );
    }
}