latexml/identity.rs
1//! Per-conversion identity banner — executable name, version, git revision and
2//! exact start time, logged once at the head of every conversion so any log
3//! names the precise binary and moment that produced it.
4//!
5//! Faithful to Perl LaTeXML, which logs `Note("$LaTeXML::IDENTITY processing
6//! $source")` at each conversion start (`bin/latexml` L83). `$IDENTITY` is
7//! `"$FindBin::Script ($LaTeXML::FULLVERSION)"` (`LaTeXML.pm` L40) — the invoked
8//! script's basename plus `"LaTeXML version <v>; revision <sha>"`, the revision
9//! filled into `Version.pm` by `make`. We mirror that (revision embedded by
10//! `build.rs` instead of `make`) and additionally stamp the exact wall-clock
11//! start time, which Perl only emits under `--verbose` (`processing started …`).
12//!
13//! Generic across every front-end: [`Converter::convert`](crate::converter::Converter::convert)
14//! emits it for `latexml_oxide` and `cortex_worker`, and `latexmlmath_oxide` (a
15//! separate digest path) emits it directly. The executable name is read from
16//! `argv[0]` at runtime, so each binary self-identifies without per-binary wiring.
17
18use std::path::Path;
19
20use chrono::{DateTime, Local};
21
22/// This crate's version (`latexml_oxide`) — the emulated engine's own version,
23/// NOT the Perl LaTeXML version it targets. Full `CARGO_PKG_VERSION`, keeping any
24/// `-rc` pre-release suffix (a log should reveal an rc); the bare-`X.Y.Z` form
25/// for BookML's version gate is [`crate::core_interface::LATEXML_VERSION`].
26pub const VERSION: &str = env!("CARGO_PKG_VERSION");
27
28/// Short git revision of the source that built this binary, embedded by
29/// `build.rs` (`"unknown"` off a checkout with no `.git` and no
30/// `LATEXML_GIT_SHA` override). Perl's `$LaTeXML::Version::REVISION`.
31pub const GIT_REVISION: &str = env!("LATEXML_GIT_SHA");
32
33/// `--version`'s text: the crate version and the [`GIT_REVISION`] stamp,
34/// `0.7.6 (revision a1b2c3d)`. The stamp is the one the conversion banner logs;
35/// printing it here lets a harness check which source built a binary without
36/// running a conversion (`ab_audit run` refuses a binary whose stamp is not the
37/// expected commit).
38pub const VERSION_WITH_REVISION: &str = concat!(
39 env!("CARGO_PKG_VERSION"),
40 " (revision ",
41 env!("LATEXML_GIT_SHA"),
42 ")"
43);
44
45/// Basename of the invoked executable — Perl's `$FindBin::Script`
46/// (`latexml_oxide`, `cortex_worker`, `latexmlmath_oxide`, …). Read from
47/// `argv[0]` so each binary self-identifies; `"latexml-oxide"` when argv is
48/// empty or unreadable (e.g. an embedder driving `Converter` directly).
49pub fn executable_name() -> String {
50 std::env::args_os()
51 .next()
52 .as_deref()
53 .map(Path::new)
54 .and_then(Path::file_name)
55 .map(|s| s.to_string_lossy().into_owned())
56 .filter(|s| !s.is_empty())
57 .unwrap_or_else(|| "latexml-oxide".to_string())
58}
59
60/// Conversion start instant. Honours `SOURCE_DATE_EPOCH` (reproducible builds),
61/// exactly as the engine's `\today`/date registers do (`tex_job.rs`), so a
62/// pinned epoch yields a deterministic banner; otherwise the local wall clock.
63fn start_time() -> DateTime<Local> {
64 start_time_at(std::env::var("SOURCE_DATE_EPOCH").ok().as_deref())
65}
66
67/// [`start_time`] given the value of `SOURCE_DATE_EPOCH`.
68fn start_time_at(source_date_epoch: Option<&str>) -> DateTime<Local> {
69 if let Some(epoch) = source_date_epoch.and_then(|e| e.trim().parse::<i64>().ok())
70 && let Some(utc) = DateTime::from_timestamp(epoch, 0)
71 {
72 return utc.with_timezone(&Local);
73 }
74 Local::now()
75}
76
77/// The one-line identity banner, e.g.
78/// `latexml_oxide (latexml-oxide 0.9.0; revision a1b2c3d) started 2026-08-21 14:32:05 -0400`.
79///
80/// Emit it through [`Note!`](latexml_core::Note) so it reaches both stderr and
81/// the captured `.latexml.log`, and inherits the verbosity gate (`--quiet`
82/// suppresses it).
83pub fn identity_banner() -> String {
84 format!(
85 "{exe} (latexml-oxide {VERSION}; revision {GIT_REVISION}) started {when}",
86 exe = executable_name(),
87 when = start_time().format("%Y-%m-%d %H:%M:%S %z"),
88 )
89}
90
91#[cfg(test)]
92mod tests {
93 use super::*;
94
95 /// The banner carries all four requested fields: an executable name, the
96 /// crate version, the embedded revision, and a `started <timestamp>` stamp.
97 #[test]
98 fn banner_has_exe_version_revision_and_time() {
99 let banner = identity_banner();
100 assert!(
101 banner.contains(VERSION),
102 "banner {banner:?} missing version {VERSION:?}"
103 );
104 assert!(
105 banner.contains("revision "),
106 "banner {banner:?} missing revision field"
107 );
108 assert!(
109 banner.contains(GIT_REVISION),
110 "banner {banner:?} missing revision {GIT_REVISION:?}"
111 );
112 assert!(
113 banner.contains(" started "),
114 "banner {banner:?} missing start-time stamp"
115 );
116 assert!(
117 !executable_name().is_empty(),
118 "executable name must be non-empty"
119 );
120 }
121
122 /// `--version` names the revision stamp in the shape `ab_audit` reads:
123 /// `<version> (revision <stamp>)`.
124 #[test]
125 fn version_text_names_the_revision() {
126 assert_eq!(
127 VERSION_WITH_REVISION,
128 format!("{VERSION} (revision {GIT_REVISION})")
129 );
130 }
131
132 /// `SOURCE_DATE_EPOCH` pins the timestamp for a deterministic (reproducible)
133 /// banner. Epoch 0 = 1970-01-01 UTC; the local-time render must land on the
134 /// 1969-12-31/1970-01-01 boundary depending on the tester's zone.
135 #[test]
136 fn source_date_epoch_pins_the_timestamp() {
137 let when = start_time_at(Some("0")).format("%Y-%m-%d").to_string();
138 assert!(
139 when == "1970-01-01" || when == "1969-12-31",
140 "SOURCE_DATE_EPOCH=0 should render the epoch date, got {when:?}"
141 );
142 }
143}