use kael::SharedString;
#[derive(Debug, Clone, PartialEq)]
pub enum IconSource {
Named(String),
FilePath(SharedString),
}
impl From<&str> for IconSource {
fn from(s: &str) -> Self {
if s.contains('/') || s.contains('\\') {
IconSource::FilePath(SharedString::from(s.to_string()))
} else if s.trim_end().to_lowercase().ends_with(".svg") {
IconSource::Named(s.trim_end_matches(".svg").to_string())
} else {
IconSource::Named(s.to_string())
}
}
}
impl From<String> for IconSource {
fn from(s: String) -> Self {
if s.contains('/') || s.contains('\\') {
IconSource::FilePath(s.into())
} else if s.trim_end().to_lowercase().ends_with(".svg") {
IconSource::Named(s.trim_end_matches(".svg").to_string())
} else {
IconSource::Named(s)
}
}
}
impl From<SharedString> for IconSource {
fn from(s: SharedString) -> Self {
let s_str = s.to_string();
if s_str.contains('/') || s_str.contains('\\') {
IconSource::FilePath(s)
} else if s_str.trim_end().to_lowercase().ends_with(".svg") {
IconSource::Named(s_str.trim_end_matches(".svg").to_string())
} else {
IconSource::Named(s_str)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_icon_source_detection() {
match IconSource::from("search") {
IconSource::Named(name) => assert_eq!(name, "search"),
_ => panic!("Should be Named"),
}
match IconSource::from("custom/icon.svg") {
IconSource::FilePath(_) => {}
_ => panic!("Should be FilePath"),
}
match IconSource::from("my-icon.svg") {
IconSource::Named(name) => assert_eq!(name, "my-icon"),
_ => panic!("Should be Named"),
}
match IconSource::from("custom\\icon.svg") {
IconSource::FilePath(_) => {}
_ => panic!("Should be FilePath"),
}
}
}