use crate::reference::types::RefDate;
#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Deserialize, Serialize, Clone, Default, PartialEq)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[cfg_attr(feature = "bindings", derive(specta::Type))]
pub struct EdtfString(pub String);
impl EdtfString {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn parse(&self) -> RefDate {
let mut input = self.0.as_str();
match citum_edtf::parse(&mut input) {
Ok(edtf) => RefDate::Edtf(edtf),
Err(_) => RefDate::Literal(self.0.clone()),
}
}
pub fn year(&self) -> String {
match self.parse() {
RefDate::Edtf(edtf) => edtf.year().to_string(),
RefDate::Literal(_) => String::new(),
}
}
pub fn day(&self) -> Option<u32> {
match self.parse() {
RefDate::Edtf(edtf) => edtf.day().filter(|&d| d > 0),
RefDate::Literal(_) => None,
}
}
pub fn is_uncertain(&self) -> bool {
self.0.contains('?')
}
pub fn is_approximate(&self) -> bool {
self.0.contains('~')
}
pub fn is_range(&self) -> bool {
matches!(self.parse(), RefDate::Edtf(edtf) if edtf.is_range())
}
pub fn is_open_range(&self) -> bool {
matches!(self.parse(), RefDate::Edtf(edtf) if edtf.is_open_range())
}
pub fn time(&self) -> Option<citum_edtf::Time> {
match self.parse() {
RefDate::Edtf(edtf) => edtf.time(),
_ => None,
}
}
pub fn has_time(&self) -> bool {
self.time().is_some()
}
}
impl fmt::Display for EdtfString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}