kartta-lang-rust 0.0.0

Rust extraction for the kartta code graph: items, members, calls and the files `mod` declarations name
Documentation
// constraint: the scan reads only the header, the leading run of blank lines, `//` comments, doc comments included,
// and `/* */` blocks, and stops at the first item, so an ordinary file costs a short header scan and allocates
// nothing; a marker counts only in comment text, never in code after a closing `*/`; a UTF-8 byte order mark before
// the header is no item
pub(crate) fn is_generated_source(source: &[u8]) -> bool {
    let source = source.strip_prefix(b"\xEF\xBB\xBF").unwrap_or(source);
    let mut in_block = false;
    for raw in source.split(|&byte| byte == b'\n') {
        let mut line = raw.trim_ascii();

        if in_block {
            let (inside, after) = find(line, b"*/").map_or((line, None), |end| {
                (&line[..end], Some(line[end + 2..].trim_ascii()))
            });
            if marks_generated(inside) {
                return true;
            }
            let Some(after) = after else {
                continue;
            };
            in_block = false;
            line = after;
        }

        match header_line(line) {
            HeaderLine::Marked => return true,
            HeaderLine::Header => {}
            HeaderLine::OpensBlock => in_block = true,
            HeaderLine::Code => break,
        }
    }

    false
}

enum HeaderLine {
    Marked,
    Header,
    OpensBlock,
    Code,
}

// constraint: a block comment closed on its line is header up to its `*/`, and the rest of the line reads as a line
// of its own, so a comment after it stays header and code after it ends the header unread
fn header_line(mut line: &[u8]) -> HeaderLine {
    loop {
        if line.is_empty() {
            return HeaderLine::Header;
        }
        if line.starts_with(b"//") {
            return marked_or(line, HeaderLine::Header);
        }
        if !line.starts_with(b"/*") {
            return HeaderLine::Code;
        }

        let Some(end) = find(&line[2..], b"*/") else {
            return marked_or(line, HeaderLine::OpensBlock);
        };
        let close = 2 + end + 2;
        if marks_generated(&line[..close]) {
            return HeaderLine::Marked;
        }
        line = line[close..].trim_ascii();
    }
}

fn marked_or(text: &[u8], otherwise: HeaderLine) -> HeaderLine {
    if marks_generated(text) {
        HeaderLine::Marked
    } else {
        otherwise
    }
}

fn marks_generated(line: &[u8]) -> bool {
    announces(line, b"@generated") || announces(line, b"DO NOT EDIT")
}

// constraint: documenting the convention means quoting the marker in backticks, so an occurrence inside a code span is
// a mention, not a banner
fn announces(line: &[u8], marker: &[u8]) -> bool {
    let mut outside_span = true;
    for (at, &byte) in line.iter().enumerate() {
        if outside_span && line[at..].starts_with(marker) {
            return true;
        }
        outside_span ^= byte == b'`';
    }
    false
}

fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
    haystack
        .windows(needle.len())
        .position(|window| window == needle)
}

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

    #[test]
    fn a_banner_announces_even_when_the_line_quotes_something_else() {
        assert!(announces(
            b"// Code generated by `gen.exe`. DO NOT EDIT.",
            b"DO NOT EDIT"
        ));
        assert!(announces(b"// @generated", b"@generated"));
    }

    #[test]
    fn a_quoted_marker_only_mentions() {
        let mention =
            b"//! in the file's leading comment run, a `@generated` / `DO NOT EDIT` marker";

        assert!(!announces(mention, b"@generated"));
        assert!(!announces(mention, b"DO NOT EDIT"));
    }

    #[test]
    fn a_marker_after_a_closed_span_still_announces() {
        assert!(announces(b"// see `docs.md`: @generated", b"@generated"));
        assert!(!announces(b"// see `docs.md: @generated", b"@generated"));
    }

    #[test]
    fn a_line_without_the_marker_never_announces() {
        assert!(!announces(b"// an ordinary header", b"@generated"));
        assert!(!announces(b"", b"@generated"));
    }
}