Skip to main content

wist_shared/
primitives.rs

1//! 跨域共用的原始类型(模型标量 DateTime/Secret 的单一真源)。
2
3pub type Int = i64;
4pub type Bool = bool;
5pub type Float = f64;
6
7#[derive(
8    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, ::serde::Serialize, ::serde::Deserialize,
9)]
10pub struct DateTime(chrono::DateTime<chrono::Utc>);
11
12impl DateTime {
13    pub fn now() -> Self {
14        Self(chrono::Utc::now())
15    }
16    /// `days` 天后的时刻(如注册 Token 有效期)。
17    pub fn in_days(days: i64) -> Self {
18        Self(
19            chrono::Utc::now()
20                .checked_add_signed(chrono::Duration::days(days))
21                .unwrap_or_else(chrono::Utc::now),
22        )
23    }
24    pub fn from_rfc3339(value: &str) -> Option<Self> {
25        chrono::DateTime::parse_from_rfc3339(value)
26            .ok()
27            .map(|v| Self(v.with_timezone(&chrono::Utc)))
28    }
29    /// 取底层 chrono 值(如 sqlx TIMESTAMPTZ 绑定)。
30    pub fn to_chrono(&self) -> chrono::DateTime<chrono::Utc> {
31        self.0
32    }
33    pub fn seconds_until(&self, later: &Self) -> i64 {
34        later.0.signed_duration_since(self.0).num_seconds().max(0)
35    }
36}
37
38#[derive(Clone, ::serde::Serialize, ::serde::Deserialize)]
39pub struct Secret(String);
40
41impl std::fmt::Debug for Secret {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        f.write_str("Secret(***)")
44    }
45}