1#[cfg(test)]
4mod test;
5
6use std::{fmt, ops::Deref};
7
8use crate::{
9 json,
10 warning::{self, IntoCaveat},
11 Verdict,
12};
13
14#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
16pub enum Warning {
17 ContainsEscapeCodes,
19
20 ContainsNonPrintableASCII,
22
23 InvalidType { type_found: json::ValueKind },
25
26 InvalidLengthMax { length: usize },
28
29 InvalidLengthExact { length: usize },
31
32 PreferUppercase,
37}
38
39impl Warning {
40 fn invalid_type(elem: &json::Element<'_>) -> Self {
41 Self::InvalidType {
42 type_found: elem.value().kind(),
43 }
44 }
45}
46
47impl fmt::Display for Warning {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 match self {
50 Self::ContainsEscapeCodes => f.write_str("The string contains escape codes."),
51 Self::ContainsNonPrintableASCII => {
52 f.write_str("The string contains non-printable bytes.")
53 }
54 Self::InvalidType { type_found } => {
55 write!(f, "The value should be a string but is `{type_found}`")
56 }
57 Self::InvalidLengthMax { length } => {
58 write!(
59 f,
60 "The string is longer than the max length `{length}` defined in the spec.",
61 )
62 }
63 Self::InvalidLengthExact { length } => {
64 write!(f, "The string should be length `{length}`.")
65 }
66 Self::PreferUppercase => {
67 write!(f, "Upper case is preffered")
68 }
69 }
70 }
71}
72
73impl crate::Warning for Warning {
74 fn id(&self) -> warning::Id {
75 match self {
76 Self::ContainsEscapeCodes => warning::Id::from_static("contains_escape_codes"),
77 Self::ContainsNonPrintableASCII => {
78 warning::Id::from_static("contains_non_printable_ascii")
79 }
80 Self::InvalidType { .. } => warning::Id::from_static("invalid_type"),
81 Self::InvalidLengthMax { .. } => warning::Id::from_static("invalid_length_max"),
82 Self::InvalidLengthExact { .. } => warning::Id::from_static("invalid_length_exact"),
83 Self::PreferUppercase => warning::Id::from_static("prefer_upper_case"),
84 }
85 }
86}
87
88#[derive(Copy, Clone, Debug)]
96pub(crate) struct CiMaxLen<'buf, const MAX_LEN: usize>(&'buf str);
97
98impl<const MAX_LEN: usize> Deref for CiMaxLen<'_, MAX_LEN> {
99 type Target = str;
100
101 fn deref(&self) -> &Self::Target {
102 self.0
103 }
104}
105
106impl<const MAX_LEN: usize> fmt::Display for CiMaxLen<'_, MAX_LEN> {
107 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108 write!(f, "{}", self.0)
109 }
110}
111
112impl<'buf, const MAX_LEN: usize> json::FromJson<'buf> for CiMaxLen<'buf, MAX_LEN> {
113 type Warning = Warning;
114
115 fn from_json(elem: &json::Element<'buf>) -> Verdict<Self, Self::Warning> {
116 let (s, mut warnings) = Base::from_json(elem)?.into_parts();
117
118 if s.len() > MAX_LEN {
119 warnings.insert(Warning::InvalidLengthMax { length: MAX_LEN }, elem);
120 }
121
122 Ok(Self(s.0).into_caveat(warnings))
123 }
124}
125
126#[derive(Copy, Clone, Debug)]
134pub(crate) struct CiExactLen<'buf, const LEN: usize>(&'buf str);
135
136impl<const LEN: usize> Deref for CiExactLen<'_, LEN> {
137 type Target = str;
138
139 fn deref(&self) -> &Self::Target {
140 self.0
141 }
142}
143
144impl<const LEN: usize> fmt::Display for CiExactLen<'_, LEN> {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 write!(f, "{}", self.0)
147 }
148}
149
150impl<'buf, const LEN: usize> json::FromJson<'buf> for CiExactLen<'buf, LEN> {
151 type Warning = Warning;
152
153 fn from_json(elem: &json::Element<'buf>) -> Verdict<Self, Self::Warning> {
154 let (s, mut warnings) = Base::from_json(elem)?.into_parts();
155
156 if s.len() != LEN {
157 warnings.insert(Warning::InvalidLengthExact { length: LEN }, elem);
158 }
159
160 Ok(Self(s.0).into_caveat(warnings))
161 }
162}
163
164#[derive(Copy, Clone, Debug)]
169struct Base<'buf>(&'buf str);
170
171impl Deref for Base<'_> {
172 type Target = str;
173
174 fn deref(&self) -> &Self::Target {
175 self.0
176 }
177}
178
179impl fmt::Display for Base<'_> {
180 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
181 write!(f, "{}", self.0)
182 }
183}
184
185impl<'buf> json::FromJson<'buf> for Base<'buf> {
186 type Warning = Warning;
187
188 fn from_json(elem: &json::Element<'buf>) -> Verdict<Self, Self::Warning> {
189 let mut warnings = warning::Set::new();
190 let Some(id) = elem.to_raw_str() else {
191 return warnings.bail(Warning::invalid_type(elem), elem);
192 };
193
194 let s = id.has_escapes(elem).ignore_warnings();
197 let s = match s {
198 json::decode::PendingStr::NoEscapes(s) => {
199 if check_printable(s) {
200 warnings.insert(Warning::ContainsNonPrintableASCII, elem);
201 }
202 s
203 }
204 json::decode::PendingStr::HasEscapes(escape_str) => {
205 warnings.insert(Warning::ContainsEscapeCodes, elem);
206 let decoded = escape_str.decode_escapes(elem).ignore_warnings();
208
209 if check_printable(&decoded) {
210 warnings.insert(Warning::ContainsNonPrintableASCII, elem);
211 }
212
213 escape_str.into_raw()
214 }
215 };
216
217 Ok(Self(s).into_caveat(warnings))
218 }
219}
220
221fn check_printable(s: &str) -> bool {
222 s.chars()
223 .any(|c| c.is_ascii_whitespace() || c.is_ascii_control())
224}