1#[cfg(test)]
17pub(crate) mod test;
18
19#[cfg(test)]
20mod test_datetime_from_json;
21
22use std::fmt;
23
24use chrono::{DateTime, NaiveDateTime, TimeZone as _, Utc};
25
26use crate::{
27 json,
28 warning::{self, GatherWarnings as _},
29 IntoCaveat as _, Verdict,
30};
31
32#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
34pub enum Warning {
35 ContainsEscapeCodes,
37
38 Decode(json::decode::Warning),
40
41 Invalid(String),
58
59 InvalidType { type_found: json::ValueKind },
61}
62
63impl Warning {
64 fn invalid_type(elem: &json::Element<'_>) -> Self {
65 Self::InvalidType {
66 type_found: elem.value().kind(),
67 }
68 }
69}
70
71impl fmt::Display for Warning {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 match self {
74 Self::ContainsEscapeCodes => {
75 f.write_str("The value contains escape codes but it does not need them.")
76 }
77 Self::Decode(warning) => fmt::Display::fmt(warning, f),
78 Self::Invalid(err) => write!(f, "The value is not valid: {err}"),
79 Self::InvalidType { type_found } => {
80 write!(f, "The value should be a string but found `{type_found}`.")
81 }
82 }
83 }
84}
85
86impl crate::Warning for Warning {
87 fn id(&self) -> warning::Id {
88 match self {
89 Self::ContainsEscapeCodes => warning::Id::from_static("contains_escape_codes"),
90 Self::Decode(kind) => kind.id(),
91 Self::Invalid(_) => warning::Id::from_static("invalid"),
92 Self::InvalidType { type_found } => {
93 warning::Id::from_string(format!("invalid_type({type_found})"))
94 }
95 }
96 }
97}
98
99impl From<json::decode::Warning> for Warning {
100 fn from(warn_kind: json::decode::Warning) -> Self {
101 Self::Decode(warn_kind)
102 }
103}
104
105impl json::FromJson<'_> for DateTime<Utc> {
106 type Warning = Warning;
107
108 fn from_json(elem: &json::Element<'_>) -> Verdict<Self, Self::Warning> {
109 let mut warnings = warning::Set::new();
110 let Some(s) = elem.to_raw_str() else {
111 return warnings.bail(Warning::invalid_type(elem), elem);
112 };
113
114 let pending_str = s.has_escapes(elem).gather_warnings_into(&mut warnings);
115
116 let s = match pending_str {
117 json::decode::PendingStr::NoEscapes(s) => s,
118 json::decode::PendingStr::HasEscapes(_) => {
119 return warnings.bail(Warning::ContainsEscapeCodes, elem);
120 }
121 };
122
123 let err = match s.parse::<DateTime<Utc>>() {
125 Ok(date) => return Ok(date.into_caveat(warnings)),
126 Err(err) => err,
127 };
128
129 let Ok(date) = s.parse::<NaiveDateTime>() else {
130 return warnings.bail(Warning::Invalid(err.to_string()), elem);
131 };
132
133 let datetime = Utc.from_utc_datetime(&date);
134 Ok(datetime.into_caveat(warnings))
135 }
136}
137
138impl json::FromJson<'_> for chrono::NaiveDate {
139 type Warning = Warning;
140
141 fn from_json(elem: &json::Element<'_>) -> Verdict<Self, Self::Warning> {
142 let mut warnings = warning::Set::new();
143 let Some(s) = elem.to_raw_str() else {
144 return warnings.bail(Warning::invalid_type(elem), elem);
145 };
146
147 let pending_str = s.has_escapes(elem).gather_warnings_into(&mut warnings);
148
149 let s = match pending_str {
150 json::decode::PendingStr::NoEscapes(s) => s,
151 json::decode::PendingStr::HasEscapes(_) => {
152 return warnings.bail(Warning::ContainsEscapeCodes, elem);
153 }
154 };
155
156 let date = match s.parse::<chrono::NaiveDate>() {
157 Ok(v) => v,
158 Err(err) => {
159 return warnings.bail(Warning::Invalid(err.to_string()), elem);
160 }
161 };
162
163 Ok(date.into_caveat(warnings))
164 }
165}
166
167impl json::FromJson<'_> for chrono::NaiveTime {
168 type Warning = Warning;
169
170 fn from_json(elem: &json::Element<'_>) -> Verdict<Self, Self::Warning> {
171 let mut warnings = warning::Set::new();
172 let value = elem.as_value();
173
174 let Some(s) = value.to_raw_str() else {
175 return warnings.bail(Warning::invalid_type(elem), elem);
176 };
177
178 let pending_str = s.has_escapes(elem).gather_warnings_into(&mut warnings);
179
180 let s = match pending_str {
181 json::decode::PendingStr::NoEscapes(s) => s,
182 json::decode::PendingStr::HasEscapes(_) => {
183 return warnings.bail(Warning::ContainsEscapeCodes, elem);
184 }
185 };
186
187 let date = match chrono::NaiveTime::parse_from_str(s, "%H:%M") {
188 Ok(v) => v,
189 Err(err) => {
190 return warnings.bail(Warning::Invalid(err.to_string()), elem);
191 }
192 };
193
194 Ok(date.into_caveat(warnings))
195 }
196}