Skip to main content

prov_views/
error.rs

1//! What can go wrong executing a view.
2
3use std::fmt;
4
5/// The result of executing a view.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// A view could not be executed.
9#[derive(Debug)]
10pub enum Error {
11    /// Reading the workspace failed.
12    Graph(prov_graph::error::Error),
13    /// The view's `under:` names nothing this workspace can resolve.
14    ///
15    /// Deliberately not folded into an empty result — see
16    /// [`select`](fn@crate::select).
17    AnchorUnresolved {
18        /// The view that declared it.
19        view: String,
20        /// The anchor exactly as written.
21        under: String,
22        /// Why it did not resolve, in a sentence a user can act on.
23        why: String,
24    },
25}
26
27impl fmt::Display for Error {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            Error::Graph(e) => write!(f, "{e}"),
31            Error::AnchorUnresolved { view, under, why } => write!(
32                f,
33                "the view `{view}` is anchored under `{under}`, but {why}"
34            ),
35        }
36    }
37}
38
39impl std::error::Error for Error {
40    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
41        match self {
42            Error::Graph(e) => Some(e),
43            Error::AnchorUnresolved { .. } => None,
44        }
45    }
46}
47
48impl From<prov_graph::error::Error> for Error {
49    fn from(error: prov_graph::error::Error) -> Self {
50        Error::Graph(error)
51    }
52}