cedar_policy_core/entities/
err.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright Cedar Contributors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use super::EntityUID;
use crate::transitive_closure;
use miette::Diagnostic;
use thiserror::Error;

/// Errors in serializing, deserializing, and processing of Entities
#[derive(Debug, Diagnostic, Error)]
pub enum EntitiesError {
    /// Error occurring in serialization of entities
    #[error("error during entity serialization")]
    #[diagnostic(transparent)]
    Serialization(#[from] crate::entities::json::err::JsonSerializationError),
    /// Error occurring in deserialization of entities
    #[error("error during entity deserialization")]
    #[diagnostic(transparent)]
    Deserialization(#[from] crate::entities::json::err::JsonDeserializationError),
    /// Error constructing the Entities collection as there is a duplicate Entity UID
    #[error(transparent)]
    #[diagnostic(transparent)]
    Duplicate(Duplicate),
    /// Errors occurring while computing or enforcing transitive closure on the
    /// entity hierarchy.
    #[error("transitive closure computation/enforcement error")]
    #[diagnostic(transparent)]
    TransitiveClosureError(#[from] TransitiveClosureError),
    /// Error because an entity doesn't conform to the schema
    #[error("entity does not conform to the schema")]
    #[diagnostic(transparent)]
    InvalidEntity(#[from] crate::entities::conformance::err::EntitySchemaConformanceError),
}

impl EntitiesError {
    pub(crate) fn duplicate(euid: EntityUID) -> Self {
        Self::Duplicate(Duplicate { euid })
    }
}

impl From<transitive_closure::TcError<EntityUID>> for EntitiesError {
    fn from(value: transitive_closure::TcError<EntityUID>) -> Self {
        let tc: TransitiveClosureError = value.into();
        tc.into()
    }
}

#[derive(Debug, Error, Diagnostic)]
#[error(transparent)]
#[diagnostic(transparent)]
/// Errors occurring while computing or enforcing transitive closure on the
/// entity hierarchy.
pub struct TransitiveClosureError {
    err: Box<transitive_closure::TcError<EntityUID>>,
}

impl TransitiveClosureError {
    #[cfg(test)]
    pub(crate) fn inner(&self) -> &transitive_closure::TcError<EntityUID> {
        self.err.as_ref()
    }
}

impl From<transitive_closure::TcError<EntityUID>> for TransitiveClosureError {
    fn from(v: transitive_closure::TcError<EntityUID>) -> Self {
        Self { err: Box::new(v) }
    }
}

#[derive(Debug, PartialEq, Eq, Error, Diagnostic)]
#[error("duplicate entity entry `{}`", .euid)]
/// Error type for entity sets containing duplicate entity uids
pub struct Duplicate {
    /// The [`EntityUID`] that was duplicated
    euid: EntityUID,
}

impl Duplicate {
    #[cfg(test)]
    pub(crate) fn euid(&self) -> &EntityUID {
        &self.euid
    }
}

/// Type alias for convenience
pub type Result<T> = std::result::Result<T, EntitiesError>;