citum_schema_data/reference/
date.rs1use crate::reference::types::RefDate;
7#[cfg(feature = "schema")]
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10use std::fmt;
11
12#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq)]
14#[cfg_attr(feature = "schema", derive(JsonSchema))]
15#[cfg_attr(feature = "bindings", derive(specta::Type))]
16pub struct EdtfString(pub String);
17
18impl EdtfString {
19 pub fn is_empty(&self) -> bool {
21 self.0.is_empty()
22 }
23
24 pub fn parse(&self) -> RefDate {
26 match self.0.parse::<citum_edtf::Edtf>() {
27 Ok(edtf) => RefDate::Edtf(edtf),
28 Err(_) => RefDate::Literal(self.0.clone()),
29 }
30 }
31
32 pub fn year(&self) -> String {
34 match self.parse() {
35 RefDate::Edtf(edtf) => edtf.year().to_string(),
36 RefDate::Literal(_) => String::new(),
37 }
38 }
39
40 pub fn day(&self) -> Option<u32> {
42 match self.parse() {
43 RefDate::Edtf(edtf) => edtf.day().filter(|&d| d > 0),
44 RefDate::Literal(_) => None,
45 }
46 }
47
48 pub fn is_uncertain(&self) -> bool {
50 self.0.contains('?')
51 }
52
53 pub fn is_approximate(&self) -> bool {
55 self.0.contains('~')
56 }
57
58 pub fn is_range(&self) -> bool {
60 matches!(self.parse(), RefDate::Edtf(edtf) if edtf.is_range())
61 }
62
63 pub fn is_open_range(&self) -> bool {
65 matches!(self.parse(), RefDate::Edtf(edtf) if edtf.is_open_range())
66 }
67
68 pub fn time(&self) -> Option<citum_edtf::Time> {
70 match self.parse() {
71 RefDate::Edtf(edtf) => edtf.time(),
72 _ => None,
73 }
74 }
75
76 pub fn has_time(&self) -> bool {
78 self.time().is_some()
79 }
80}
81
82impl fmt::Display for EdtfString {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 write!(f, "{}", self.0)
85 }
86}