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, 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 { .. } => warning::Id::from_static("invalid_type"),
93 }
94 }
95}
96
97impl From<json::decode::Warning> for Warning {
98 fn from(warn_kind: json::decode::Warning) -> Self {
99 Self::Decode(warn_kind)
100 }
101}
102
103impl json::FromJson<'_> for DateTime<Utc> {
104 type Warning = Warning;
105
106 fn from_json(elem: &json::Element<'_>) -> Verdict<Self, Self::Warning> {
107 let mut warnings = warning::Set::new();
108 let Some(s) = elem.to_raw_str() else {
109 return warnings.bail(Warning::invalid_type(elem), elem);
110 };
111
112 let pending_str = s.has_escapes(elem).gather_warnings_into(&mut warnings);
113
114 let s = match pending_str {
115 json::decode::PendingStr::NoEscapes(s) => s,
116 json::decode::PendingStr::HasEscapes(_) => {
117 return warnings.bail(Warning::ContainsEscapeCodes, elem);
118 }
119 };
120
121 let err = match s.parse::<DateTime<Utc>>() {
123 Ok(date) => return Ok(date.into_caveat(warnings)),
124 Err(err) => err,
125 };
126
127 let Ok(date) = s.parse::<NaiveDateTime>() else {
128 return warnings.bail(Warning::Invalid(err.to_string()), elem);
129 };
130
131 let datetime = Utc.from_utc_datetime(&date);
132 Ok(datetime.into_caveat(warnings))
133 }
134}
135
136impl json::FromJson<'_> for chrono::NaiveDate {
137 type Warning = Warning;
138
139 fn from_json(elem: &json::Element<'_>) -> Verdict<Self, Self::Warning> {
140 let mut warnings = warning::Set::new();
141 let Some(s) = elem.to_raw_str() else {
142 return warnings.bail(Warning::invalid_type(elem), elem);
143 };
144
145 let pending_str = s.has_escapes(elem).gather_warnings_into(&mut warnings);
146
147 let s = match pending_str {
148 json::decode::PendingStr::NoEscapes(s) => s,
149 json::decode::PendingStr::HasEscapes(_) => {
150 return warnings.bail(Warning::ContainsEscapeCodes, elem);
151 }
152 };
153
154 let date = match s.parse::<chrono::NaiveDate>() {
155 Ok(v) => v,
156 Err(err) => {
157 return warnings.bail(Warning::Invalid(err.to_string()), elem);
158 }
159 };
160
161 Ok(date.into_caveat(warnings))
162 }
163}
164
165impl json::FromJson<'_> for chrono::NaiveTime {
166 type Warning = Warning;
167
168 fn from_json(elem: &json::Element<'_>) -> Verdict<Self, Self::Warning> {
169 let mut warnings = warning::Set::new();
170 let value = elem.as_value();
171
172 let Some(s) = value.to_raw_str() else {
173 return warnings.bail(Warning::invalid_type(elem), elem);
174 };
175
176 let pending_str = s.has_escapes(elem).gather_warnings_into(&mut warnings);
177
178 let s = match pending_str {
179 json::decode::PendingStr::NoEscapes(s) => s,
180 json::decode::PendingStr::HasEscapes(_) => {
181 return warnings.bail(Warning::ContainsEscapeCodes, elem);
182 }
183 };
184
185 let date = match chrono::NaiveTime::parse_from_str(s, "%H:%M") {
186 Ok(v) => v,
187 Err(err) => {
188 return warnings.bail(Warning::Invalid(err.to_string()), elem);
189 }
190 };
191
192 Ok(date.into_caveat(warnings))
193 }
194}