appstruct-cli 0.2.0

Command-line compiler and development workflow for AppStruct applications
use std::io;

pub(super) fn validate_name(name: &str) -> io::Result<()> {
    let valid = !name.is_empty()
        && name.len() <= 64
        && name
            .bytes()
            .next()
            .is_some_and(|byte| byte.is_ascii_lowercase())
        && name
            .bytes()
            .last()
            .is_some_and(|byte| byte.is_ascii_alphanumeric())
        && name
            .bytes()
            .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-');
    if valid {
        Ok(())
    } else {
        Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "project name must be 1-64 lowercase ASCII letters, digits, or hyphens, starting with a letter",
        ))
    }
}

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

    #[test]
    fn rejects_unsafe_project_names() {
        for name in ["", "../demo", "Demo", "demo_name", "demo-"] {
            assert!(validate_name(name).is_err(), "accepted {name:?}");
        }
        assert!(validate_name("project-42").is_ok());
    }
}