cedar_policy/api/err/
validation_errors.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 `ValidationError` without qualifying it with
24// `crate`, but not used otherwise, so non-doc builds warned about unused
25// imports.
26#[cfg(doc)]
27use crate::ValidationError;
28
29// Generates a public struct wrapping a core validator error 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 `ValidationError` variant in `err.rs`
32// (`cargo doc` will fail otherwise). This macro generates a basic doc-string
33// linking back to the `ValidationError` enum variant where the primary
34// documentation should be written.
35macro_rules! wrap_core_error {
36    ($s:ident) => {
37        #[derive(Debug, Clone, Error, Diagnostic)]
38        #[error(transparent)]
39        #[diagnostic(transparent)]
40        #[doc=concat!("Structure containing details about a [`ValidationError::", stringify!($s), "`].")]
41        pub struct $s(cedar_policy_validator::validation_errors::$s);
42
43        impl $s {
44            /// Access the `[PolicyId]` for the policy where this error 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_errors::$s> for $s {
52            fn from(e: cedar_policy_validator::validation_errors::$s) -> Self {
53                Self(e)
54            }
55        }
56    };
57}
58
59wrap_core_error!(UnrecognizedEntityType);
60wrap_core_error!(UnrecognizedActionId);
61wrap_core_error!(InvalidActionApplication);
62wrap_core_error!(UnexpectedType);
63wrap_core_error!(IncompatibleTypes);
64wrap_core_error!(UnsafeAttributeAccess);
65wrap_core_error!(UnsafeOptionalAttributeAccess);
66wrap_core_error!(UnsafeTagAccess);
67wrap_core_error!(NoTagsAllowed);
68wrap_core_error!(UndefinedFunction);
69wrap_core_error!(WrongNumberArguments);
70wrap_core_error!(FunctionArgumentValidation);
71wrap_core_error!(HierarchyNotRespected);
72wrap_core_error!(EntityDerefLevelViolation);
73wrap_core_error!(EmptySetForbidden);
74wrap_core_error!(NonLitExtConstructor);
75wrap_core_error!(InternalInvariantViolation);