Skip to main content

tempoch_core/format/
traits.rs

1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Vallés Puig, Ramon
3
4//! Core trait definitions for the format system (`FormatForScale`, etc.).
5
6use crate::earth::context::TimeContext;
7use crate::format::TimeFormat;
8use crate::foundation::error::ConversionError;
9use crate::foundation::sealed::Sealed;
10use crate::model::scale::Scale;
11use crate::model::time::Time;
12use qtty::Quantity;
13
14/// Witness that format `F` can encode and decode instants on scale `S`.
15#[allow(private_bounds)]
16pub trait FormatForScale<S: Scale>: TimeFormat + Sealed {
17    fn try_from_time<Fin: TimeFormat>(
18        time: Time<S, Fin>,
19        ctx: &TimeContext,
20    ) -> Result<Quantity<Self::Unit>, ConversionError>;
21    fn try_into_time(
22        raw: Quantity<Self::Unit>,
23        ctx: &TimeContext,
24    ) -> Result<Time<S, Self>, ConversionError>
25    where
26        Self: Sized;
27}
28
29/// Witness that format `F` can encode scale `S` without a [`TimeContext`].
30#[allow(private_bounds)]
31pub trait InfallibleFormatForScale<S: Scale>: FormatForScale<S> + Sealed {
32    fn from_time<Fin: TimeFormat>(time: Time<S, Fin>) -> Quantity<Self::Unit>;
33    fn into_time(raw: Quantity<Self::Unit>) -> Time<S, Self>;
34}