Skip to main content

substrate_data/schema/reader/
resolve.rs

1//! [`Resolve`]: what a tool makes of the references it reads, and
2//! [`Unresolved`], why one is wrong.
3
4use std::fmt;
5
6use crate::model::value::{Path, Ref, RefKey};
7
8/// What a tool makes of the references it reads: a build checks each one
9/// against what there is (and may rewrite its id), an editor shows them.
10pub trait Resolve {
11    /// A reference at `path`: the id to keep, or why it's wrong.
12    ///
13    /// # Errors
14    ///
15    /// [`Unresolved`] when the reference names nothing there is, or names
16    /// it in a way the tool refuses.
17    fn reference(&mut self, r: &Ref, path: &Path) -> Result<RefKey, Unresolved>;
18}
19
20/// Why a reference doesn't resolve.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum Unresolved {
23    /// There is nothing it names.
24    Missing(Ref),
25    /// It names something, but not in a way this tool takes (a reason for a person).
26    Refused {
27        /// The reference.
28        reference: Ref,
29        /// Why, for a person.
30        reason: String,
31    },
32}
33
34impl fmt::Display for Unresolved {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        match self {
37            Unresolved::Missing(r) => write!(f, "`{r}`: there is no such {}", r.kind),
38            Unresolved::Refused { reference, reason } => write!(f, "`{reference}`: {reason}"),
39        }
40    }
41}
42
43impl std::error::Error for Unresolved {}