cedar_policy_core/from_normalized_str.rs
1/*
2 * Copyright Cedar Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::parser::err::{ParseError, ParseErrors, ToASTError, ToASTErrorKind};
18use crate::parser::Loc;
19use std::fmt::Display;
20use std::str::FromStr;
21
22/// Trait for parsing "normalized" strings only, throwing an error if a
23/// non-normalized string is encountered. See docs on the
24/// [`FromNormalizedStr::from_normalized_str`] trait function.
25pub trait FromNormalizedStr: FromStr<Err = ParseErrors> + Display {
26 /// Create a `Self` by parsing a string, which is required to be normalized.
27 /// That is, the input is required to roundtrip with the `Display` impl on `Self`:
28 /// `Self::from_normalized_str(x).to_string() == x` must hold.
29 ///
30 /// In Cedar's context, that means that `from_normalized_str()` will not
31 /// accept strings with spurious whitespace (e.g. `A :: B :: C::"foo"`),
32 /// Cedar comments (e.g. `A::B::"bar" // comment`), etc. See
33 /// [RFC 9](https://github.com/cedar-policy/rfcs/blob/main/text/0009-disallow-whitespace-in-entityuid.md)
34 /// for more details and justification.
35 ///
36 /// For the version that accepts whitespace and Cedar comments, use the
37 /// actual `FromStr` implementations.
38 fn from_normalized_str(s: &str) -> Result<Self, ParseErrors> {
39 default_from_normalized_str(s, Self::describe_self)
40 }
41
42 /// Short string description of the `Self` type, to be used in error messages.
43 /// What are we trying to parse?
44 fn describe_self() -> &'static str;
45}
46
47/// Default implementation of `from_normalized_str()`, which may be overridden
48/// for particular types if there is a more optimized implementation available.
49///
50/// See comments on [`FromNormalizedStr::from_normalized_str()`].
51pub fn default_from_normalized_str<T: FromStr<Err = ParseErrors> + Display>(
52 s: &str,
53 describe_self: impl FnOnce() -> &'static str,
54) -> Result<T, ParseErrors> {
55 let parsed = T::from_str(s)?;
56 let normalized_src = parsed.to_string();
57 if normalized_src == s {
58 // the normalized representation is indeed the one that was provided
59 Ok(parsed)
60 } else {
61 let diff_byte = s
62 .bytes()
63 .zip(normalized_src.bytes())
64 .enumerate()
65 .find(|(_, (b0, b1))| b0 != b1)
66 .map(|(idx, _)| idx)
67 .unwrap_or_else(|| s.len().min(normalized_src.len()));
68
69 Err(ParseErrors::singleton(ParseError::ToAST(ToASTError::new(
70 ToASTErrorKind::NonNormalizedString {
71 kind: describe_self(),
72 src: s.to_string(),
73 normalized_src,
74 },
75 Some(Loc::new(diff_byte, s.into())),
76 ))))
77 }
78}