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
use std::fmt;
/// Stable category of a Rust highlighter failure.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ErrorKind {
/// An asset could not be read from the selected provider.
AssetIo,
/// An asset's binary format or version is invalid.
AssetFormat,
/// An asset does not match its release-pinned digest or byte size.
AssetIntegrity,
/// The selected offline source does not contain the requested asset.
AssetUnavailable,
/// Downloading an asset from the remote mirror failed.
AssetDownload,
/// A custom grammar or theme registration is invalid.
InvalidRegistration,
/// No grammar is registered for the requested language.
UnknownLanguage,
/// No theme is registered for the requested theme name.
UnknownTheme,
/// A TextMate grammar could not tokenize the input.
Tokenization,
/// A TextMate theme could not be resolved.
Theme,
/// An internal invariant failed.
Internal,
}
/// An owned error with a stable category and human-readable context.
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
message: String,
source: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl Error {
/// Creates an error with a category and contextual message.
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
source: None,
}
}
/// Retains a cause without exposing an implementation-specific error type.
#[must_use]
pub fn with_source(mut self, source: impl std::error::Error + Send + Sync + 'static) -> Self {
self.source = Some(Box::new(source));
self
}
/// Returns the category for fallback policy decisions.
pub fn kind(&self) -> ErrorKind {
self.kind
}
pub(crate) fn from_reason(message: impl Into<String>) -> Self {
Self::new(ErrorKind::Internal, message)
}
}
impl fmt::Display for Error {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.message)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source.as_ref().map(|source| source.as_ref() as _)
}
}
/// Result returned by the native Ferriki runtime.
pub type Result<T> = std::result::Result<T, Error>;