iri_string/types/generic/
error.rs

1//! Resource identifier creation error.
2
3use core::fmt;
4
5#[cfg(feature = "std")]
6use std::error;
7
8use crate::validate::Error;
9
10/// Error on conversion into an IRI type.
11///
12/// Enabled by `alloc` or `std` feature.
13// This type itself does not require `alloc` or `std, but the type is used only when `alloc`
14// feature is enabled. To avoid exporting unused stuff, the type (and the `types::generic::error`
15// module) is available only when necessary.
16//
17// Note that all types which implement `Spec` also implement `SpecInternal`.
18pub struct CreationError<T> {
19    /// Soruce data.
20    source: T,
21    /// Validation error.
22    error: Error,
23}
24
25impl<T> CreationError<T> {
26    /// Returns the source data.
27    #[must_use]
28    pub fn into_source(self) -> T {
29        self.source
30    }
31
32    /// Returns the validation error.
33    #[must_use]
34    pub fn validation_error(&self) -> Error {
35        self.error
36    }
37
38    /// Creates a new `CreationError`.
39    #[must_use]
40    pub(crate) fn new(error: Error, source: T) -> Self {
41        Self { source, error }
42    }
43}
44
45impl<T: fmt::Debug> fmt::Debug for CreationError<T> {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        f.debug_struct("CreationError")
48            .field("source", &self.source)
49            .field("error", &self.error)
50            .finish()
51    }
52}
53
54impl<T: Clone> Clone for CreationError<T> {
55    fn clone(&self) -> Self {
56        Self {
57            source: self.source.clone(),
58            error: self.error,
59        }
60    }
61}
62
63impl<T> fmt::Display for CreationError<T> {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        self.error.fmt(f)
66    }
67}
68
69#[cfg(feature = "std")]
70impl<T: fmt::Debug> error::Error for CreationError<T> {}