font_awesome_as_a_crate/
lib.rs1use std::error::Error;
9use std::fmt::{self, Display, Formatter};
10
11#[cfg(font_awesome_out_dir)]
12include!(concat!(env!("OUT_DIR"), "/fontawesome.rs"));
13#[cfg(not(font_awesome_out_dir))]
14include!("fontawesome.rs");
15
16#[derive(Clone, Copy, Eq, PartialEq, Debug)]
17pub enum Type {
18 Brands,
19 Regular,
20 Solid,
21}
22
23#[derive(Clone, Copy, Eq, PartialEq, Debug)]
24pub struct TypeError;
25
26impl Display for TypeError {
27 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
28 write!(
29 f,
30 "Invalid Font Awesome icon type: must be one of brands, regular, or solid"
31 )
32 }
33}
34
35impl Error for TypeError {}
36
37#[derive(Clone, Copy, Eq, PartialEq, Debug)]
38pub struct NameError;
39
40impl Display for NameError {
41 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
42 write!(
43 f,
44 "Invalid Font Awesome icon name: visit https://fontawesome.com/icons?d=gallery&m=free to see valid names"
45 )
46 }
47}
48
49impl Error for NameError {}
50
51impl Type {
52 pub const fn as_str(self) -> &'static str {
53 match self {
54 Type::Brands => "brands",
55 Type::Regular => "regular",
56 Type::Solid => "solid",
57 }
58 }
59}
60
61impl std::str::FromStr for Type {
62 type Err = TypeError;
63 fn from_str(s: &str) -> Result<Type, TypeError> {
64 match s {
65 "brands" => Ok(Type::Brands),
66 "regular" => Ok(Type::Regular),
67 "solid" => Ok(Type::Solid),
68 _ => Err(TypeError),
69 }
70 }
71}
72
73impl Display for Type {
74 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
75 Display::fmt(self.as_str(), f)
76 }
77}
78
79pub const fn svg(type_: Type, name: &str) -> Result<&'static str, NameError> {
83 let svg = fontawesome_svg(type_.as_str(), name);
84 if svg.is_empty() {
85 return Err(NameError);
86 }
87 Ok(svg)
88}
89
90#[cfg(test)]
91mod tests {
92 const fn usable_as_const_() {
93 assert!(crate::svg(crate::Type::Solid, "cog").is_ok());
94 }
95 #[test]
96 fn usable_as_const() {
97 usable_as_const_();
98 }
99 #[test]
100 fn it_works() {
101 assert!(crate::svg(crate::Type::Solid, "cog").is_ok());
102 assert!(crate::svg(crate::Type::Solid, "snuffleupigus").is_err());
103 }
104}