kanbrain-core 0.6.0

The core of kanbrain (the `kb` command line and the kanban app): entries, markdown storage, tags and time tracking. Internal: its API follows kanbrain's versions and isn't meant for other crates.
Documentation
//! Rules for user-typed text: tags, file-name slugs, and headings inside descriptions and comments.

/// Slugs in file names are cut at this many characters.
const MAX_SLUG_LEN: usize = 40;

/// Splits on commas/whitespace, drops a leading `#`, lowercases and dedupes.
pub fn parse_tags(s: &str) -> Vec<String> {
    let mut tags: Vec<String> = Vec::new();
    for raw in s.split(|c: char| c == ',' || c.is_whitespace()) {
        let tag = raw.trim_start_matches('#').to_lowercase();
        if !tag.is_empty() && !tags.contains(&tag) {
            tags.push(tag);
        }
    }
    tags
}

/// `1 session`, `3 sessions`: `n` with its noun, `one` for one and `many` otherwise (`0 sessions`).
pub fn counted(n: usize, one: &str, many: &str) -> String {
    format!("{n} {}", if n == 1 { one } else { many })
}

/// A title on one line, as its file's `# ` line holds it: line breaks typed into it (`kb add $'Plan\nSteps'`) become
/// spaces, rather than lines the file would read as something else, and the ends are trimmed.
pub fn one_line(title: &str) -> String {
    // A lone `\r` is a line break too, as markdown reads it, though `str::lines` keeps it.
    title.split(['\n', '\r']).map(str::trim).filter(|line| !line.is_empty()).collect::<Vec<_>>().join(" ")
}

/// `Fix: prod login!` → `fix-prod-login`; ASCII only so the name works on every file system.
pub fn slugify(title: &str) -> String {
    let mut slug = String::new();
    for c in title.chars() {
        if slug.len() >= MAX_SLUG_LEN {
            break;
        }
        if c.is_ascii_alphanumeric() {
            slug.push(c.to_ascii_lowercase());
        } else if !slug.ends_with('-') {
            slug.push('-');
        }
    }
    let slug = slug.trim_matches('-');
    if slug.is_empty() { "task".to_string() } else { slug.to_string() }
}

/// In a task file `## ` starts a section and, inside Comments, `### ` starts a comment.
/// Headings typed into a description or comment are pushed below those levels so they
/// can't break the file. Returns the text and whether anything was changed.
pub fn sanitize_description(text: &str) -> (String, bool) {
    demote(text, &["## "], "### ")
}

/// See [`sanitize_description`].
pub fn sanitize_comment(text: &str) -> (String, bool) {
    demote(text, &["## ", "### "], "#### ")
}

/// Rewrites lines starting with any of `prefixes` to start with `to` instead.
fn demote(text: &str, prefixes: &[&str], to: &str) -> (String, bool) {
    let mut changed = false;
    let lines: Vec<String> = text
        .lines()
        .map(|line| match prefixes.iter().find_map(|p| line.strip_prefix(p)) {
            Some(rest) => {
                changed = true;
                format!("{to}{rest}")
            }
            None => line.to_string(),
        })
        .collect();
    (lines.join("\n").trim().to_string(), changed)
}

#[cfg(test)]
#[path = "../../tests/model/text.rs"]
mod tests;