Skip to main content

oxblade_icons/
lib.rs

1//! Inline SVG rendering, following `blade-ui-kit/blade-icons` 1.11.0.
2//!
3//! Icon sources contain trusted SVG markup. Request data can select icon names and
4//! supply plain attribute values, but must not supply SVG markup or attribute keys.
5
6#![forbid(unsafe_code)]
7
8mod attributes;
9mod registry;
10
11#[cfg(feature = "blade")]
12pub mod blade;
13
14#[cfg(feature = "laravel")]
15pub mod laravel;
16
17pub use attributes::{AttrValue, Attributes, Svg, Trusted};
18pub use registry::{IconSet, Icons, IconsBuilder, Notice};
19
20use std::fmt;
21
22/// A failed lookup, invalid icon configuration, or unsupported attribute.
23#[derive(Clone, Debug, Eq, PartialEq)]
24#[non_exhaustive]
25pub enum Error {
26    /// An icon or its fallback could not be found.
27    NotFound(String),
28    /// An icon name is not a safe relative lookup key.
29    InvalidName(String),
30    /// An attribute name is invalid or would register a script handler.
31    InvalidAttribute(String),
32    /// The source is not a supported SVG document for inline interpolation.
33    InvalidSvg(String),
34    /// A set or its prefix is already registered.
35    DuplicateSet(String),
36    /// A configured fallback does not resolve to an icon.
37    UnknownFallback(String),
38    /// Deferred SVG markup needs a Blade rendering stack and is not supported.
39    Deferred,
40}
41
42impl fmt::Display for Error {
43    fn fmt(&self, out: &mut fmt::Formatter<'_>) -> fmt::Result {
44        match self {
45            Self::NotFound(name) => write!(out, "icon {name:?} not found"),
46            Self::InvalidName(name) => write!(out, "invalid icon name {name:?}"),
47            Self::InvalidAttribute(name) => write!(out, "invalid SVG attribute {name:?}"),
48            Self::InvalidSvg(name) => write!(out, "unsupported inline SVG source for {name:?}"),
49            Self::DuplicateSet(name) => write!(out, "duplicate icon set or prefix {name:?}"),
50            Self::UnknownFallback(name) => write!(out, "unknown fallback icon {name:?}"),
51            Self::Deferred => out.write_str("deferred icons are not supported"),
52        }
53    }
54}
55
56impl std::error::Error for Error {}
57
58/// The result of an icon operation.
59pub type Result<T> = std::result::Result<T, Error>;
60
61/// Convert a generated `oxblade-*` data crate into a registered icon set.
62///
63/// Pass the crate's Rust identifier, for example `icon_set!(oxblade_lucide)`.
64/// The macro checks the data format at compile time. No data crate depends on
65/// the renderer, and the renderer does not select a set unless you register it.
66#[macro_export]
67macro_rules! icon_set {
68    ($crate_name:path) => {{
69        use $crate_name as __oxblade_icon_data;
70        const _: () = assert!(
71            __oxblade_icon_data::FORMAT == 1,
72            "unsupported oxblade icon data format"
73        );
74        $crate::IconSet::from_static(
75            __oxblade_icon_data::NAME,
76            __oxblade_icon_data::PREFIX,
77            __oxblade_icon_data::ICONS,
78        )
79        .source(__oxblade_icon_data::SOURCE)
80        .notice($crate::Notice {
81            title: __oxblade_icon_data::SOURCE,
82            spdx: __oxblade_icon_data::LICENSE,
83            text: __oxblade_icon_data::NOTICE,
84        })
85    }};
86}