cedar_policy_core/entities/
err.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 super::EntityUID;
18use crate::transitive_closure;
19use miette::Diagnostic;
20use thiserror::Error;
21
22/// Errors in serializing, deserializing, and processing of Entities
23#[derive(Debug, Diagnostic, Error)]
24pub enum EntitiesError {
25    /// Error occurring in serialization of entities
26    #[error("error during entity serialization")]
27    #[diagnostic(transparent)]
28    Serialization(#[from] crate::entities::json::err::JsonSerializationError),
29    /// Error occurring in deserialization of entities
30    #[error("error during entity deserialization")]
31    #[diagnostic(transparent)]
32    Deserialization(#[from] crate::entities::json::err::JsonDeserializationError),
33    /// Error constructing the Entities collection as there is a duplicate Entity UID
34    #[error(transparent)]
35    #[diagnostic(transparent)]
36    Duplicate(Duplicate),
37    /// Errors occurring while computing or enforcing transitive closure on the
38    /// entity hierarchy.
39    #[error("transitive closure computation/enforcement error")]
40    #[diagnostic(transparent)]
41    TransitiveClosureError(#[from] TransitiveClosureError),
42    /// Error because an entity doesn't conform to the schema
43    #[error("entity does not conform to the schema")]
44    #[diagnostic(transparent)]
45    InvalidEntity(#[from] crate::entities::conformance::err::EntitySchemaConformanceError),
46}
47
48impl EntitiesError {
49    pub(crate) fn duplicate(euid: EntityUID) -> Self {
50        Self::Duplicate(Duplicate { euid })
51    }
52}
53
54impl From<transitive_closure::TcError<EntityUID>> for EntitiesError {
55    fn from(value: transitive_closure::TcError<EntityUID>) -> Self {
56        let tc: TransitiveClosureError = value.into();
57        tc.into()
58    }
59}
60
61#[derive(Debug, Error, Diagnostic)]
62#[error(transparent)]
63#[diagnostic(transparent)]
64/// Errors occurring while computing or enforcing transitive closure on the
65/// entity hierarchy.
66pub struct TransitiveClosureError {
67    err: Box<transitive_closure::TcError<EntityUID>>,
68}
69
70impl TransitiveClosureError {
71    #[cfg(test)]
72    pub(crate) fn inner(&self) -> &transitive_closure::TcError<EntityUID> {
73        self.err.as_ref()
74    }
75}
76
77impl From<transitive_closure::TcError<EntityUID>> for TransitiveClosureError {
78    fn from(v: transitive_closure::TcError<EntityUID>) -> Self {
79        Self { err: Box::new(v) }
80    }
81}
82
83#[derive(Debug, PartialEq, Eq, Error, Diagnostic)]
84#[error("duplicate entity entry `{}`", .euid)]
85/// Error type for entity sets containing duplicate entity uids
86pub struct Duplicate {
87    /// The [`EntityUID`] that was duplicated
88    euid: EntityUID,
89}
90
91impl Duplicate {
92    #[cfg(test)]
93    pub(crate) fn euid(&self) -> &EntityUID {
94        &self.euid
95    }
96}
97
98/// Type alias for convenience
99pub type Result<T> = std::result::Result<T, EntitiesError>;