iconify 0.3.1

Proc-macros for generating icons from the Iconify API
Documentation
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use core::fmt;
use std::{env, str::FromStr};

use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::parse::{Parse, ParseStream};

use crate::attrs::{get_lit_bool, get_lit_str};

trait AppendQueryPair {
    fn append_query_pair(&mut self, key: &str, value: &Option<String>);
}

impl AppendQueryPair for url::form_urlencoded::Serializer<'_, url::UrlQuery<'_>> {
    fn append_query_pair(&mut self, key: &str, value: &Option<String>) {
        if let Some(value) = value {
            self.append_pair(key, &value.to_string());
        }
    }
}

enum IconifyRotation {
    Rotate90,
    Rotate180,
    Rotate270,
}

impl fmt::Display for IconifyRotation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IconifyRotation::Rotate90 => write!(f, "90deg"),
            IconifyRotation::Rotate180 => write!(f, "180deg"),
            IconifyRotation::Rotate270 => write!(f, "270deg"),
        }
    }
}

impl FromStr for IconifyRotation {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "90" => Ok(IconifyRotation::Rotate90),
            "180" => Ok(IconifyRotation::Rotate180),
            "270" => Ok(IconifyRotation::Rotate270),
            _ => Err(()),
        }
    }
}

enum IconifyFlip {
    Horizontal,
    Vertical,
    Both,
}

impl fmt::Display for IconifyFlip {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IconifyFlip::Horizontal => write!(f, "horizontal"),
            IconifyFlip::Vertical => write!(f, "vertical"),
            IconifyFlip::Both => write!(f, "horizontal,vertical"),
        }
    }
}

impl FromStr for IconifyFlip {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "horizontal" => Ok(IconifyFlip::Horizontal),
            "vertical" => Ok(IconifyFlip::Vertical),
            "both" | "horizontal,vertical" | "vertical,horizontal" => Ok(IconifyFlip::Both),
            _ => Err(()),
        }
    }
}

struct IconifyInput {
    pack: String,
    name: String,
    color: Option<String>,
    width: Option<String>,
    height: Option<String>,
    flip: Option<IconifyFlip>,
    rotate: Option<IconifyRotation>,
    view_box: bool,
}

impl IconifyInput {
    fn icon_url(&self) -> Result<String, url::ParseError> {
        let mut url = url::Url::parse(&iconify_url())?;

        // Set the pack and icon name in the url path.
        {
            let mut path_segments = url
                .path_segments_mut()
                .map_err(|_| url::ParseError::RelativeUrlWithoutBase)?;

            path_segments.push(&self.pack);
            path_segments.push(&format!("{}.svg", &self.name));
        }

        // Set the query parameters.
        {
            let mut query_pairs = url.query_pairs_mut();

            query_pairs.append_query_pair("color", &self.color);
            query_pairs.append_query_pair("width", &self.width);
            query_pairs.append_query_pair("height", &self.height);
            query_pairs.append_query_pair("flip", &self.flip.as_ref().map(IconifyFlip::to_string));
            query_pairs.append_query_pair(
                "rotate",
                &self.rotate.as_ref().map(IconifyRotation::to_string),
            );
            query_pairs.append_query_pair(
                "box",
                &self.view_box.then(|| Some("true".to_string())).flatten(),
            );
        }

        Ok(url.to_string())
    }

    #[cfg(all(not(test), feature = "cache"))]
    fn hash_digest(&self) -> Result<String, syn::Error> {
        use hex::ToHex;

        let mut buf = [0u8; 8];
        let url = self.icon_url().map_err(|err| {
            syn::Error::new(Span::call_site(), format!("failed to parse url: {err}"))
        })?;

        blake3::Hasher::new()
            .update(url.as_bytes())
            .finalize_xof()
            .fill(&mut buf);

        Ok(buf.encode_hex::<String>())
    }
}

impl Parse for IconifyInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let pack_name_lit = input.parse::<syn::LitStr>()?;
        let pack_name_string = pack_name_lit.value();

        let mut pack_name = pack_name_string.split(':');

        let error = || syn::Error::new(pack_name_lit.span(), "expected `pack_name:icon_name`");
        let pack = pack_name.next().ok_or_else(error)?.to_string();
        let name = pack_name.next().ok_or_else(error)?.to_string();

        if pack_name.next().is_some() {
            return Err(error());
        }

        let mut color = None;
        let mut width = None;
        let mut height = None;
        let mut flip = None;
        let mut rotate = None;
        let mut view_box = false;

        if input.peek(syn::Token![,]) {
            input.parse::<syn::Token![,]>()?;
            let metas = input.parse_terminated(syn::Meta::parse, syn::Token![,])?;

            for meta in metas {
                use syn::Meta::NameValue;
                match meta {
                    // Parse syn!("...", color = ...)].
                    NameValue(m) if m.path.is_ident("color") => {
                        let value = get_lit_str("color", &m.value)?;
                        color = Some(value.value());
                    }
                    // Parse syn!("...", width = ...)].
                    NameValue(m) if m.path.is_ident("width") => {
                        let value = get_lit_str("width", &m.value)?;
                        width = Some(value.value());
                    }
                    // Parse syn!("...", height = ...)].
                    NameValue(m) if m.path.is_ident("height") => {
                        let value = get_lit_str("height", &m.value)?;
                        height = Some(value.value());
                    }
                    // Parse syn!("...", flip = ...)].
                    NameValue(m) if m.path.is_ident("flip") => {
                        let value = get_lit_str("flip", &m.value)?;
                        let flip_val = IconifyFlip::from_str(&value.value())
                            .map_err(|_| syn::Error::new(value.span(), "Invalid flip value"))?;
                        flip = Some(flip_val);
                    }
                    // Parse syn!("...", rotate = ...)].
                    NameValue(m) if m.path.is_ident("rotate") => {
                        let value = get_lit_str("rotate", &m.value)?;
                        let rotate_val = IconifyRotation::from_str(&value.value()).map_err(|_| {
                            syn::Error::new_spanned(
                            value,
                            "Invalid rotate value. Rotate can be one of \"90\", \"180\", or \"270\".",
                        )
                        })?;
                        rotate = Some(rotate_val);
                    }
                    // Parse syn!("...", view_box = ...)].
                    NameValue(m) if m.path.is_ident("view_box") => {
                        view_box = get_lit_bool("view_box", &m.value)?;
                    }
                    _ => {
                        return Err(syn::Error::new_spanned(
                            meta,
                            "Not a name value pair: `foo = \"...\"`",
                        ));
                    }
                }
            }
        }

        Ok(Self {
            pack,
            name,
            color,
            width,
            height,
            flip,
            rotate,
            view_box,
        })
    }
}

fn iconify_url() -> String {
    env::var("ICONIFY_URL").unwrap_or("https://api.iconify.design".to_string())
}

#[cfg(all(not(test), feature = "cache"))]
fn iconify_cache_dir() -> std::path::PathBuf {
    use directories::BaseDirs;
    use std::path::PathBuf;

    if let Ok(dir) = env::var("ICONIFY_CACHE_DIR") {
        return PathBuf::from(dir);
    }

    let dir = if cfg!(target_family = "unix") {
        // originally we used cache_dir for all non-Windows platforms but that returns
        // a path that's not writable in cross-rs Docker. /tmp should always work
        PathBuf::from("/tmp")
    } else if cfg!(target_os = "windows") {
        // I didn't like the idea of having a cache dir in the root of %LOCALAPPDATA%.
        PathBuf::from(BaseDirs::new().unwrap().cache_dir()).join("cache")
    } else {
        PathBuf::from(BaseDirs::new().unwrap().cache_dir())
    };

    dir.join("iconify-rs")
}

#[cfg(all(not(test), feature = "cache"))]
fn iconify_cache_path(input: &IconifyInput) -> Result<std::path::PathBuf, syn::Error> {
    let digest = input.hash_digest()?;

    let mut path = iconify_cache_dir();
    path.push(&input.pack);
    path.push(format!("{}-{}", input.name, digest));
    path.set_extension("svg");
    Ok(path)
}

#[cfg(feature = "offline")]
fn offline_dir() -> std::path::PathBuf {
    use std::path::PathBuf;

    if let Ok(dir) = env::var("ICONIFY_OFFLINE_DIR") {
        return PathBuf::from(dir);
    }

    let mut path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
    path.push("icons");
    path
}

#[cfg(feature = "offline")]
fn offline_icon_path(input: &IconifyInput) -> Result<std::path::PathBuf, syn::Error> {
    let digest = input.hash_digest()?;

    let mut path = offline_dir();
    path.push(&input.pack);
    path.push(format!("{}-{}", input.name, digest));
    path.set_extension("svg");
    Ok(path)
}

#[cfg(feature = "offline")]
fn offline_svg(input: &IconifyInput) -> Result<String, syn::Error> {
    let path = offline_icon_path(input)?;

    std::fs::read_to_string(&path).map_err(|err| {
        syn::Error::new(
            Span::call_site(),
            format!("failed to read offline icon. {err}.\nusually this means you need to prepare icons first with ICONIFY_PREPARE."),
        )
    })
}

#[cfg(feature = "offline")]
fn prepare_offline_icons() -> bool {
    env::var("ICONIFY_PREPARE").ok().as_deref() == Some("true")
}

fn fetch_svg(iconify_input: &IconifyInput) -> Result<String, syn::Error> {
    #[cfg(all(not(test), feature = "cache"))]
    let path = {
        let path = iconify_cache_path(iconify_input)?;

        if let Ok(text) = std::fs::read_to_string(&path) {
            return Ok(text);
        }

        path
    };

    let url = iconify_input
        .icon_url()
        .map_err(|err| syn::Error::new(Span::call_site(), format!("couldn't parse url: {err}")))?;

    let response = ureq::get(&url).call().map_err(|err| {
        syn::Error::new(Span::call_site(), format!("failed to fetch icon: {err}"))
    })?;

    let text = response.into_string().map_err(|err| {
        syn::Error::new(Span::call_site(), format!("failed to fetch icon: {err}"))
    })?;

    // Iconify API does not set the status code to 404 when an icon is not found... amazing.
    if text == "404" {
        return Err(syn::Error::new(
            Span::call_site(),
            format!("icon not found: {}", url),
        ));
    }

    #[cfg(all(not(test), feature = "cache"))]
    {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, &text).unwrap();
    }

    Ok(text)
}

pub fn iconify_svg_impl(input: TokenStream) -> syn::Result<TokenStream> {
    let iconify_input = syn::parse2::<IconifyInput>(input)?;

    // If we're using offline icons, we need to fetch them from the
    // iconify API during development. This is done by setting the
    // ICONIFY_PREPARE environment variable.
    #[cfg(feature = "offline")]
    let svg = if prepare_offline_icons() {
        fetch_svg(&iconify_input)
    } else {
        offline_svg(&iconify_input)
    }?;

    #[cfg(not(feature = "offline"))]
    let svg = fetch_svg(&iconify_input)?;

    #[cfg(feature = "offline")]
    if prepare_offline_icons() {
        // Prepare offline icons
        let path = offline_icon_path(&iconify_input)?;

        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(&path, &svg).unwrap();
    }

    Ok(quote! {
        #svg
    })
}

#[cfg(test)]
mod tests {
    use super::iconify_svg_impl;
    use quote::quote;
    use std::result::Result;

    #[test]
    fn test_basic() -> Result<(), String> {
        let svg = iconify_svg_impl(quote! {
            "mdi:home"
        })
        .unwrap()
        .to_string();

        assert_eq!(
            svg,
            "\"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\" width=\\\"1em\\\" height=\\\"1em\\\" viewBox=\\\"0 0 24 24\\\"><path fill=\\\"currentColor\\\" d=\\\"M10 20v-6h4v6h5v-8h3L12 3L2 12h3v8z\\\"/></svg>\""
        );

        Ok(())
    }

    #[test]
    fn test_basic_attributes() -> Result<(), String> {
        let svg = iconify_svg_impl(quote! {
            "mdi:home",
            color = "red",
            width = "2em",
            height = "3em",
            flip = "both",
            rotate = "90",
            view_box = true
        })
        .unwrap()
        .to_string();

        assert_eq!(
            svg,
            "\"<svg xmlns=\\\"http://www.w3.org/2000/svg\\\" width=\\\"2em\\\" height=\\\"3em\\\" viewBox=\\\"0 0 24 24\\\"><rect x=\\\"0\\\" y=\\\"0\\\" width=\\\"24\\\" height=\\\"24\\\" fill=\\\"rgba(255, 255, 255, 0)\\\" /><g transform=\\\"rotate(-90 12 12)\\\"><path fill=\\\"red\\\" d=\\\"M10 20v-6h4v6h5v-8h3L12 3L2 12h3v8z\\\"/></g></svg>\""
        );

        Ok(())
    }

    #[test]
    fn test_pack_parse_fail() -> Result<(), String> {
        let no_colon = iconify_svg_impl(quote! {
            "mdi-home"
        })
        .unwrap_err()
        .to_string();

        let too_many_colons = iconify_svg_impl(quote! {
            "mdi:home:foo"
        })
        .unwrap_err()
        .to_string();

        assert_eq!(no_colon, "expected `pack_name:icon_name`");
        assert_eq!(too_many_colons, "expected `pack_name:icon_name`");

        Ok(())
    }

    #[test]
    fn test_pack_not_found_fail() -> Result<(), String> {
        let pack_not_found = iconify_svg_impl(quote! {
            "this-is-not:an-icon-i-hope"
        })
        .unwrap_err()
        .to_string();

        assert_eq!(
            pack_not_found,
            "icon not found: https://api.iconify.design/this-is-not/an-icon-i-hope.svg?"
        );

        Ok(())
    }
}