cedar_policy/api/err/validation_warnings.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 miette::Diagnostic;
18use ref_cast::RefCast;
19use thiserror::Error;
20
21use crate::PolicyId;
22
23// Required for doc link to `ValidationWarning` without qualifying it with
24// `crate`, but not used otherwise, so non-doc builds warned about unused
25// imports.
26#[cfg(doc)]
27use crate::ValidationWarning;
28
29// Generates a public struct wrapping a core validator warning struct. The core
30// and external struct will have exactly the same name. This name _must_ be the
31// same as the name of the corresponding `ValidationWarning` variant in `err.rs`
32// (`cargo doc` will fail otherwise). This macro generates a basic doc-string
33// linking back to the `ValidationWarning` enum variant where the primary
34// documentation should be written.
35macro_rules! wrap_core_warning {
36 ($s:ident) => {
37 #[derive(Debug, Clone, Error, Diagnostic)]
38 #[error(transparent)]
39 #[diagnostic(transparent)]
40 #[doc=concat!("Structure containing details about a [`ValidationWarning::", stringify!($s), "`].")]
41 pub struct $s(cedar_policy_validator::validation_warnings::$s);
42
43 impl $s {
44 /// Access the [`PolicyId`] for the policy where this warning was found.
45 pub fn policy_id(&self) -> &PolicyId {
46 PolicyId::ref_cast(&self.0.policy_id)
47 }
48 }
49
50 #[doc(hidden)]
51 impl From<cedar_policy_validator::validation_warnings::$s> for $s {
52 fn from(e: cedar_policy_validator::validation_warnings::$s) -> Self {
53 Self(e)
54 }
55 }
56 };
57}
58
59wrap_core_warning!(MixedScriptString);
60wrap_core_warning!(BidiCharsInString);
61wrap_core_warning!(BidiCharsInIdentifier);
62wrap_core_warning!(MixedScriptIdentifier);
63wrap_core_warning!(ConfusableIdentifier);
64wrap_core_warning!(ImpossiblePolicy);