Skip to main content

soml/
jiff.rs

1/*!
2Conversions between jiff's types and SOML's, and modules for `#[serde(with)]`, with the `jiff` feature.
3
4jiff's `Timestamp` and `SignedDuration` read SOML instants and durations without this, because they ask for text. But they write themselves as strings, so a document gets a quoted `'2026-09-19T14:00:00Z'` instead of an instant. A `civil::Date` needs its module in both directions, because SOML keeps a date distinct from a string. The modules here write native values:
5
6```rust
7#[derive(serde::Serialize, serde::Deserialize)]
8struct Deploy {
9	#[serde(with = "soml::jiff::timestamp")]
10	at: jiff::Timestamp,
11	#[serde(with = "soml::jiff::signed_duration")]
12	took: jiff::SignedDuration,
13	#[serde(with = "soml::jiff::timestamp::option", default)]
14	finished: Option<jiff::Timestamp>,
15}
16
17let deploy: Deploy = soml::from_str("at: 2026-09-19T14:00:00Z\ntook: 1m30s")?;
18assert_eq!(soml::to_string(&deploy)?, "at: 2026-09-19T14:00:00Z\ntook: 1m30s\nfinished: null\n");
19# Ok::<(), soml::Error>(())
20```
21
22jiff's timestamps end at `9999-12-30T22:00:00.999999999Z`, about a day before SOML's, so a later instant cannot be converted.
23*/
24
25use crate::{Date, Duration, Error, Instant};
26use ::jiff::{SignedDuration, Timestamp};
27
28impl TryFrom<Timestamp> for Instant {
29	type Error = Error;
30
31	/**
32	Fails when the timestamp is outside the years 0001 to 9999.
33	*/
34	fn try_from(timestamp: Timestamp) -> Result<Self, Error> {
35		Self::from_unix_nanoseconds(timestamp.as_nanosecond()).ok_or_else(|| {
36			Error::data(format!(
37				"The timestamp {timestamp} is outside the years 0001 to 9999"
38			))
39		})
40	}
41}
42
43impl TryFrom<Instant> for Timestamp {
44	type Error = Error;
45
46	/**
47	Fails for an instant after `9999-12-30T22:00:00.999999999Z`, where jiff's range ends.
48	*/
49	fn try_from(instant: Instant) -> Result<Self, Error> {
50		// jiff adds the nanoseconds to the seconds whatever their signs, as `Instant` does, and nanoseconds below one billion always fit an `i32`.
51		Self::new(instant.unix_seconds(), instant.nanoseconds() as i32).map_err(|error| {
52			Error::data(format!(
53				"The instant {instant} is outside the range of a jiff timestamp: {error}"
54			))
55		})
56	}
57}
58
59impl TryFrom<SignedDuration> for Duration {
60	type Error = Error;
61
62	/**
63	Fails when the duration is longer than `i64::MAX` nanoseconds, about 292 years either way.
64	*/
65	fn try_from(duration: SignedDuration) -> Result<Self, Error> {
66		i64::try_from(duration.as_nanos())
67			.map(Self::from_nanoseconds)
68			.map_err(|_| {
69				Error::data(format!(
70					"The duration {duration:?} is outside the SOML range of about 292 years"
71				))
72			})
73	}
74}
75
76impl From<Duration> for SignedDuration {
77	fn from(duration: Duration) -> Self {
78		Self::from_nanos(duration.nanoseconds())
79	}
80}
81
82serde_with_module! {
83	/**
84	For a `jiff::Timestamp` field: `#[serde(with = "soml::jiff::timestamp")]`.
85	*/
86	timestamp, ::jiff::Timestamp, crate::Instant
87}
88
89serde_with_module! {
90	/**
91	For a `jiff::SignedDuration` field: `#[serde(with = "soml::jiff::signed_duration")]`.
92	*/
93	signed_duration, ::jiff::SignedDuration, crate::Duration
94}
95
96impl TryFrom<::jiff::civil::Date> for Date {
97	type Error = Error;
98
99	/**
100	Fails when the native date's year is outside 1 through 9999. Components are converted directly, without a time or timezone.
101	*/
102	fn try_from(date: ::jiff::civil::Date) -> Result<Self, Error> {
103		u16::try_from(date.year())
104			.ok()
105			.and_then(|year| Self::new(year, date.month() as u8, date.day() as u8))
106			.ok_or_else(|| {
107				Error::data(format!("The date {date} is outside the years 0001 to 9999"))
108			})
109	}
110}
111
112impl From<Date> for ::jiff::civil::Date {
113	fn from(date: Date) -> Self {
114		Self::new(date.year() as i16, date.month() as i8, date.day() as i8)
115			.expect("the native Gregorian calendar holds every SOML date")
116	}
117}
118
119serde_with_module! {
120	/**
121	For a `::jiff::civil::Date` field: `#[serde(with = "soml::jiff::date")]`. Preserves the native date type and components in both directions.
122	*/
123	date, ::jiff::civil::Date, crate::Date
124}