//! Version string rendering for `--version` and the `version` subcommand.

/// Package name from Cargo.
pub const NAME: &str = env!("CARGO_PKG_NAME");
/// Package version from Cargo.
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// Git commit hash captured at build time; `None` if git was unavailable.
pub const COMMIT: Option<&str> = option_env!("HANUKE_GIT_COMMIT");

/// Renders the version line. When `with_commit` is set, appends the git commit
/// hash the binary was built from (or "unknown" if it could not be determined).
pub fn format_version(with_commit: bool) -> String {
    if with_commit {
        format!("{NAME} {VERSION} ({})", COMMIT.unwrap_or("unknown"))
    } else {
        format!("{NAME} {VERSION}")
    }
}

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

    #[test]
    fn plain_version_is_name_and_version() {
        assert_eq!(format_version(false), format!("{NAME} {VERSION}"));
    }

    #[test]
    fn commit_version_appends_hash_in_parens() {
        let s = format_version(true);
        assert!(s.starts_with(&format!("{NAME} {VERSION} (")));
        assert!(s.ends_with(')'));
    }
}
