Skip to main content

cedar_policy_core/transitive_closure/
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 miette::Diagnostic;
18use std::fmt::Debug;
19use std::fmt::Display;
20use thiserror::Error;
21
22/// Error type for errors raised during transitive closure computation. This
23/// error type is parametrized by a type `K` which is the type of a unique
24/// identifier for graph nodes and the type returned by `get_key` on the
25/// `TCNode` trait.
26#[derive(Debug, Diagnostic, Error, PartialEq, Eq)]
27pub enum TcError<K: Debug + Display> {
28    /// Error raised when `TCComputation::EnforceAlreadyComputed` finds that the
29    /// TC was in fact not already computed
30    #[error("expected all transitive edges to exist, but `{child}` -> `{parent}` and `{parent}` -> `{grandparent}` exists, while `{child}` -> `{grandparent}` does not")]
31    MissingTcEdge {
32        /// Child entity at fault
33        child: K,
34        /// Parent entity at fault
35        parent: K,
36        /// Grandparent entity at fault. This is a parent of `parent` but not an
37        /// ancestor of `child`.
38        grandparent: K,
39    },
40    /// Error raised when enforce_dag finds that the graph is not a DAG
41    #[error("input graph has a cycle containing vertex `{}`", .vertex_with_loop)]
42    HasCycle {
43        /// Because DAG enforcement can only be called after compute_tc/enforce_tc, a cycle will manifest as a vertex with a loop
44        vertex_with_loop: K,
45    },
46}
47
48/// Type alias for convenience
49pub type Result<T, K> = std::result::Result<T, TcError<K>>;