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
use std::fmt;
/// Indicates a resource that did not exist in `Resources`.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ResourceFetchError {
/// Short type name of the `CmdBlock::Input` type.
pub resource_name_short: String,
/// Full type name of the `CmdBlock::Input` type.
pub resource_name_full: String,
}
impl ResourceFetchError {
/// Returns a new `ResourceFetchError` with the given type's name.
pub fn new<R>() -> Self {
let resource_name_short = tynm::type_name::<R>();
let resource_name_full = std::any::type_name::<R>().to_string();
Self {
resource_name_short,
resource_name_full,
}
}
/// Returns the short type name, e.g. `String`
///
/// For generic types, this returns the short type names for the type and
/// the parameter, e.g. `Option<String>`.
pub fn resource_name_short(&self) -> &str {
self.resource_name_short.as_ref()
}
/// Returns the full type name, e.g. `std::string::String`
///
/// For generic types, this returns the short type names for the type and
/// the parameter, e.g. `std::option::Option<std::string::String>`.
pub fn resource_name_full(&self) -> &str {
self.resource_name_full.as_ref()
}
}
impl fmt::Display for ResourceFetchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self {
resource_name_short,
resource_name_full: _,
} = self;
write!(
f,
"Failed to fetch `{resource_name_short}` from `resources`."
)
}
}
impl std::error::Error for ResourceFetchError {}