Skip to main content

ocpi_tariffs/
string.rs

1//! Case Insensitive String. Only printable ASCII allowed.
2
3use std::{fmt, ops::Deref};
4
5use crate::{
6    json,
7    warning::{self, IntoCaveat},
8    Caveat, Verdict,
9};
10
11/// The warnings that can happen when parsing a case-insensitive string.
12#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
13pub enum Warning {
14    /// There should be no escape codes in a `CiString`.
15    ContainsEscapeCodes,
16
17    /// There should only be printable ASCII bytes in a `CiString`.
18    ContainsNonPrintableASCII,
19
20    /// The JSON value given is not a string.
21    InvalidType,
22
23    /// The length of the string exceeds the specs constraint.
24    InvalidLengthMax { length: usize },
25
26    /// The length of the string is not equal to the specs constraint.
27    InvalidLengthExact { length: usize },
28
29    /// The casing of the string is not common practice.
30    ///
31    /// Note: This is not enforced by the string types in this module, but can be used
32    /// by linting code to signal that the casing of a given string is unorthodox.
33    PreferUppercase,
34}
35
36impl fmt::Display for Warning {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            Self::ContainsEscapeCodes => f.write_str("The string contains escape codes."),
40            Self::ContainsNonPrintableASCII => {
41                f.write_str("The string contains non-printable bytes.")
42            }
43            Self::InvalidType => f.write_str("The value should be a string."),
44            Self::InvalidLengthMax { length } => {
45                write!(
46                    f,
47                    "The string is longer than the max length `{length}` defined in the spec.",
48                )
49            }
50            Self::InvalidLengthExact { length } => {
51                write!(f, "The string should be length `{length}`.")
52            }
53            Self::PreferUppercase => {
54                write!(f, "Upper case is preffered")
55            }
56        }
57    }
58}
59
60impl crate::Warning for Warning {
61    fn id(&self) -> crate::SmartString {
62        match self {
63            Self::ContainsEscapeCodes => "contains_escape_codes".into(),
64            Self::ContainsNonPrintableASCII => "contains_non_printable_ascii".into(),
65            Self::InvalidType => "invalid_type".into(),
66            Self::InvalidLengthMax { .. } => "invalid_length_max".into(),
67            Self::InvalidLengthExact { .. } => "invalid_length_exact".into(),
68            Self::PreferUppercase => "prefer_upper_case".into(),
69        }
70    }
71}
72
73/// String that can have `[0..=MAX_LEN]` bytes.
74///
75/// Only printable ASCII allowed. Non-printable characters like: Carriage returns, Tabs, Line breaks, etc. are not allowed.
76/// Case insensitivity is not enforced.
77///
78/// See: <https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/types.asciidoc#11-cistring-type>
79/// See: <https://github.com/ocpi/ocpi/blob/release-2.1.1-bugfixes/types.md#11-cistring-type>
80#[derive(Copy, Clone, Debug)]
81pub(crate) struct CiMaxLen<'buf, const MAX_LEN: usize>(&'buf str);
82
83impl<const MAX_LEN: usize> Deref for CiMaxLen<'_, MAX_LEN> {
84    type Target = str;
85
86    fn deref(&self) -> &Self::Target {
87        self.0
88    }
89}
90
91impl<const MAX_LEN: usize> fmt::Display for CiMaxLen<'_, MAX_LEN> {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        write!(f, "{}", self.0)
94    }
95}
96
97impl<const MAX_LEN: usize> IntoCaveat for CiMaxLen<'_, MAX_LEN> {
98    fn into_caveat<W: crate::Warning>(self, warnings: warning::Set<W>) -> Caveat<Self, W> {
99        Caveat::new(self, warnings)
100    }
101}
102
103impl<'buf, 'elem: 'buf, const MAX_LEN: usize> json::FromJson<'elem, 'buf>
104    for CiMaxLen<'buf, MAX_LEN>
105{
106    type Warning = Warning;
107
108    fn from_json(elem: &'elem json::Element<'buf>) -> Verdict<Self, Self::Warning> {
109        let (s, mut warnings) = Base::from_json(elem)?.into_parts();
110
111        if s.len() > MAX_LEN {
112            warnings.with_elem(Warning::InvalidLengthMax { length: MAX_LEN }, elem);
113        }
114
115        Ok(Self(s.0).into_caveat(warnings))
116    }
117}
118
119/// String that can have `LEN` bytes exactly.
120///
121/// Only printable ASCII allowed. Non-printable characters like: Carriage returns, Tabs, Line breaks, etc. are not allowed.
122/// Case insensitivity is not enforced.
123///
124/// See: <https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/types.asciidoc#11-cistring-type>
125/// See: <https://github.com/ocpi/ocpi/blob/release-2.1.1-bugfixes/types.md#11-cistring-type>
126#[derive(Copy, Clone, Debug)]
127pub(crate) struct CiExactLen<'buf, const LEN: usize>(&'buf str);
128
129impl<const LEN: usize> Deref for CiExactLen<'_, LEN> {
130    type Target = str;
131
132    fn deref(&self) -> &Self::Target {
133        self.0
134    }
135}
136
137impl<const LEN: usize> fmt::Display for CiExactLen<'_, LEN> {
138    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139        write!(f, "{}", self.0)
140    }
141}
142
143impl<const LEN: usize> IntoCaveat for CiExactLen<'_, LEN> {
144    fn into_caveat<W: crate::Warning>(self, warnings: warning::Set<W>) -> Caveat<Self, W> {
145        Caveat::new(self, warnings)
146    }
147}
148
149impl<'buf, 'elem: 'buf, const LEN: usize> json::FromJson<'elem, 'buf> for CiExactLen<'buf, LEN> {
150    type Warning = Warning;
151
152    fn from_json(elem: &'elem json::Element<'buf>) -> Verdict<Self, Self::Warning> {
153        let (s, mut warnings) = Base::from_json(elem)?.into_parts();
154
155        if s.len() != LEN {
156            warnings.with_elem(Warning::InvalidLengthExact { length: LEN }, elem);
157        }
158
159        Ok(Self(s.0).into_caveat(warnings))
160    }
161}
162
163/// Case Insensitive String. Only printable ASCII allowed. (Non-printable characters like: Carriage returns, Tabs, Line breaks, etc. are not allowed)
164///
165/// See: <https://github.com/ocpi/ocpi/blob/release-2.2.1-bugfixes/types.asciidoc#11-cistring-type>
166/// See: <https://github.com/ocpi/ocpi/blob/release-2.1.1-bugfixes/types.md#11-cistring-type>
167#[derive(Copy, Clone, Debug)]
168struct Base<'buf>(&'buf str);
169
170impl Deref for Base<'_> {
171    type Target = str;
172
173    fn deref(&self) -> &Self::Target {
174        self.0
175    }
176}
177
178impl fmt::Display for Base<'_> {
179    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180        write!(f, "{}", self.0)
181    }
182}
183
184impl IntoCaveat for Base<'_> {
185    fn into_caveat<W: crate::Warning>(self, warnings: warning::Set<W>) -> Caveat<Self, W> {
186        Caveat::new(self, warnings)
187    }
188}
189
190impl<'buf, 'elem: 'buf> json::FromJson<'elem, 'buf> for Base<'buf> {
191    type Warning = Warning;
192
193    fn from_json(elem: &'elem json::Element<'buf>) -> Verdict<Self, Self::Warning> {
194        let mut warnings = warning::Set::new();
195        let Some(id) = elem.as_raw_str() else {
196            return warnings.bail(Warning::InvalidType, elem);
197        };
198
199        // We don't care about the details of any warnings the escapes in the Id may have.
200        // The Id should simply not have any escapes.
201        let s = id.has_escapes(elem).ignore_warnings();
202        let s = match s {
203            json::decode::PendingStr::NoEscapes(s) => {
204                if check_printable(s) {
205                    warnings.with_elem(Warning::ContainsNonPrintableASCII, elem);
206                }
207                s
208            }
209            json::decode::PendingStr::HasEscapes(escape_str) => {
210                warnings.with_elem(Warning::ContainsEscapeCodes, elem);
211                // We decode the escapes to check if any of the escapes result in non-printable ASCII.
212                let decoded = escape_str.decode_escapes(elem).ignore_warnings();
213
214                if check_printable(&decoded) {
215                    warnings.with_elem(Warning::ContainsNonPrintableASCII, elem);
216                }
217
218                escape_str.into_raw()
219            }
220        };
221
222        Ok(Self(s).into_caveat(warnings))
223    }
224}
225
226fn check_printable(s: &str) -> bool {
227    s.chars()
228        .any(|c| c.is_ascii_whitespace() || c.is_ascii_control())
229}
230
231#[cfg(test)]
232mod test {
233    #![allow(
234        clippy::indexing_slicing,
235        reason = "unwraps are allowed anywhere in tests"
236    )]
237
238    use assert_matches::assert_matches;
239
240    use crate::{
241        json::{self, FromJson},
242        warning,
243    };
244
245    use super::{CiExactLen, CiMaxLen, Warning};
246
247    const LEN: usize = 3;
248
249    #[test]
250    fn should_parse_max_len() {
251        let input = "hel";
252        let (output, warnings) = test_max_len(input);
253        assert!(warnings.is_empty(), "{warnings:#?}");
254        assert_eq!(output, input);
255    }
256
257    #[test]
258    fn should_fail_on_max_len() {
259        let input = "hello";
260        let (output, warnings) = test_max_len(input);
261        let warnings = warnings.into_path_map();
262        let warnings = &warnings["$"];
263        let length = assert_matches!(
264            warnings.as_slice(),
265            [Warning::InvalidLengthMax { length }] => *length
266        );
267        assert_eq!(length, LEN);
268        assert_eq!(output, input);
269    }
270
271    #[test]
272    fn should_parse_exact_len() {
273        let input = "hel";
274        let (output, warnings) = test_expect_len(input);
275        assert!(warnings.is_empty(), "{warnings:#?}");
276        assert_eq!(output, input);
277    }
278
279    #[test]
280    fn should_fail_on_exact_len() {
281        let input = "hello";
282        let (output, warnings) = test_expect_len(input);
283        let warnings = warnings.into_path_map();
284        let warnings = &warnings["$"];
285        let length = assert_matches!(
286            warnings.as_slice(),
287            [Warning::InvalidLengthExact { length }] => *length
288        );
289        assert_eq!(length, LEN);
290        assert_eq!(output, input);
291    }
292
293    #[track_caller]
294    fn test_max_len(s: &str) -> (String, warning::Set<Warning>) {
295        let quoted_input = format!(r#""{s}""#);
296        let elem = json::parse(&quoted_input).unwrap();
297        let output = CiMaxLen::<'_, LEN>::from_json(&elem).unwrap();
298        let (output, warnings) = output.into_parts();
299        (output.to_string(), warnings)
300    }
301
302    #[track_caller]
303    fn test_expect_len(s: &str) -> (String, warning::Set<Warning>) {
304        let quoted_input = format!(r#""{s}""#);
305        let elem = json::parse(&quoted_input).unwrap();
306        let output = CiExactLen::<'_, LEN>::from_json(&elem).unwrap();
307        let (output, warnings) = output.into_parts();
308        (output.to_string(), warnings)
309    }
310}