sdml_errors/
errors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*!
Provides the project-wide Error and Result types as well as helper functions.
 */

use crate::diagnostics::Diagnostic;
use std::fmt::{Debug, Display};
use tracing::error;

// ------------------------------------------------------------------------------------------------
// Public Types  Error and Result
// ------------------------------------------------------------------------------------------------

///
/// The Error type for this crate.
///
#[derive(Debug)]
pub enum Error {
    /// An error was signaled by the standard library I/O functions.
    IoError {
        source: std::io::Error,
    },
    /// An error was signaled by the standard string conversion functions.
    Utf8Error {
        source: core::str::Utf8Error,
    },
    /// An error was signaled by the standard string conversion functions.
    FromUtf8Error {
        source: std::string::FromUtf8Error,
    },
    /// An error was signaled while parsing a `Url`. Note that the methods `from_file_path` and
    /// `from_directory_path` return `()` on error.
    UrlParseError {
        source: Option<url::ParseError>,
    },
    /// An error was signaled while parsing a tracing filter expression.
    TracingFilterError {
        source: tracing_subscriber::filter::ParseError,
    },
    /// An error was signaled while initializing a tracing subscriber..
    TracingSubscriberError {
        source: tracing::subscriber::SetGlobalDefaultError,
    },
    CodespanReportingError {
        source: codespan_reporting::files::Error,
    },
    /// This allows for a complete `Diagnostic` structure to be passed as an Error.
    LanguageValidationError {
        source: Diagnostic,
    },
    LanguageTagError {
        source: language_tags::ParseError,
    },
    #[cfg(feature = "templates")]
    Template {
        source: tera::Error,
    },
    GeneratorError {
        name: String,
        message: String,
    },
}

///
/// A Result type that specifically uses this crate's Error.
///
pub type Result<T> = std::result::Result<T, Error>;

// ------------------------------------------------------------------------------------------------
// Private Macros
// ------------------------------------------------------------------------------------------------

macro_rules! report_and_return {
    ($err: expr) => {
        let err = $err;
        error!("{}", err);
        return err;
    };
}

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Self::IoError { source } => format!("An I/O error occurred; source: {source}"),
                Self::Utf8Error { source } =>
                    format!("A UTF-8 conversion error occurred; source: {source}"),
                Self::FromUtf8Error { source } =>
                    format!("A UTF-8 conversion error occurred; source: {source}"),
                Self::UrlParseError { source } =>
                    if let Some(source) = source {
                        format!("An error occurred parsing a URL; source: {source}")
                    } else {
                        "An error occurred creating a URL from a file path".to_string()
                    },
                Self::TracingFilterError { source } =>
                    format!("A error occurred parsing a tracing filter; source: {source}"),
                Self::TracingSubscriberError { source } =>
                    format!("A error occurred setting the tracing subscriber; source: {source}"),
                Self::CodespanReportingError { source } =>
                    format!("An error occurred formatting codespan reports; source: {source}"),
                Self::LanguageValidationError { source } =>
                    format!("Validation diagnostic: {}", source.message),
                Self::LanguageTagError { source } =>
                    format!("An error occurred parsing a BCP-47 language tag; source: {source}"),
                Self::GeneratorError { name, message } =>
                    format!("An error occurred in a generator named `{name}`: {message}"),
                #[cfg(feature = "templates")]
                Self::Template { source } =>
                    format!("An error occurred in the template generator; source: {source}"),
            }
        )
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        #[allow(unreachable_patterns)]
        match self {
            Error::IoError { source } => Some(source),
            Error::Utf8Error { source } => Some(source),
            Self::FromUtf8Error { source } => Some(source),
            Self::TracingFilterError { source } => Some(source),
            Self::TracingSubscriberError { source } => Some(source),
            Self::CodespanReportingError { source } => Some(source),
            #[cfg(feature = "templates")]
            Self::Template { source } => Some(source),
            _ => None,
        }
    }
}

impl<T> From<Error> for Result<T> {
    fn from(value: Error) -> Self {
        Err(value)
    }
}

impl From<std::io::Error> for Error {
    fn from(source: std::io::Error) -> Self {
        report_and_return! {
            Error::IoError { source }
        }
    }
}

impl From<core::str::Utf8Error> for Error {
    fn from(source: core::str::Utf8Error) -> Self {
        report_and_return! {
            Error::Utf8Error { source }
        }
    }
}

impl From<std::string::FromUtf8Error> for Error {
    fn from(source: std::string::FromUtf8Error) -> Self {
        report_and_return! {
            Error::FromUtf8Error { source }
        }
    }
}

impl From<url::ParseError> for Error {
    fn from(source: url::ParseError) -> Self {
        report_and_return! {
            Error::UrlParseError { source: Some(source) }
        }
    }
}

impl From<tracing_subscriber::filter::ParseError> for Error {
    fn from(source: tracing_subscriber::filter::ParseError) -> Self {
        report_and_return! {
            Error::TracingFilterError { source }
        }
    }
}

impl From<tracing::subscriber::SetGlobalDefaultError> for Error {
    fn from(source: tracing::subscriber::SetGlobalDefaultError) -> Self {
        report_and_return! {
            Error::TracingSubscriberError { source }
        }
    }
}

impl From<codespan_reporting::files::Error> for Error {
    fn from(source: codespan_reporting::files::Error) -> Self {
        report_and_return! {
            Error::CodespanReportingError { source }
        }
    }
}

impl From<language_tags::ParseError> for Error {
    fn from(source: language_tags::ParseError) -> Self {
        report_and_return! {
            Error::LanguageTagError { source }
        }
    }
}

impl From<Diagnostic> for Error {
    fn from(source: Diagnostic) -> Self {
        report_and_return! {
            Self::LanguageValidationError { source }
        }
    }
}

#[cfg(feature = "templates")]
impl From<tera::Error> for Error {
    fn from(source: tera::Error) -> Self {
        report_and_return! {
            Self::Template { source }
        }
    }
}