iconmate 1.2.1

CLI to fetch icons and save them into your Vite, NextJS, or similar TypeScript project
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
use reqwest::{Client, StatusCode, Url};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::collections::HashMap;

const DEFAULT_ICONIFY_BASE_URL: &str = "https://api.iconify.design";
pub const ICONIFY_BASE_URL_ENV: &str = "ICONMATE_ICONIFY_BASE_URL";

#[derive(Debug, Clone)]
pub struct IconifyClient {
    client: Client,
    base_url: Url,
}

impl IconifyClient {
    pub fn new() -> Result<Self, IconifyError> {
        Self::from_base_url(DEFAULT_ICONIFY_BASE_URL)
    }

    pub fn from_env() -> Result<Self, IconifyError> {
        let base = std::env::var(ICONIFY_BASE_URL_ENV)
            .unwrap_or_else(|_| DEFAULT_ICONIFY_BASE_URL.to_string());
        Self::from_base_url(&base)
    }

    pub fn from_base_url(base_url: &str) -> Result<Self, IconifyError> {
        let normalized = if base_url.ends_with('/') {
            base_url.to_string()
        } else {
            format!("{base_url}/")
        };

        let base_url = Url::parse(&normalized).map_err(|source| IconifyError::InvalidBaseUrl {
            base_url: base_url.to_string(),
            source: source.to_string(),
        })?;

        Ok(Self {
            client: Client::new(),
            base_url,
        })
    }

    pub async fn collections(&self) -> Result<IconifyCollectionsResponse, IconifyError> {
        let collections: HashMap<String, IconifyCollectionMeta> =
            self.get_json("collections", &[]).await?;
        Ok(IconifyCollectionsResponse { collections })
    }

    pub async fn collection(
        &self,
        prefix: &str,
    ) -> Result<IconifyCollectionResponse, IconifyError> {
        let response: IconifyCollectionApiResponse = self
            .get_json("collection", &[("prefix".to_string(), prefix.to_string())])
            .await?;

        let icons = merge_collection_icons(
            response.icons,
            response.uncategorized.as_ref(),
            response.categories.as_ref(),
        );

        Ok(IconifyCollectionResponse {
            prefix: response.prefix,
            icons,
            uncategorized: response.uncategorized,
        })
    }

    pub async fn search(
        &self,
        query: &str,
        limit: Option<u32>,
        start: Option<u32>,
        include_collections: bool,
    ) -> Result<IconifySearchResponse, IconifyError> {
        let mut params = vec![("query".to_string(), query.to_string())];

        if let Some(limit) = limit {
            params.push(("limit".to_string(), limit.to_string()));
        }

        if let Some(start) = start {
            params.push(("start".to_string(), start.to_string()));
        }

        let mut response: IconifySearchResponse = self.get_json("search", &params).await?;

        if !include_collections {
            response.collections = None;
        }

        Ok(response)
    }

    pub async fn svg(&self, prefix_icon: &str) -> Result<String, IconifyError> {
        let path = format!("{prefix_icon}.svg");
        self.get_text(&path, &[]).await
    }

    pub async fn icon_json(
        &self,
        prefix: &str,
        icon: &str,
    ) -> Result<serde_json::Value, IconifyError> {
        let path = format!("{prefix}.json");
        self.get_json(&path, &[("icons".to_string(), icon.to_string())])
            .await
    }

    pub async fn icon_json_by_name(
        &self,
        prefix_icon: &str,
    ) -> Result<serde_json::Value, IconifyError> {
        let (prefix, icon) = prefix_icon
            .split_once(':')
            .ok_or_else(|| IconifyError::InvalidIconName(prefix_icon.to_string()))?;
        self.icon_json(prefix, icon).await
    }

    async fn get_json<T: DeserializeOwned>(
        &self,
        path: &str,
        query: &[(String, String)],
    ) -> Result<T, IconifyError> {
        let url = self.build_url(path, query)?;
        let endpoint = url.to_string();

        let response = self
            .client
            .get(url)
            .send()
            .await
            .map_err(IconifyError::Network)?;
        let status = response.status();
        let body = response.text().await.map_err(IconifyError::Network)?;

        if !status.is_success() {
            return Err(IconifyError::HttpStatus {
                status,
                endpoint,
                body,
            });
        }

        serde_json::from_str(&body).map_err(|source| IconifyError::JsonDecode { endpoint, source })
    }

    async fn get_text(
        &self,
        path: &str,
        query: &[(String, String)],
    ) -> Result<String, IconifyError> {
        let url = self.build_url(path, query)?;
        let endpoint = url.to_string();

        let response = self
            .client
            .get(url)
            .send()
            .await
            .map_err(IconifyError::Network)?;
        let status = response.status();
        let body = response.text().await.map_err(IconifyError::Network)?;

        if !status.is_success() {
            return Err(IconifyError::HttpStatus {
                status,
                endpoint,
                body,
            });
        }

        Ok(body)
    }

    fn build_url(&self, path: &str, query: &[(String, String)]) -> Result<Url, IconifyError> {
        let relative_path = if path.starts_with('/') {
            format!("./{}", path.trim_start_matches('/'))
        } else {
            format!("./{path}")
        };

        let mut url =
            self.base_url
                .join(&relative_path)
                .map_err(|source| IconifyError::InvalidEndpoint {
                    path: path.to_string(),
                    source: source.to_string(),
                })?;

        if !query.is_empty() {
            let mut pairs = url.query_pairs_mut();
            for (key, value) in query {
                pairs.append_pair(key, value);
            }
        }

        Ok(url)
    }
}

fn merge_collection_icons(
    icons: Vec<String>,
    uncategorized: Option<&Vec<String>>,
    categories: Option<&HashMap<String, Vec<String>>>,
) -> Vec<String> {
    let mut merged = icons;

    if let Some(uncategorized) = uncategorized {
        merged.extend(uncategorized.iter().cloned());
    }

    if let Some(categories) = categories {
        for category_icons in categories.values() {
            merged.extend(category_icons.iter().cloned());
        }
    }

    let mut deduped = Vec::new();
    let mut seen = std::collections::HashSet::new();

    for icon in merged {
        if seen.insert(icon.clone()) {
            deduped.push(icon);
        }
    }

    deduped
}

#[derive(Debug)]
pub enum IconifyError {
    InvalidBaseUrl {
        base_url: String,
        source: String,
    },
    InvalidEndpoint {
        path: String,
        source: String,
    },
    InvalidIconName(String),
    Network(reqwest::Error),
    HttpStatus {
        status: StatusCode,
        endpoint: String,
        body: String,
    },
    JsonDecode {
        endpoint: String,
        source: serde_json::Error,
    },
}

impl std::fmt::Display for IconifyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IconifyError::InvalidBaseUrl { base_url, .. } => {
                write!(f, "invalid Iconify base URL: {base_url}")
            }
            IconifyError::InvalidEndpoint { path, .. } => {
                write!(f, "invalid Iconify endpoint path: {path}")
            }
            IconifyError::InvalidIconName(name) => {
                write!(
                    f,
                    "invalid Iconify icon name (expected <prefix:icon>): {name}"
                )
            }
            IconifyError::Network(source) => write!(f, "Iconify network error: {source}"),
            IconifyError::HttpStatus {
                status, endpoint, ..
            } => {
                write!(f, "Iconify request failed ({status}) for {endpoint}")
            }
            IconifyError::JsonDecode { endpoint, source } => {
                write!(
                    f,
                    "failed to parse Iconify response from {endpoint}: {source}"
                )
            }
        }
    }
}

impl std::error::Error for IconifyError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            IconifyError::InvalidBaseUrl { .. } => None,
            IconifyError::InvalidEndpoint { .. } => None,
            IconifyError::InvalidIconName(_) => None,
            IconifyError::Network(source) => Some(source),
            IconifyError::JsonDecode { source, .. } => Some(source),
            IconifyError::HttpStatus { .. } => None,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IconifySearchResponse {
    #[serde(default)]
    pub icons: Vec<String>,
    #[serde(default)]
    pub total: u32,
    #[serde(default)]
    pub limit: u32,
    #[serde(default)]
    pub start: u32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub collections: Option<HashMap<String, serde_json::Value>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IconifyCollectionsResponse {
    pub collections: HashMap<String, IconifyCollectionMeta>,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct IconifyCollectionMeta {
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub title: Option<String>,
    #[serde(default)]
    pub total: Option<u32>,
    #[serde(flatten, default)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl IconifyCollectionMeta {
    pub fn display_name(&self, fallback: &str) -> String {
        if let Some(name) = &self.name {
            return name.clone();
        }

        if let Some(title) = &self.title {
            return title.clone();
        }

        fallback.to_string()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IconifyCollectionResponse {
    pub prefix: String,
    #[serde(default)]
    pub icons: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub uncategorized: Option<Vec<String>>,
}

#[derive(Debug, Clone, Deserialize)]
struct IconifyCollectionApiResponse {
    pub prefix: String,
    #[serde(default)]
    pub icons: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub uncategorized: Option<Vec<String>>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub categories: Option<HashMap<String, Vec<String>>>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_search_response_without_collections() {
        let fixture = r#"
        {
            "icons": ["mdi:home", "mdi:heart"],
            "total": 2,
            "limit": 20,
            "start": 0
        }
        "#;

        let response: IconifySearchResponse =
            serde_json::from_str(fixture).expect("fixture should deserialize");

        assert_eq!(response.icons.len(), 2);
        assert_eq!(response.total, 2);
        assert!(response.collections.is_none());
    }

    #[test]
    fn parse_search_response_with_collections() {
        let fixture = r#"
        {
            "icons": ["mdi:home"],
            "total": 1,
            "limit": 1,
            "start": 0,
            "collections": {
                "mdi": {"name": "Material Design Icons", "total": 7000}
            }
        }
        "#;

        let response: IconifySearchResponse =
            serde_json::from_str(fixture).expect("fixture should deserialize");

        assert_eq!(response.icons, vec!["mdi:home"]);
        assert!(response.collections.is_some());
        assert!(
            response
                .collections
                .as_ref()
                .expect("collections should exist")
                .contains_key("mdi")
        );
    }

    #[test]
    fn parse_collections_response() {
        let fixture = r#"
        {
            "mdi": {
                "name": "Material Design Icons",
                "total": 7447
            },
            "heroicons": {
                "title": "Heroicons",
                "total": 292
            }
        }
        "#;

        let response: HashMap<String, IconifyCollectionMeta> =
            serde_json::from_str(fixture).expect("fixture should deserialize");

        let mdi = response.get("mdi").expect("mdi should exist");
        assert_eq!(mdi.display_name("mdi"), "Material Design Icons");
        assert_eq!(mdi.total, Some(7447));

        let heroicons = response.get("heroicons").expect("heroicons should exist");
        assert_eq!(heroicons.display_name("heroicons"), "Heroicons");
    }

    #[test]
    fn parse_collection_response_with_optional_uncategorized() {
        let fixture = r#"
        {
            "prefix": "mdi",
            "icons": ["home", "heart"],
            "uncategorized": ["orphan"]
        }
        "#;

        let response: IconifyCollectionResponse =
            serde_json::from_str(fixture).expect("fixture should deserialize");

        assert_eq!(response.prefix, "mdi");
        assert_eq!(response.icons, vec!["home", "heart"]);
        assert_eq!(response.uncategorized, Some(vec!["orphan".to_string()]));
    }

    #[test]
    fn merge_icons_from_uncategorized_and_categories() {
        let categories = HashMap::from([
            (
                "Actions".to_string(),
                vec!["home".to_string(), "heart".to_string()],
            ),
            (
                "Shapes".to_string(),
                vec!["star".to_string(), "home".to_string()],
            ),
        ]);

        let merged = merge_collection_icons(
            Vec::new(),
            Some(&vec!["orphan".to_string(), "home".to_string()]),
            Some(&categories),
        );

        assert!(merged.contains(&"orphan".to_string()));
        assert!(merged.contains(&"home".to_string()));
        assert!(merged.contains(&"heart".to_string()));
        assert!(merged.contains(&"star".to_string()));
        assert_eq!(
            merged.iter().filter(|icon| icon.as_str() == "home").count(),
            1
        );
    }
}