Skip to main content

prefixmap/
error.rs

1use crate::PrefixMap;
2use iri_s::error::IriSError;
3use thiserror::Error;
4
5/// Errors that can occur when working with [`PrefixMap`]
6#[derive(Debug, Error, Clone)]
7pub enum PrefixMapError {
8    /// Error originating from the [`iri_s`] crate
9    #[error(transparent)]
10    IriSError(#[from] IriSError),
11
12    /// Error indicating that a requested prefix alias was not found in the [`PrefixMap`]
13    #[error("Alias '{prefix}' not found in prefix map\nAvailable aliases: [{}]", prefixmap.aliases().cloned().collect::<Vec<_>>().join(", "))]
14    PrefixNotFound { prefix: String, prefixmap: PrefixMap },
15
16    /// Error indicating a formatting issue
17    #[error(transparent)]
18    FormatError(#[from] std::fmt::Error),
19
20    /// Error indicating an IO issue
21    #[error("IO Error: {error}")]
22    IOError { error: String },
23
24    /// Error indicating that an alias already exists in the [`PrefixMap`]
25    #[error("Alias '{prefix}' already exists in prefix map with value '{value}'")]
26    AliasAlreadyExists { prefix: String, value: String },
27}
28
29/// Error returned when trying to get an IRI from a prefixed name [`IriRef`]
30#[derive(Debug, Error, Clone)]
31#[error("Cannot obtain IRI from prefixed name IriRef {prefix}:{local}")]
32pub struct IriRefError {
33    pub prefix: String,
34    pub local: String,
35}
36
37/// Represents all the possible errors that can occur when dereferencing IRIs
38#[derive(Debug, Error, Clone)]
39pub enum DerefError {
40    /// An error originating from the [`iri_s`] crate
41    #[error(transparent)]
42    IriSError(#[from] IriSError),
43
44    /// An error originating when obtaining an IRI from a [`PrefixMap`]
45    ///
46    /// ## Fields
47    /// - `error`: The underlying [`PrefixMapError`]
48    #[error("Error obtaining IRI for '{alias}:{local}': {error}")]
49    DerefPrefixMapError {
50        alias: String,
51        local: String,
52        error: Box<PrefixMapError>,
53    },
54
55    /// An error occured when trying to dereference a prefixed name without a prefix map
56    #[error("No prefix map to dereference prefixed name {prefix}{local}")]
57    NoPrefixMapPrefixedName { prefix: String, local: String },
58
59    /// An error occured when trying to obtain an IRI from an [`IriRef`]
60    #[error(transparent)]
61    UnderefError(#[from] IriRefError),
62}