yog-codegen 0.1.0

Yog's toolkit for code generators: name cases, string literals of each language, headers, an indenting writer, writes that keep unchanged files.
Documentation
//! What every generated file opens with: who wrote it, from what, and
//! how it's written again.

use crate::syntax::Comments;

/// How a generated file is written again.
#[derive(Clone, Copy, Debug)]
pub enum Regenerate<'a> {
    /// By running this command.
    Command(&'a str),
    /// By every build, on its own.
    EveryBuild,
}

/// A generated file's header.
#[derive(Clone, Copy, Debug)]
pub struct Header<'a> {
    /// The generator (`yog-ids`, `` `yog build` ``).
    pub tool: &'a str,
    /// What it generates from (`Yog-Mod-Loader/rust/crates/yog-ids`, `content/**`).
    pub source: &'a str,
    pub regenerate: Regenerate<'a>,
}

impl Header<'_> {
    /// The header's text.
    pub fn text(&self) -> String {
        let again = match self.regenerate {
            Regenerate::Command(command) => format!("regenerate with `{command}`"),
            Regenerate::EveryBuild => "it's rewritten on every build".to_string(),
        };
        format!(
            "Generated by {} from {} — don't edit; {again}.",
            self.tool, self.source
        )
    }

    /// The header as a comment of language `L`, on a line of its own.
    pub fn comment<L: Comments>(&self) -> String {
        format!("{}\n", L::comment(&self.text()))
    }
}