use gpui::SharedString;
#[derive(Debug, Clone, PartialEq)]
pub enum IconSource {
Named(String),
FilePath(SharedString),
}
fn strip_svg_extension(s: &str) -> &str {
let trimmed = s.trim_end();
if trimmed.to_lowercase().ends_with(".svg") {
&trimmed[..trimmed.len() - ".svg".len()]
} else {
s
}
}
impl From<&str> for IconSource {
fn from(s: &str) -> Self {
if s.contains('/') || s.contains('\\') {
IconSource::FilePath(SharedString::from(s.to_string()))
} else {
IconSource::Named(strip_svg_extension(s).to_string())
}
}
}
impl From<String> for IconSource {
fn from(s: String) -> Self {
if s.contains('/') || s.contains('\\') {
IconSource::FilePath(s.into())
} else {
IconSource::Named(strip_svg_extension(&s).to_string())
}
}
}
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 {
IconSource::Named(strip_svg_extension(&s_str).to_string())
}
}
}
#[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("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"),
}
}
}