Skip to main content

recall_server/
lib.rs

1//! Recall's server half: SQLite persistence, LLM-assisted merge, and the
2//! HTTP API.
3//!
4//! The routes, status codes, JSON shapes and auth behavior are deliberately
5//! identical to the Node implementation this replaces, because during a
6//! migration a machine still on the old client and one already on this
7//! binary talk to the same deployment.
8
9#![deny(missing_docs)]
10
11pub mod admin;
12pub mod config;
13pub mod merge;
14pub mod server;
15pub mod store;
16
17pub use config::{Config, ConfigError, TlsMode};
18pub use merge::Merger;
19pub use server::Server;
20pub use store::Store;
21
22use time::OffsetDateTime;
23
24/// A timestamp in the format every stored row and every API response uses:
25/// JavaScript's `Date.toISOString()` — millisecond precision, `Z` suffix,
26/// e.g. `2026-09-03T21:49:55.191Z`.
27///
28/// Rows written by the Node server are already in the database in this
29/// shape, and clients and the admin page display them, so the format is
30/// part of the compatibility surface rather than a style choice.
31pub fn now() -> String {
32    format_timestamp(OffsetDateTime::now_utc())
33}
34
35/// [`now`]'s format, for a moment other than now: an expiry, or the
36/// cut-off a sweep compares against. Being one fixed-width format, two of
37/// these compare as strings in the same order as the times they name, which
38/// is what lets SQLite compare them.
39pub(crate) fn format_timestamp(at: OffsetDateTime) -> String {
40    // `[subsecond digits:3]` is load-bearing: it must render actual
41    // milliseconds, not three literal zeroes.
42    let fmt = time::macros::format_description!(
43        "[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z"
44    );
45    at.to_offset(time::UtcOffset::UTC)
46        .format(&fmt)
47        .expect("the timestamp format is a compile-time constant")
48}
49
50/// Reads a timestamp [`now`] wrote. [`None`] for anything else.
51pub(crate) fn parse_timestamp(text: &str) -> Option<OffsetDateTime> {
52    OffsetDateTime::parse(text, &time::format_description::well_known::Rfc3339).ok()
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[test]
60    fn timestamps_read_back_to_the_same_moment() {
61        let at = OffsetDateTime::from_unix_timestamp_nanos(1_788_472_195_191_000_000).unwrap();
62        assert_eq!(parse_timestamp(&format_timestamp(at)), Some(at));
63        assert_eq!(parse_timestamp("yesterday"), None);
64    }
65
66    #[test]
67    fn timestamps_match_javascripts_toisostring() {
68        let t = now();
69        assert_eq!(t.len(), 24, "got {t}");
70        assert!(t.ends_with('Z'), "got {t}");
71        assert_eq!(&t[10..11], "T", "got {t}");
72        assert_eq!(&t[19..20], ".", "got {t}");
73        assert!(
74            t[20..23].chars().all(|c| c.is_ascii_digit()),
75            "milliseconds must be digits, got {t}"
76        );
77    }
78
79    /// The Go port had a bug where the layout rendered three literal zeroes
80    /// instead of milliseconds. Any real sub-second value must survive.
81    #[test]
82    fn renders_real_milliseconds_not_literal_zeroes() {
83        let at = OffsetDateTime::from_unix_timestamp_nanos(1_788_472_195_191_000_000).unwrap();
84        assert_eq!(format_timestamp(at), "2026-09-03T21:49:55.191Z");
85    }
86}