use nautilus_common::log_warn;
use nautilus_core::UnixNanos;
const MIN_PLAUSIBLE_UNIX_NANOS: u64 = 10_000_000_000_000_000;
const fn unix_nanos_scale_is_plausible(ts: UnixNanos) -> bool {
ts.as_u64() >= MIN_PLAUSIBLE_UNIX_NANOS
}
pub(super) fn warn_if_implausible_unix_nanos(kind: &str, ts_event: UnixNanos, ts_init: UnixNanos) {
warn_if_implausible_named(kind, "ts_event", ts_event);
warn_if_implausible_named(kind, "ts_init", ts_init);
}
pub(super) fn warn_if_implausible_optional(kind: &str, field: &str, ts: Option<UnixNanos>) {
if let Some(ts) = ts {
warn_if_implausible_named(kind, field, ts);
}
}
fn warn_if_implausible_named(kind: &str, field: &str, ts: UnixNanos) {
if !unix_nanos_scale_is_plausible(ts) {
log_warn!(
"Implausible Unix-nanosecond scale for {kind} {field}={ts}; value looks like leftover seconds, milliseconds, or microseconds"
);
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
#[case(0, false)]
#[case(1_770_000_000, false)]
#[case(1_770_000_000_000, false)]
#[case(1_770_000_000_000_000, false)]
#[case(MIN_PLAUSIBLE_UNIX_NANOS - 1, false)]
#[case(MIN_PLAUSIBLE_UNIX_NANOS, true)]
#[case(1_770_000_000_000_000_000, true)]
fn test_unix_nanos_scale_is_plausible(#[case] raw: u64, #[case] expected: bool) {
assert_eq!(unix_nanos_scale_is_plausible(UnixNanos::new(raw)), expected);
}
}