#![forbid(unsafe_code)]
use next_rust_view::{Attr, Element, Node, Part, View, raw_html};
#[allow(non_snake_case)]
mod generated;
pub use generated::*;
pub const LUCIDE_VERSION: &str = generated::LUCIDE_VERSION;
#[derive(Debug, PartialEq, Eq)]
pub struct IconData {
pub name: &'static str,
pub body: &'static str,
}
#[derive(Debug)]
pub struct Icon {
data: &'static IconData,
size: Option<String>,
color: Option<String>,
stroke_width: Option<f32>,
absolute_stroke_width: bool,
fill: Option<String>,
title: Option<String>,
extra: Element,
}
impl Icon {
pub fn new(data: &'static IconData) -> Self {
Icon {
data,
size: None,
color: None,
stroke_width: None,
absolute_stroke_width: false,
fill: None,
title: None,
extra: Element::new("svg"),
}
}
pub fn size(mut self, v: impl ToString) -> Self {
self.size = Some(v.to_string());
self
}
pub fn color(mut self, v: impl Into<String>) -> Self {
self.color = Some(v.into());
self
}
pub fn stroke_width(mut self, v: f32) -> Self {
self.stroke_width = Some(v);
self
}
pub fn absolute_stroke_width(mut self, v: bool) -> Self {
self.absolute_stroke_width = v;
self
}
pub fn fill(mut self, v: impl Into<String>) -> Self {
self.fill = Some(v.into());
self
}
pub fn title(mut self, v: impl Into<String>) -> Self {
self.title = Some(v.into());
self
}
pub fn class(self, v: impl Into<String>) -> Self {
self.with(next_rust_view::attrs::class(v.into()))
}
pub fn with(mut self, part: impl Part) -> Self {
self.extra = self.extra.with(part);
self
}
pub fn name(&self) -> &'static str {
self.data.name
}
fn stroke(&self) -> String {
let width = self.stroke_width.unwrap_or(2.0);
let px = self.size.as_deref().map(|s| s.trim_end_matches("px")).and_then(|s| s.parse::<f32>().ok());
match (self.absolute_stroke_width, px) {
(true, Some(px)) if px > 0.0 => number(width * 24.0 / px),
_ => number(width),
}
}
}
fn number(v: f32) -> String {
let s = format!("{v:.3}");
s.trim_end_matches('0').trim_end_matches('.').to_owned()
}
impl View for Icon {
fn into_node(self) -> Node {
let size = self.size.clone().unwrap_or_else(|| "24".into());
let mut svg = Element::new("svg");
for (name, value) in [
("width", size.clone()),
("height", size),
("viewBox", "0 0 24 24".into()),
("fill", self.fill.clone().unwrap_or_else(|| "none".into())),
("stroke", self.color.clone().unwrap_or_else(|| "currentColor".into())),
("stroke-width", self.stroke()),
("stroke-linecap", "round".into()),
("stroke-linejoin", "round".into()),
] {
svg.set_attr(Attr::new(name, value));
}
match &self.title {
Some(title) => {
svg.set_attr(Attr::new("role", "img"));
svg.set_attr(Attr::new("aria-label", title.clone()));
svg.children.push(Element::new("title").with(title.clone()).into_node());
}
None => svg.set_attr(Attr::new("aria-hidden", "true")),
}
for a in self.extra.attrs {
svg.set_attr(a);
}
svg.children.push(raw_html(self.data.body));
svg.children.extend(self.extra.children);
svg.into_node()
}
}
pub fn all() -> &'static [&'static IconData] {
generated::ALL
}
pub fn by_name(name: &str) -> Option<Icon> {
if let Ok(i) = generated::ALL.binary_search_by(|d| d.name.cmp(name)) {
return Some(Icon::new(generated::ALL[i]));
}
let i = generated::ALIASES.binary_search_by(|(alias, _)| (*alias).cmp(name)).ok()?;
Some(Icon::new(generated::ALIASES[i].1))
}
#[macro_export]
macro_rules! Icon {
($icon:ident $(, $($body:tt)*)?) => { $crate::__icon_munch!([$crate::$icon()] $($($body)*)?) };
}
#[doc(hidden)]
#[macro_export]
macro_rules! __icon_munch {
([$($acc:tt)*]) => { $($acc)* };
([$($acc:tt)*] $key:ident = $val:expr $(, $($rest:tt)*)?) => {
$crate::__icon_munch!([$($acc)*.$key($val)] $($($rest)*)?)
};
([$($acc:tt)*] $part:expr $(, $($rest:tt)*)?) => {
$crate::__icon_munch!([$($acc)*.with($part)] $($($rest)*)?)
};
}