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: {0}")]
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 due to encountering two different entities
34    /// with the same Entity UID
35    #[error(transparent)]
36    #[diagnostic(transparent)]
37    Duplicate(Duplicate),
38    /// Errors occurring while computing or enforcing transitive closure on the
39    /// entity hierarchy.
40    #[error("transitive closure computation/enforcement error")]
41    #[diagnostic(transparent)]
42    TransitiveClosureError(#[from] TransitiveClosureError),
43    /// Error because an entity doesn't conform to the schema
44    #[error("entity does not conform to the schema")]
45    #[diagnostic(transparent)]
46    InvalidEntity(#[from] crate::entities::conformance::err::EntitySchemaConformanceError),
47}
48
49impl EntitiesError {
50    pub(crate) fn duplicate(euid: EntityUID) -> Self {
51        Self::Duplicate(Duplicate { euid })
52    }
53}
54
55impl From<transitive_closure::TcError<EntityUID>> for EntitiesError {
56    fn from(value: transitive_closure::TcError<EntityUID>) -> Self {
57        let tc: TransitiveClosureError = value.into();
58        tc.into()
59    }
60}
61
62#[derive(Debug, Error, Diagnostic)]
63#[error(transparent)]
64#[diagnostic(transparent)]
65/// Errors occurring while computing or enforcing transitive closure on the
66/// entity hierarchy.
67pub struct TransitiveClosureError {
68    err: Box<transitive_closure::TcError<EntityUID>>,
69}
70
71#[cfg(test)]
72impl TransitiveClosureError {
73    pub(crate) fn inner(&self) -> &transitive_closure::TcError<EntityUID> {
74        self.err.as_ref()
75    }
76}
77
78impl From<transitive_closure::TcError<EntityUID>> for TransitiveClosureError {
79    fn from(v: transitive_closure::TcError<EntityUID>) -> Self {
80        Self { err: Box::new(v) }
81    }
82}
83
84#[derive(Debug, PartialEq, Eq, Error, Diagnostic)]
85#[error("duplicate entity entry `{}`", .euid)]
86/// Error type for entity sets containing duplicate entity uids
87pub struct Duplicate {
88    /// The [`EntityUID`] that was duplicated
89    pub(crate) euid: EntityUID,
90}
91
92#[cfg(test)]
93impl Duplicate {
94    pub(crate) fn euid(&self) -> &EntityUID {
95        &self.euid
96    }
97}
98
99/// Type alias for convenience
100pub type Result<T> = std::result::Result<T, EntitiesError>;