use tabnas::{Text, Value};
pub const OFFSET_DATE_TIME: &str = "offset-date-time";
pub const LOCAL_DATE_TIME: &str = "local-date-time";
pub const LOCAL_DATE: &str = "local-date";
pub const LOCAL_TIME: &str = "local-time";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TomlTime {
pub kind: String,
pub src: String,
}
impl TomlTime {
pub fn new(kind: impl Into<String>, src: impl Into<String>) -> Self {
Self {
kind: kind.into(),
src: src.into(),
}
}
pub fn to_value(&self) -> Value {
Value::Text(Text {
quote: self.kind.clone(),
string: self.src.clone(),
})
}
}
pub fn toml_time(value: &Value) -> Option<TomlTime> {
let Value::Text(text) = value else {
return None;
};
matches!(
text.quote.as_str(),
OFFSET_DATE_TIME | LOCAL_DATE_TIME | LOCAL_DATE | LOCAL_TIME
)
.then(|| TomlTime::new(text.quote.clone(), text.string.clone()))
}
pub fn isodate_val(groups: &[String]) -> Value {
isodate_time(groups).to_value()
}
pub fn isodate_time(groups: &[String]) -> TomlTime {
let offset = groups.get(4).is_some_and(|group| !group.is_empty());
let timed = groups.get(1).is_some_and(|group| !group.is_empty());
let kind = match (offset, timed) {
(true, true) => OFFSET_DATE_TIME,
(true, false) => "offset-date",
(false, true) => LOCAL_DATE_TIME,
(false, false) => LOCAL_DATE,
};
TomlTime::new(kind, groups.first().cloned().unwrap_or_default())
}
pub fn localtime_val(groups: &[String]) -> Value {
localtime_time(groups).to_value()
}
pub fn localtime_time(groups: &[String]) -> TomlTime {
TomlTime::new(LOCAL_TIME, groups.first().cloned().unwrap_or_default())
}