fc_ui/capabilities/primitives/
icon_source.rs1use gpui::SharedString;
4
5#[derive(Debug, Clone, PartialEq)]
7pub enum IconSource {
8 Named(String),
11
12 FilePath(SharedString),
15}
16
17fn strip_svg_extension(s: &str) -> &str {
20 let trimmed = s.trim_end();
21 if trimmed.to_lowercase().ends_with(".svg") {
22 &trimmed[..trimmed.len() - ".svg".len()]
23 } else {
24 s
25 }
26}
27
28impl From<&str> for IconSource {
29 fn from(s: &str) -> Self {
30 if s.contains('/') || s.contains('\\') {
32 IconSource::FilePath(SharedString::from(s.to_string()))
33 } else {
34 IconSource::Named(strip_svg_extension(s).to_string())
37 }
38 }
39}
40
41impl From<String> for IconSource {
42 fn from(s: String) -> Self {
43 if s.contains('/') || s.contains('\\') {
44 IconSource::FilePath(s.into())
45 } else {
46 IconSource::Named(strip_svg_extension(&s).to_string())
47 }
48 }
49}
50
51impl From<SharedString> for IconSource {
52 fn from(s: SharedString) -> Self {
53 let s_str = s.to_string();
54 if s_str.contains('/') || s_str.contains('\\') {
55 IconSource::FilePath(s)
56 } else {
57 IconSource::Named(strip_svg_extension(&s_str).to_string())
58 }
59 }
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 #[test]
67 fn test_icon_source_detection() {
68 match IconSource::from("search") {
70 IconSource::Named(name) => assert_eq!(name, "search"),
71 _ => panic!("Should be Named"),
72 }
73
74 match IconSource::from("custom/icon.svg") {
76 IconSource::FilePath(_) => {}
77 _ => panic!("Should be FilePath"),
78 }
79
80 match IconSource::from("my-icon.svg") {
82 IconSource::Named(name) => assert_eq!(name, "my-icon"),
83 _ => panic!("Should be Named"),
84 }
85
86 match IconSource::from("my-icon.SVG") {
88 IconSource::Named(name) => assert_eq!(name, "my-icon"),
89 _ => panic!("Should be Named"),
90 }
91
92 match IconSource::from("custom\\icon.svg") {
94 IconSource::FilePath(_) => {}
95 _ => panic!("Should be FilePath"),
96 }
97 }
98}