Skip to content

Commit 965a40e

Browse files
Generate types for each font awesome icon
1 parent d5a3784 commit 965a40e

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

crates/font-awesome-as-a-crate/build.rs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
collections::HashMap,
23
env,
34
fs::{read_dir, File},
45
io::{Read, Write},
@@ -11,13 +12,26 @@ fn main() {
1112
write_fontawesome_sprite();
1213
}
1314

15+
fn capitalize_first_letter(s: &str) -> String {
16+
let mut c = s.chars();
17+
match c.next() {
18+
None => String::new(),
19+
Some(f) => f.to_uppercase().chain(c).collect(),
20+
}
21+
}
22+
1423
fn write_fontawesome_sprite() {
24+
let mut types = HashMap::new();
1525
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("fontawesome.rs");
1626
let mut dest_file = File::create(dest_path).unwrap();
1727
dest_file
1828
.write_all(b"const fn fontawesome_svg(dir:&str,file:&str)->&'static str{match(dir.as_bytes(),file.as_bytes()){")
1929
.expect("fontawesome fn write");
20-
for dirname in &["brands", "regular", "solid"] {
30+
for (dirname, trait_name) in &[
31+
("brands", "Brands"),
32+
("regular", "Regular"),
33+
("solid", "Solid"),
34+
] {
2135
let dir = read_dir(Path::new("fontawesome-free-6.2.0-desktop/svgs").join(dirname)).unwrap();
2236
let mut data = String::new();
2337
for file in dir {
@@ -32,20 +46,43 @@ fn write_fontawesome_sprite() {
3246
.expect("fontawesome file read");
3347
// if this assert goes off, add more hashes here and in the format! below
3448
assert!(!data.contains("###"), "file {filename} breaks raw string");
49+
let filename = filename.replace(".svg", "");
3550
dest_file
3651
.write_all(
37-
format!(
38-
r####"(b"{dirname}",b"{filename}")=>r#"{data}"#,"####,
39-
data = data,
40-
dirname = dirname,
41-
filename = filename.replace(".svg", ""),
42-
)
43-
.as_bytes(),
52+
format!(r####"(b"{dirname}",b"{filename}")=>r#"{data}"#,"####).as_bytes(),
4453
)
4554
.expect("write fontawesome file");
55+
types
56+
.entry(filename)
57+
.or_insert_with(|| Vec::with_capacity(3))
58+
.push(trait_name);
4659
}
4760
}
4861
dest_file
49-
.write_all(b"_=>\"\"}}")
62+
.write_all(b"_=>\"\"}} pub mod icons { use super::{IconStr, Regular, Brands, Solid};")
5063
.expect("fontawesome fn write");
64+
65+
for (icon, kinds) in types {
66+
let mut type_name = "Icon".to_string();
67+
type_name.extend(icon.split('-').map(|s| capitalize_first_letter(s)));
68+
let kinds = kinds
69+
.iter()
70+
.map(|k| format!("impl {k} for {type_name} {{}}\n"))
71+
.collect::<String>();
72+
dest_file
73+
.write_all(
74+
format!(
75+
"\n#[derive(Debug, Clone, Copy, PartialEq, Eq)]
76+
pub struct {type_name};
77+
impl IconStr for {type_name} {{
78+
fn icon_str(&self) -> &'static str {{ r#\"{icon}\"# }}
79+
}}
80+
{kinds}"
81+
)
82+
.as_bytes(),
83+
)
84+
.expect("write fontawesome file types");
85+
}
86+
87+
dest_file.write_all(b"}").expect("fontawesome fn write");
5188
}

crates/font-awesome-as-a-crate/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,26 @@ pub const fn svg(type_: Type, name: &str) -> Result<&'static str, NameError> {
8787
Ok(svg)
8888
}
8989

90+
pub trait IconStr {
91+
fn icon_str(&self) -> &'static str;
92+
}
93+
94+
pub trait Brands: IconStr {
95+
fn get_type() -> Type {
96+
Type::Brands
97+
}
98+
}
99+
pub trait Regular: IconStr {
100+
fn get_type() -> Type {
101+
Type::Regular
102+
}
103+
}
104+
pub trait Solid: IconStr {
105+
fn get_type() -> Type {
106+
Type::Solid
107+
}
108+
}
109+
90110
#[cfg(test)]
91111
mod tests {
92112
const fn usable_as_const_() {

0 commit comments

Comments
 (0)