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
use std::error::Error;
use std::fmt::{self, Display, Formatter};
#[cfg(font_awesome_out_dir)]
include!(concat!(env!("OUT_DIR"), "/fontawesome.rs"));
#[cfg(not(font_awesome_out_dir))]
include!("fontawesome.rs");
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub enum Type {
Brands,
Regular,
Solid,
}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub struct TypeError;
impl Display for TypeError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"Invalid Font Awesome icon type: must be one of brands, regular, or solid"
)
}
}
impl Error for TypeError {}
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
pub struct NameError;
impl Display for NameError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"Invalid Font Awesome icon name: visit https://fontawesome.com/icons?d=gallery&m=free to see valid names"
)
}
}
impl Error for NameError {}
impl Type {
pub const fn as_str(self) -> &'static str {
match self {
Type::Brands => "brands",
Type::Regular => "regular",
Type::Solid => "solid",
}
}
}
impl std::str::FromStr for Type {
type Err = TypeError;
fn from_str(s: &str) -> Result<Type, TypeError> {
match s {
"brands" => Ok(Type::Brands),
"regular" => Ok(Type::Regular),
"solid" => Ok(Type::Solid),
_ => Err(TypeError),
}
}
}
impl Display for Type {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(self.as_str(), f)
}
}
pub const fn svg(type_: Type, name: &str) -> Result<&'static str, NameError> {
let svg = fontawesome_svg(type_.as_str(), name);
if svg.is_empty() {
return Err(NameError);
}
Ok(svg)
}
#[cfg(test)]
mod tests {
const fn usable_as_const_() {
assert!(crate::svg(crate::Type::Solid, "cog").is_ok());
}
#[test]
fn usable_as_const() {
usable_as_const_();
}
#[test]
fn it_works() {
assert!(crate::svg(crate::Type::Solid, "cog").is_ok());
assert!(crate::svg(crate::Type::Solid, "snuffleupigus").is_err());
}
}