cellos-projector 0.5.1

Projection layer for CellOS — consumes JetStream CloudEvents into in-memory cell/formation state. Used by cellos-server.
Documentation
//! Compile-time build-info helpers shared by the three projector binaries
//! (`cellos-projector`, `cellos-state-server`, `cellos-audit-justification`).
//!
//! P4-03 introduces a uniform `--version` / `-V` handler across CellOS
//! binaries with the format:
//!
//! ```text
//! <binary-name> <semver> (<short-build-sha>)
//! ```
//!
//! The binary name is supplied by the caller (each `main.rs` knows its own
//! `[[bin]]` name); semver comes from `CARGO_PKG_VERSION`; the short SHA
//! falls back to `unknown` for local dev builds where the release pipeline
//! has not exported a commit stamp.

/// Build SHA stamped at compile time. Prefer `VERGEN_GIT_SHA` (set by a
/// `vergen`-style build script when present); fall back to the `GIT_COMMIT`
/// env var the release pipeline already exports. `unknown` is fine for local
/// dev builds — release attestation hits the env-var path.
pub const BUILD_SHA: &str = match option_env!("VERGEN_GIT_SHA") {
    Some(s) => s,
    None => match option_env!("GIT_COMMIT") {
        Some(s) => s,
        None => "unknown",
    },
};

/// Truncate the build SHA to the conventional 7-character short form.
/// Inputs shorter than 7 chars (including `unknown`) are returned unchanged.
pub fn short_sha() -> &'static str {
    if BUILD_SHA.len() > 7 {
        &BUILD_SHA[..7]
    } else {
        BUILD_SHA
    }
}

/// Format the standard P4-03 version line for a given binary name.
///
/// ```text
/// <binary> <semver> (<short-sha>)
/// ```
///
/// `binary` is taken as a parameter rather than read from `argv[0]` so the
/// format is stable regardless of how the binary was invoked (path, symlink,
/// `busybox`-style multi-call dispatch).
pub fn version_line(binary: &str, semver: &str) -> String {
    format!("{binary} {semver} ({})", short_sha())
}

/// Inspect `argv[1]` for `--version` / `-V`; if present, print the version
/// line for `binary` (using `CARGO_PKG_VERSION` of the calling crate, passed
/// in as `semver`) and return `true`. Returns `false` (and prints nothing)
/// when no version flag is present.
///
/// `semver` is supplied by the caller because `env!("CARGO_PKG_VERSION")`
/// resolves at the *call site* — that's the calling binary's version, which
/// is what we want, not the library crate's version.
pub fn print_version_if_requested(binary: &str, semver: &str) -> bool {
    let arg = match std::env::args().nth(1) {
        Some(s) => s,
        None => return false,
    };
    if arg != "--version" && arg != "-V" {
        return false;
    }
    println!("{}", version_line(binary, semver));
    true
}

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

    #[test]
    fn short_sha_is_at_most_seven_chars() {
        // Either we got a real 40-char hex (truncated to 7) or a fallback
        // shorter than 7 — never longer.
        assert!(short_sha().len() <= 7);
    }

    #[test]
    fn version_line_format_is_stable() {
        // Lock the format string against accidental drift; downstream
        // attestation tooling parses this shape.
        let line = version_line("cellos-foo", "1.2.3");
        assert!(line.starts_with("cellos-foo 1.2.3 ("));
        assert!(line.ends_with(')'));
    }
}