#[derive(Debug, Clone)]
#[cfg_attr(not(feature = "lucide-icons"), derive(PartialEq))]
pub enum Icon {
Text(String),
#[cfg(feature = "lucide-icons")]
Lucide(lucide_icons::Icon),
#[cfg(feature = "svg-icons")]
Svg(std::path::PathBuf),
}
#[cfg(feature = "lucide-icons")]
impl PartialEq for Icon {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Icon::Text(a), Icon::Text(b)) => a == b,
(Icon::Lucide(a), Icon::Lucide(b)) => (*a as usize) == (*b as usize),
#[cfg(feature = "svg-icons")]
(Icon::Svg(a), Icon::Svg(b)) => a == b,
_ => false,
}
}
}
impl From<&str> for Icon {
fn from(s: &str) -> Self {
Icon::Text(s.to_string())
}
}
impl From<String> for Icon {
fn from(s: String) -> Self {
Icon::Text(s)
}
}
#[cfg(feature = "lucide-icons")]
impl From<lucide_icons::Icon> for Icon {
fn from(i: lucide_icons::Icon) -> Self {
Icon::Lucide(i)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_icons_equal_when_same_string() {
let a: Icon = "★".into();
let b: Icon = "★".into();
assert_eq!(a, b);
}
#[test]
fn text_icons_not_equal_when_different_string() {
let a: Icon = "★".into();
let b: Icon = "☆".into();
assert_ne!(a, b);
}
#[test]
fn text_icon_from_string_vs_str() {
let a: Icon = "hello".into();
let b: Icon = String::from("hello").into();
assert_eq!(a, b);
}
#[cfg(feature = "svg-icons")]
#[test]
fn svg_icons_equal_when_same_path() {
let a = Icon::Svg(std::path::PathBuf::from("icons/foo.svg"));
let b = Icon::Svg(std::path::PathBuf::from("icons/foo.svg"));
assert_eq!(a, b);
}
#[cfg(feature = "svg-icons")]
#[test]
fn svg_icons_not_equal_when_different_path() {
let a = Icon::Svg(std::path::PathBuf::from("icons/foo.svg"));
let b = Icon::Svg(std::path::PathBuf::from("icons/bar.svg"));
assert_ne!(a, b);
}
}