Skip to main content

devpage_core/
icons.rs

1//! Icons by name, drawn as inline SVG in the text color: the Lucide set
2//! (`assets/vendor/lucide.json`, ISC) and the few brand marks Lucide doesn't
3//! carry, from Simple Icons (CC0).
4
5use std::collections::HashMap;
6use std::sync::LazyLock;
7
8/// One SVG element of an icon: its tag and its attributes.
9type Node = (String, Vec<(String, String)>);
10
11/// One element as Lucide's JSON writes it: `[tag, {attribute: value}]`.
12type RawNode = (String, serde_json::Map<String, serde_json::Value>);
13
14/// Lucide's icons, each a list of SVG elements.
15static LUCIDE: LazyLock<HashMap<String, Vec<Node>>> = LazyLock::new(|| {
16    let raw: HashMap<String, Vec<RawNode>> =
17        serde_json::from_str(include_str!("../assets/vendor/lucide.json")).unwrap_or_default();
18    raw.into_iter()
19        .map(|(name, nodes)| {
20            let nodes = nodes
21                .into_iter()
22                .map(|(tag, attributes)| {
23                    let attributes = attributes
24                        .into_iter()
25                        .map(|(key, value)| {
26                            let value = match value {
27                                serde_json::Value::String(text) => text,
28                                other => other.to_string(),
29                            };
30                            (key, value)
31                        })
32                        .collect();
33                    (tag, attributes)
34                })
35                .collect();
36            (name, nodes)
37        })
38        .collect()
39});
40
41/// Brand marks Lucide leaves out, as one filled path on a 24 × 24 grid.
42const BRANDS: &[(&str, &str)] = &[(
43    "github",
44    "M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12",
45)];
46
47/// Whether `name` is an icon devpage can draw.
48pub fn exists(name: &str) -> bool {
49    BRANDS.iter().any(|(brand, _)| *brand == name) || LUCIDE.contains_key(name)
50}
51
52/// The icon as inline SVG, sized by the text around it, or `None` when there
53/// is no such icon.
54pub fn svg(name: &str) -> Option<String> {
55    if let Some((_, path)) = BRANDS.iter().find(|(brand, _)| *brand == name) {
56        return Some(format!(
57            r#"<svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="{path}"/></svg>"#
58        ));
59    }
60    let nodes = LUCIDE.get(name)?;
61    let mut body = String::new();
62    for (tag, attributes) in nodes {
63        body.push('<');
64        body.push_str(tag);
65        for (key, value) in attributes {
66            body.push_str(&format!(r#" {key}="{}""#, value.replace('"', "&quot;")));
67        }
68        body.push_str("/>");
69    }
70    Some(format!(
71        r#"<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">{body}</svg>"#
72    ))
73}
74
75#[cfg(test)]
76mod tests {
77    use super::*;
78
79    #[test]
80    fn draws_lucide_icons_and_brand_marks_and_knows_unknown_names() {
81        let book = svg("book-open").unwrap();
82        assert!(book.starts_with("<svg class=\"icon\""), "{book}");
83        assert!(book.contains("<path d="), "{book}");
84        assert!(svg("github").unwrap().contains("fill=\"currentColor\""));
85        assert!(exists("presentation") && exists("github"));
86        assert!(!exists("no-such-icon"));
87        assert_eq!(svg("no-such-icon"), None);
88    }
89}