1use std::borrow::Cow;
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
41#[non_exhaustive]
42pub enum IconRole {
43 DialogWarning,
46 DialogError,
48 DialogInfo,
50 DialogQuestion,
52 DialogSuccess,
54 Shield,
56
57 WindowClose,
60 WindowMinimize,
62 WindowMaximize,
64 WindowRestore,
66
67 ActionSave,
70 ActionDelete,
72 ActionCopy,
74 ActionPaste,
76 ActionCut,
78 ActionUndo,
80 ActionRedo,
82 ActionSearch,
84 ActionSettings,
86 ActionEdit,
88 ActionAdd,
90 ActionRemove,
92 ActionRefresh,
94 ActionPrint,
96
97 NavBack,
100 NavForward,
102 NavUp,
104 NavDown,
106 NavHome,
108 NavMenu,
110
111 FileGeneric,
114 FolderClosed,
116 FolderOpen,
118 TrashEmpty,
120 TrashFull,
122
123 StatusBusy,
126 StatusCheck,
128 StatusError,
130
131 UserAccount,
134 Notification,
136 Help,
138 Lock,
140}
141
142impl std::fmt::Display for IconRole {
143 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144 f.write_str(self.name())
145 }
146}
147
148impl IconRole {
149 #[must_use]
164 pub fn name(&self) -> &'static str {
165 match self {
166 Self::DialogWarning => "dialog-warning",
168 Self::DialogError => "dialog-error",
169 Self::DialogInfo => "dialog-info",
170 Self::DialogQuestion => "dialog-question",
171 Self::DialogSuccess => "dialog-success",
172 Self::Shield => "shield",
173 Self::WindowClose => "window-close",
175 Self::WindowMinimize => "window-minimize",
176 Self::WindowMaximize => "window-maximize",
177 Self::WindowRestore => "window-restore",
178 Self::ActionSave => "action-save",
180 Self::ActionDelete => "action-delete",
181 Self::ActionCopy => "action-copy",
182 Self::ActionPaste => "action-paste",
183 Self::ActionCut => "action-cut",
184 Self::ActionUndo => "action-undo",
185 Self::ActionRedo => "action-redo",
186 Self::ActionSearch => "action-search",
187 Self::ActionSettings => "action-settings",
188 Self::ActionEdit => "action-edit",
189 Self::ActionAdd => "action-add",
190 Self::ActionRemove => "action-remove",
191 Self::ActionRefresh => "action-refresh",
192 Self::ActionPrint => "action-print",
193 Self::NavBack => "nav-back",
195 Self::NavForward => "nav-forward",
196 Self::NavUp => "nav-up",
197 Self::NavDown => "nav-down",
198 Self::NavHome => "nav-home",
199 Self::NavMenu => "nav-menu",
200 Self::FileGeneric => "file-generic",
202 Self::FolderClosed => "folder-closed",
203 Self::FolderOpen => "folder-open",
204 Self::TrashEmpty => "trash-empty",
205 Self::TrashFull => "trash-full",
206 Self::StatusBusy => "status-busy",
208 Self::StatusCheck => "status-check",
209 Self::StatusError => "status-error",
210 Self::UserAccount => "user-account",
212 Self::Notification => "notification",
213 Self::Help => "help",
214 Self::Lock => "lock",
215 }
216 }
217
218 pub const ALL: [IconRole; 42] = [
222 Self::DialogWarning,
224 Self::DialogError,
225 Self::DialogInfo,
226 Self::DialogQuestion,
227 Self::DialogSuccess,
228 Self::Shield,
229 Self::WindowClose,
231 Self::WindowMinimize,
232 Self::WindowMaximize,
233 Self::WindowRestore,
234 Self::ActionSave,
236 Self::ActionDelete,
237 Self::ActionCopy,
238 Self::ActionPaste,
239 Self::ActionCut,
240 Self::ActionUndo,
241 Self::ActionRedo,
242 Self::ActionSearch,
243 Self::ActionSettings,
244 Self::ActionEdit,
245 Self::ActionAdd,
246 Self::ActionRemove,
247 Self::ActionRefresh,
248 Self::ActionPrint,
249 Self::NavBack,
251 Self::NavForward,
252 Self::NavUp,
253 Self::NavDown,
254 Self::NavHome,
255 Self::NavMenu,
256 Self::FileGeneric,
258 Self::FolderClosed,
259 Self::FolderOpen,
260 Self::TrashEmpty,
261 Self::TrashFull,
262 Self::StatusBusy,
264 Self::StatusCheck,
265 Self::StatusError,
266 Self::UserAccount,
268 Self::Notification,
269 Self::Help,
270 Self::Lock,
271 ];
272}
273
274#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
292#[non_exhaustive]
293#[must_use]
294pub enum IconData {
295 Svg(Cow<'static, [u8]>),
301
302 Rgba {
304 width: u32,
306 height: u32,
308 data: Vec<u8>,
310 },
311}
312
313impl IconData {
314 #[must_use]
319 pub fn bytes(&self) -> &[u8] {
320 match self {
321 IconData::Svg(cow) => cow,
322 IconData::Rgba { data, .. } => data,
323 }
324 }
325}
326
327#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
350#[serde(rename_all = "kebab-case")]
351#[non_exhaustive]
352pub enum IconSet {
353 SfSymbols,
355 #[serde(rename = "segoe-fluent")]
357 SegoeIcons,
358 Freedesktop,
360 Material,
362 Lucide,
364}
365
366impl std::fmt::Display for IconSet {
367 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
368 f.write_str(self.name())
369 }
370}
371
372impl IconSet {
373 #[must_use]
380 pub fn from_name(name: &str) -> Option<Self> {
381 match name {
382 "sf-symbols" => Some(Self::SfSymbols),
383 "segoe-fluent" => Some(Self::SegoeIcons),
384 "freedesktop" => Some(Self::Freedesktop),
385 "material" => Some(Self::Material),
386 "lucide" => Some(Self::Lucide),
387 _ => None,
388 }
389 }
390
391 #[must_use]
393 pub fn name(&self) -> &'static str {
394 match self {
395 Self::SfSymbols => "sf-symbols",
396 Self::SegoeIcons => "segoe-fluent",
397 Self::Freedesktop => "freedesktop",
398 Self::Material => "material",
399 Self::Lucide => "lucide",
400 }
401 }
402}
403
404pub trait IconProvider: std::fmt::Debug {
447 fn icon_name(&self, set: IconSet) -> Option<&str>;
449
450 fn icon_svg(&self, set: IconSet) -> Option<Cow<'static, [u8]>>;
455}
456
457impl IconProvider for IconRole {
458 fn icon_name(&self, set: IconSet) -> Option<&str> {
459 icon_name(*self, set)
460 }
461
462 fn icon_svg(&self, set: IconSet) -> Option<Cow<'static, [u8]>> {
463 crate::model::bundled::bundled_icon_svg(*self, set).map(Cow::Borrowed)
464 }
465}
466
467#[must_use]
483#[allow(unreachable_patterns)] pub fn icon_name(role: IconRole, set: IconSet) -> Option<&'static str> {
485 match set {
486 IconSet::SfSymbols => sf_symbols_name(role),
487 IconSet::SegoeIcons => segoe_name(role),
488 IconSet::Freedesktop => freedesktop_name(role),
489 IconSet::Material => material_name(role),
490 IconSet::Lucide => lucide_name(role),
491 _ => None,
492 }
493}
494
495#[must_use]
512pub fn system_icon_set() -> IconSet {
513 if cfg!(any(target_os = "macos", target_os = "ios")) {
514 IconSet::SfSymbols
515 } else if cfg!(target_os = "windows") {
516 IconSet::SegoeIcons
517 } else if cfg!(target_os = "linux") {
518 IconSet::Freedesktop
519 } else {
520 IconSet::Material
521 }
522}
523
524#[must_use]
555pub fn system_icon_theme() -> String {
556 crate::detect::system().icon_theme().to_string()
557}
558
559#[must_use]
568#[allow(unreachable_code)]
569pub fn detect_icon_theme() -> String {
570 #[cfg(any(target_os = "macos", target_os = "ios"))]
571 {
572 return "sf-symbols".to_string();
573 }
574
575 #[cfg(target_os = "windows")]
576 {
577 return "segoe-fluent".to_string();
578 }
579
580 #[cfg(target_os = "linux")]
581 {
582 detect_linux_icon_theme()
583 }
584
585 #[cfg(not(any(
586 target_os = "linux",
587 target_os = "windows",
588 target_os = "macos",
589 target_os = "ios"
590 )))]
591 {
592 "material".to_string()
593 }
594}
595
596#[cfg(target_os = "linux")]
598fn detect_linux_icon_theme() -> String {
599 let de = crate::detect::detect_linux_desktop();
600
601 match de {
602 crate::detect::LinuxDesktop::Kde => detect_kde_icon_theme(),
603 crate::detect::LinuxDesktop::Gnome
604 | crate::detect::LinuxDesktop::Budgie
605 | crate::detect::LinuxDesktop::Hyprland
606 | crate::detect::LinuxDesktop::Sway
607 | crate::detect::LinuxDesktop::River
608 | crate::detect::LinuxDesktop::Niri
609 | crate::detect::LinuxDesktop::Wayfire
610 | crate::detect::LinuxDesktop::CosmicDe => {
611 gsettings_icon_theme("org.gnome.desktop.interface")
612 }
613 crate::detect::LinuxDesktop::Cinnamon => {
614 gsettings_icon_theme("org.cinnamon.desktop.interface")
615 }
616 crate::detect::LinuxDesktop::Xfce => detect_xfce_icon_theme(),
617 crate::detect::LinuxDesktop::Mate => gsettings_icon_theme("org.mate.interface"),
618 crate::detect::LinuxDesktop::LxQt => detect_lxqt_icon_theme(),
619 crate::detect::LinuxDesktop::Unknown => {
620 let kde = detect_kde_icon_theme();
621 if kde != "hicolor" {
622 return kde;
623 }
624 let gnome = gsettings_icon_theme("org.gnome.desktop.interface");
625 if gnome != "hicolor" {
626 return gnome;
627 }
628 "hicolor".to_string()
629 }
630 }
631}
632
633#[cfg(target_os = "linux")]
641fn detect_kde_icon_theme() -> String {
642 let Some(config_dir) = xdg_config_dir() else {
643 return "hicolor".to_string();
644 };
645 let paths = [
646 config_dir.join("kdeglobals"),
647 config_dir.join("kdedefaults").join("kdeglobals"),
648 ];
649
650 for path in &paths {
651 if let Some(theme) = read_ini_value(path, "Icons", "Theme") {
652 return theme;
653 }
654 }
655 "hicolor".to_string()
656}
657
658#[cfg(target_os = "linux")]
660fn gsettings_icon_theme(schema: &str) -> String {
661 std::process::Command::new("gsettings")
662 .args(["get", schema, "icon-theme"])
663 .output()
664 .ok()
665 .filter(|o| o.status.success())
666 .and_then(|o| String::from_utf8(o.stdout).ok())
667 .map(|s| s.trim().trim_matches('\'').to_string())
668 .filter(|s| !s.is_empty())
669 .unwrap_or_else(|| "hicolor".to_string())
670}
671
672#[cfg(target_os = "linux")]
674fn detect_xfce_icon_theme() -> String {
675 std::process::Command::new("xfconf-query")
676 .args(["-c", "xsettings", "-p", "/Net/IconThemeName"])
677 .output()
678 .ok()
679 .filter(|o| o.status.success())
680 .and_then(|o| String::from_utf8(o.stdout).ok())
681 .map(|s| s.trim().to_string())
682 .filter(|s| !s.is_empty())
683 .unwrap_or_else(|| "hicolor".to_string())
684}
685
686#[cfg(target_os = "linux")]
691fn detect_lxqt_icon_theme() -> String {
692 let Some(config_dir) = xdg_config_dir() else {
693 return "hicolor".to_string();
694 };
695 let path = config_dir.join("lxqt").join("lxqt.conf");
696
697 if let Ok(content) = std::fs::read_to_string(&path) {
698 for line in content.lines() {
699 let trimmed = line.trim();
700 if let Some(value) = trimmed.strip_prefix("icon_theme=") {
701 let value = value.trim();
702 if !value.is_empty() {
703 return value.to_string();
704 }
705 }
706 }
707 }
708 "hicolor".to_string()
709}
710
711#[cfg(target_os = "linux")]
716fn xdg_config_dir() -> Option<std::path::PathBuf> {
717 if let Ok(config_home) = std::env::var("XDG_CONFIG_HOME")
718 && !config_home.is_empty()
719 {
720 return Some(std::path::PathBuf::from(config_home));
721 }
722 std::env::var("HOME")
723 .ok()
724 .filter(|h| !h.is_empty())
725 .map(|h| std::path::PathBuf::from(h).join(".config"))
726}
727
728#[cfg(target_os = "linux")]
734fn read_ini_value(path: &std::path::Path, section: &str, key: &str) -> Option<String> {
735 let content = std::fs::read_to_string(path).ok()?;
736 let target_section = format!("[{}]", section);
737 let mut in_section = false;
738
739 for line in content.lines() {
740 let trimmed = line.trim();
741 if trimmed.starts_with('[') {
742 in_section = trimmed == target_section;
743 continue;
744 }
745 if in_section && let Some(value) = trimmed.strip_prefix(key) {
746 let value = value.trim_start();
747 if let Some(value) = value.strip_prefix('=') {
748 let value = value.trim();
749 if !value.is_empty() {
750 return Some(value.to_string());
751 }
752 }
753 }
754 }
755 None
756}
757
758#[allow(unreachable_patterns)]
761fn sf_symbols_name(role: IconRole) -> Option<&'static str> {
762 Some(match role {
763 IconRole::DialogWarning => "exclamationmark.triangle.fill",
765 IconRole::DialogError => "xmark.circle.fill",
766 IconRole::DialogInfo => "info.circle.fill",
767 IconRole::DialogQuestion => "questionmark.circle.fill",
768 IconRole::DialogSuccess => "checkmark.circle.fill",
769 IconRole::Shield => "shield.fill",
770
771 IconRole::WindowClose => "xmark",
773 IconRole::WindowMinimize => "minus",
774 IconRole::WindowMaximize => "arrow.up.left.and.arrow.down.right",
775 IconRole::WindowRestore => "arrow.down.right.and.arrow.up.left",
776
777 IconRole::ActionSave => "square.and.arrow.down",
779 IconRole::ActionDelete => "trash",
780 IconRole::ActionCopy => "doc.on.doc",
781 IconRole::ActionPaste => "doc.on.clipboard",
782 IconRole::ActionCut => "scissors",
783 IconRole::ActionUndo => "arrow.uturn.backward",
784 IconRole::ActionRedo => "arrow.uturn.forward",
785 IconRole::ActionSearch => "magnifyingglass",
786 IconRole::ActionSettings => "gearshape",
787 IconRole::ActionEdit => "pencil",
788 IconRole::ActionAdd => "plus",
789 IconRole::ActionRemove => "minus",
790 IconRole::ActionRefresh => "arrow.clockwise",
791 IconRole::ActionPrint => "printer",
792
793 IconRole::NavBack => "chevron.backward",
795 IconRole::NavForward => "chevron.forward",
796 IconRole::NavUp => "chevron.up",
797 IconRole::NavDown => "chevron.down",
798 IconRole::NavHome => "house",
799 IconRole::NavMenu => "line.horizontal.3",
800
801 IconRole::FileGeneric => "doc",
803 IconRole::FolderClosed => "folder",
804 IconRole::FolderOpen => return None,
806 IconRole::TrashEmpty => "trash",
807 IconRole::TrashFull => "trash.fill",
808
809 IconRole::StatusBusy => return None,
812 IconRole::StatusCheck => "checkmark",
813 IconRole::StatusError => "xmark.circle.fill",
814
815 IconRole::UserAccount => "person.fill",
817 IconRole::Notification => "bell.fill",
818 IconRole::Help => "questionmark.circle",
819 IconRole::Lock => "lock.fill",
820
821 _ => return None,
822 })
823}
824
825#[allow(unreachable_patterns)]
826fn segoe_name(role: IconRole) -> Option<&'static str> {
827 Some(match role {
828 IconRole::DialogWarning => "SIID_WARNING",
830 IconRole::DialogError => "SIID_ERROR",
831 IconRole::DialogInfo => "SIID_INFO",
832 IconRole::DialogQuestion => "IDI_QUESTION",
833 IconRole::DialogSuccess => "CheckMark",
834 IconRole::Shield => "SIID_SHIELD",
835
836 IconRole::WindowClose => "ChromeClose",
838 IconRole::WindowMinimize => "ChromeMinimize",
839 IconRole::WindowMaximize => "ChromeMaximize",
840 IconRole::WindowRestore => "ChromeRestore",
841
842 IconRole::ActionSave => "Save",
844 IconRole::ActionDelete => "SIID_DELETE",
845 IconRole::ActionCopy => "Copy",
846 IconRole::ActionPaste => "Paste",
847 IconRole::ActionCut => "Cut",
848 IconRole::ActionUndo => "Undo",
849 IconRole::ActionRedo => "Redo",
850 IconRole::ActionSearch => "SIID_FIND",
851 IconRole::ActionSettings => "SIID_SETTINGS",
852 IconRole::ActionEdit => "Edit",
853 IconRole::ActionAdd => "Add",
854 IconRole::ActionRemove => "Remove",
855 IconRole::ActionRefresh => "Refresh",
856 IconRole::ActionPrint => "SIID_PRINTER",
857
858 IconRole::NavBack => "Back",
860 IconRole::NavForward => "Forward",
861 IconRole::NavUp => "Up",
862 IconRole::NavDown => "Down",
863 IconRole::NavHome => "Home",
864 IconRole::NavMenu => "GlobalNavigationButton",
865
866 IconRole::FileGeneric => "SIID_DOCNOASSOC",
868 IconRole::FolderClosed => "SIID_FOLDER",
869 IconRole::FolderOpen => "SIID_FOLDEROPEN",
870 IconRole::TrashEmpty => "SIID_RECYCLER",
871 IconRole::TrashFull => "SIID_RECYCLERFULL",
872
873 IconRole::StatusBusy => return None,
876 IconRole::StatusCheck => "CheckMark",
877 IconRole::StatusError => "SIID_ERROR",
878
879 IconRole::UserAccount => "SIID_USERS",
881 IconRole::Notification => "Ringer",
882 IconRole::Help => "SIID_HELP",
883 IconRole::Lock => "SIID_LOCK",
884
885 _ => return None,
886 })
887}
888
889#[allow(unreachable_patterns)]
890fn freedesktop_name(role: IconRole) -> Option<&'static str> {
891 Some(match role {
892 IconRole::DialogWarning => "dialog-warning",
894 IconRole::DialogError => "dialog-error",
895 IconRole::DialogInfo => "dialog-information",
896 IconRole::DialogQuestion => "dialog-question",
897 IconRole::DialogSuccess => "object-select-symbolic",
901 IconRole::Shield => "security-high",
902
903 IconRole::WindowClose => "window-close",
905 IconRole::WindowMinimize => "window-minimize",
906 IconRole::WindowMaximize => "window-maximize",
907 IconRole::WindowRestore => "window-restore",
908
909 IconRole::ActionSave => "document-save",
911 IconRole::ActionDelete => "edit-delete",
912 IconRole::ActionCopy => "edit-copy",
913 IconRole::ActionPaste => "edit-paste",
914 IconRole::ActionCut => "edit-cut",
915 IconRole::ActionUndo => "edit-undo",
916 IconRole::ActionRedo => "edit-redo",
917 IconRole::ActionSearch => "edit-find",
918 IconRole::ActionSettings => "preferences-system",
919 IconRole::ActionEdit => "document-edit",
920 IconRole::ActionAdd => "list-add",
921 IconRole::ActionRemove => "list-remove",
922 IconRole::ActionRefresh => "view-refresh",
923 IconRole::ActionPrint => "document-print",
924
925 IconRole::NavBack => "go-previous",
927 IconRole::NavForward => "go-next",
928 IconRole::NavUp => "go-up",
929 IconRole::NavDown => "go-down",
930 IconRole::NavHome => "go-home",
931 IconRole::NavMenu => "open-menu",
932
933 IconRole::FileGeneric => "text-x-generic",
935 IconRole::FolderClosed => "folder",
936 IconRole::FolderOpen => "folder-open",
937 IconRole::TrashEmpty => "user-trash",
938 IconRole::TrashFull => "user-trash-full",
939
940 IconRole::StatusBusy => "content-loading-symbolic",
947 IconRole::StatusCheck => "emblem-default",
948 IconRole::StatusError => "dialog-error",
949
950 IconRole::UserAccount => "system-users",
952 IconRole::Notification => "notification-active",
954 IconRole::Help => "help-browser",
955 IconRole::Lock => "system-lock-screen",
956
957 _ => return None,
958 })
959}
960
961#[allow(unreachable_patterns)]
962fn material_name(role: IconRole) -> Option<&'static str> {
963 Some(match role {
964 IconRole::DialogWarning => "warning",
966 IconRole::DialogError => "error",
967 IconRole::DialogInfo => "info",
968 IconRole::DialogQuestion => "help",
969 IconRole::DialogSuccess => "check_circle",
970 IconRole::Shield => "shield",
971
972 IconRole::WindowClose => "close",
974 IconRole::WindowMinimize => "minimize",
975 IconRole::WindowMaximize => "open_in_full",
976 IconRole::WindowRestore => "close_fullscreen",
977
978 IconRole::ActionSave => "save",
980 IconRole::ActionDelete => "delete",
981 IconRole::ActionCopy => "content_copy",
982 IconRole::ActionPaste => "content_paste",
983 IconRole::ActionCut => "content_cut",
984 IconRole::ActionUndo => "undo",
985 IconRole::ActionRedo => "redo",
986 IconRole::ActionSearch => "search",
987 IconRole::ActionSettings => "settings",
988 IconRole::ActionEdit => "edit",
989 IconRole::ActionAdd => "add",
990 IconRole::ActionRemove => "remove",
991 IconRole::ActionRefresh => "refresh",
992 IconRole::ActionPrint => "print",
993
994 IconRole::NavBack => "arrow_back",
996 IconRole::NavForward => "arrow_forward",
997 IconRole::NavUp => "arrow_upward",
998 IconRole::NavDown => "arrow_downward",
999 IconRole::NavHome => "home",
1000 IconRole::NavMenu => "menu",
1001
1002 IconRole::FileGeneric => "description",
1004 IconRole::FolderClosed => "folder",
1005 IconRole::FolderOpen => "folder_open",
1006 IconRole::TrashEmpty => "delete",
1007 IconRole::TrashFull => "delete",
1009
1010 IconRole::StatusBusy => "progress_activity",
1012 IconRole::StatusCheck => "check",
1013 IconRole::StatusError => "error",
1014
1015 IconRole::UserAccount => "person",
1017 IconRole::Notification => "notifications",
1018 IconRole::Help => "help",
1019 IconRole::Lock => "lock",
1020
1021 _ => return None,
1022 })
1023}
1024
1025#[allow(unreachable_patterns)]
1026fn lucide_name(role: IconRole) -> Option<&'static str> {
1027 Some(match role {
1028 IconRole::DialogWarning => "triangle-alert",
1030 IconRole::DialogError => "circle-x",
1031 IconRole::DialogInfo => "info",
1032 IconRole::DialogQuestion => "circle-question-mark",
1033 IconRole::DialogSuccess => "circle-check",
1034 IconRole::Shield => "shield",
1035
1036 IconRole::WindowClose => "x",
1038 IconRole::WindowMinimize => "minimize",
1039 IconRole::WindowMaximize => "maximize",
1040 IconRole::WindowRestore => "minimize-2",
1041
1042 IconRole::ActionSave => "save",
1044 IconRole::ActionDelete => "trash-2",
1045 IconRole::ActionCopy => "copy",
1046 IconRole::ActionPaste => "clipboard-paste",
1047 IconRole::ActionCut => "scissors",
1048 IconRole::ActionUndo => "undo-2",
1049 IconRole::ActionRedo => "redo-2",
1050 IconRole::ActionSearch => "search",
1051 IconRole::ActionSettings => "settings",
1052 IconRole::ActionEdit => "pencil",
1053 IconRole::ActionAdd => "plus",
1054 IconRole::ActionRemove => "minus",
1055 IconRole::ActionRefresh => "refresh-cw",
1056 IconRole::ActionPrint => "printer",
1057
1058 IconRole::NavBack => "chevron-left",
1060 IconRole::NavForward => "chevron-right",
1061 IconRole::NavUp => "chevron-up",
1062 IconRole::NavDown => "chevron-down",
1063 IconRole::NavHome => "house",
1064 IconRole::NavMenu => "menu",
1065
1066 IconRole::FileGeneric => "file",
1068 IconRole::FolderClosed => "folder-closed",
1069 IconRole::FolderOpen => "folder-open",
1070 IconRole::TrashEmpty => "trash-2",
1071 IconRole::TrashFull => "trash-2",
1073
1074 IconRole::StatusBusy => "loader",
1076 IconRole::StatusCheck => "check",
1077 IconRole::StatusError => "circle-x",
1078
1079 IconRole::UserAccount => "user",
1081 IconRole::Notification => "bell",
1082 IconRole::Help => "circle-question-mark",
1083 IconRole::Lock => "lock",
1084
1085 _ => return None,
1086 })
1087}
1088
1089#[cfg(test)]
1090#[allow(clippy::unwrap_used, clippy::expect_used)]
1091mod tests {
1092 use super::*;
1093
1094 #[test]
1097 fn icon_role_all_has_42_variants() {
1098 assert_eq!(IconRole::ALL.len(), 42);
1099 }
1100
1101 #[test]
1102 fn icon_role_all_contains_every_variant() {
1103 use std::collections::HashSet;
1107 let all_set: HashSet<IconRole> = IconRole::ALL.iter().copied().collect();
1108 let check = |role: IconRole| {
1109 assert!(
1110 all_set.contains(&role),
1111 "IconRole::{role:?} missing from ALL array"
1112 );
1113 };
1114
1115 #[deny(unreachable_patterns)]
1117 match IconRole::DialogWarning {
1118 IconRole::DialogWarning
1119 | IconRole::DialogError
1120 | IconRole::DialogInfo
1121 | IconRole::DialogQuestion
1122 | IconRole::DialogSuccess
1123 | IconRole::Shield
1124 | IconRole::WindowClose
1125 | IconRole::WindowMinimize
1126 | IconRole::WindowMaximize
1127 | IconRole::WindowRestore
1128 | IconRole::ActionSave
1129 | IconRole::ActionDelete
1130 | IconRole::ActionCopy
1131 | IconRole::ActionPaste
1132 | IconRole::ActionCut
1133 | IconRole::ActionUndo
1134 | IconRole::ActionRedo
1135 | IconRole::ActionSearch
1136 | IconRole::ActionSettings
1137 | IconRole::ActionEdit
1138 | IconRole::ActionAdd
1139 | IconRole::ActionRemove
1140 | IconRole::ActionRefresh
1141 | IconRole::ActionPrint
1142 | IconRole::NavBack
1143 | IconRole::NavForward
1144 | IconRole::NavUp
1145 | IconRole::NavDown
1146 | IconRole::NavHome
1147 | IconRole::NavMenu
1148 | IconRole::FileGeneric
1149 | IconRole::FolderClosed
1150 | IconRole::FolderOpen
1151 | IconRole::TrashEmpty
1152 | IconRole::TrashFull
1153 | IconRole::StatusBusy
1154 | IconRole::StatusCheck
1155 | IconRole::StatusError
1156 | IconRole::UserAccount
1157 | IconRole::Notification
1158 | IconRole::Help
1159 | IconRole::Lock => {}
1160 }
1161
1162 check(IconRole::DialogWarning);
1164 check(IconRole::DialogError);
1165 check(IconRole::DialogInfo);
1166 check(IconRole::DialogQuestion);
1167 check(IconRole::DialogSuccess);
1168 check(IconRole::Shield);
1169 check(IconRole::WindowClose);
1170 check(IconRole::WindowMinimize);
1171 check(IconRole::WindowMaximize);
1172 check(IconRole::WindowRestore);
1173 check(IconRole::ActionSave);
1174 check(IconRole::ActionDelete);
1175 check(IconRole::ActionCopy);
1176 check(IconRole::ActionPaste);
1177 check(IconRole::ActionCut);
1178 check(IconRole::ActionUndo);
1179 check(IconRole::ActionRedo);
1180 check(IconRole::ActionSearch);
1181 check(IconRole::ActionSettings);
1182 check(IconRole::ActionEdit);
1183 check(IconRole::ActionAdd);
1184 check(IconRole::ActionRemove);
1185 check(IconRole::ActionRefresh);
1186 check(IconRole::ActionPrint);
1187 check(IconRole::NavBack);
1188 check(IconRole::NavForward);
1189 check(IconRole::NavUp);
1190 check(IconRole::NavDown);
1191 check(IconRole::NavHome);
1192 check(IconRole::NavMenu);
1193 check(IconRole::FileGeneric);
1194 check(IconRole::FolderClosed);
1195 check(IconRole::FolderOpen);
1196 check(IconRole::TrashEmpty);
1197 check(IconRole::TrashFull);
1198 check(IconRole::StatusBusy);
1199 check(IconRole::StatusCheck);
1200 check(IconRole::StatusError);
1201 check(IconRole::UserAccount);
1202 check(IconRole::Notification);
1203 check(IconRole::Help);
1204 check(IconRole::Lock);
1205 }
1206
1207 #[test]
1208 fn icon_role_all_no_duplicates() {
1209 let all = &IconRole::ALL;
1210 for (i, role) in all.iter().enumerate() {
1211 for (j, other) in all.iter().enumerate() {
1212 if i != j {
1213 assert_ne!(role, other, "Duplicate at index {i} and {j}");
1214 }
1215 }
1216 }
1217 }
1218
1219 #[test]
1220 fn icon_role_derives_copy_clone() {
1221 let role = IconRole::ActionCopy;
1222 let copied1 = role;
1223 let copied2 = role;
1224 assert_eq!(role, copied1);
1225 assert_eq!(role, copied2);
1226 }
1227
1228 #[test]
1229 fn icon_role_derives_debug() {
1230 let s = format!("{:?}", IconRole::DialogWarning);
1231 assert!(s.contains("DialogWarning"));
1232 }
1233
1234 #[test]
1235 fn icon_role_derives_hash() {
1236 use std::collections::HashSet;
1237 let mut set = HashSet::new();
1238 set.insert(IconRole::ActionSave);
1239 set.insert(IconRole::ActionDelete);
1240 assert_eq!(set.len(), 2);
1241 assert!(set.contains(&IconRole::ActionSave));
1242 }
1243
1244 #[test]
1247 fn icon_data_svg_construct_and_match() {
1248 let data = IconData::Svg(Cow::Borrowed(b"<svg></svg>"));
1249 assert_eq!(data.bytes(), b"<svg></svg>");
1250 match data {
1251 IconData::Svg(ref cow) => assert_eq!(cow.as_ref(), b"<svg></svg>"),
1252 _ => panic!("Expected Svg variant"),
1253 }
1254 }
1255
1256 #[test]
1257 fn icon_data_rgba_construct_and_match() {
1258 let pixels = vec![255, 0, 0, 255]; let data = IconData::Rgba {
1260 width: 1,
1261 height: 1,
1262 data: pixels.clone(),
1263 };
1264 match data {
1265 IconData::Rgba {
1266 width,
1267 height,
1268 data,
1269 } => {
1270 assert_eq!(width, 1);
1271 assert_eq!(height, 1);
1272 assert_eq!(data, pixels);
1273 }
1274 _ => panic!("Expected Rgba variant"),
1275 }
1276 }
1277
1278 #[test]
1279 fn icon_data_derives_debug() {
1280 let data = IconData::Svg(Cow::Borrowed(b""));
1281 let s = format!("{:?}", data);
1282 assert!(s.contains("Svg"));
1283 }
1284
1285 #[test]
1286 fn icon_data_derives_clone() {
1287 let data = IconData::Rgba {
1288 width: 16,
1289 height: 16,
1290 data: vec![0; 16 * 16 * 4],
1291 };
1292 let cloned = data.clone();
1293 assert_eq!(data, cloned);
1294 }
1295
1296 #[test]
1297 fn icon_data_derives_eq() {
1298 let a = IconData::Svg(Cow::Borrowed(b"<svg/>"));
1299 let b = IconData::Svg(Cow::Owned(b"<svg/>".to_vec()));
1300 assert_eq!(a, b);
1301
1302 let c = IconData::Svg(Cow::Borrowed(b"<other/>"));
1303 assert_ne!(a, c);
1304 }
1305
1306 #[test]
1309 fn icon_set_from_name_sf_symbols() {
1310 assert_eq!(IconSet::from_name("sf-symbols"), Some(IconSet::SfSymbols));
1311 }
1312
1313 #[test]
1314 fn icon_set_from_name_segoe_fluent() {
1315 assert_eq!(
1316 IconSet::from_name("segoe-fluent"),
1317 Some(IconSet::SegoeIcons)
1318 );
1319 }
1320
1321 #[test]
1322 fn icon_set_from_name_freedesktop() {
1323 assert_eq!(
1324 IconSet::from_name("freedesktop"),
1325 Some(IconSet::Freedesktop)
1326 );
1327 }
1328
1329 #[test]
1330 fn icon_set_from_name_material() {
1331 assert_eq!(IconSet::from_name("material"), Some(IconSet::Material));
1332 }
1333
1334 #[test]
1335 fn icon_set_from_name_lucide() {
1336 assert_eq!(IconSet::from_name("lucide"), Some(IconSet::Lucide));
1337 }
1338
1339 #[test]
1340 fn icon_set_from_name_unknown() {
1341 assert_eq!(IconSet::from_name("unknown"), None);
1342 }
1343
1344 #[test]
1345 fn icon_set_name_sf_symbols() {
1346 assert_eq!(IconSet::SfSymbols.name(), "sf-symbols");
1347 }
1348
1349 #[test]
1350 fn icon_set_name_segoe_fluent() {
1351 assert_eq!(IconSet::SegoeIcons.name(), "segoe-fluent");
1352 }
1353
1354 #[test]
1355 fn icon_set_name_freedesktop() {
1356 assert_eq!(IconSet::Freedesktop.name(), "freedesktop");
1357 }
1358
1359 #[test]
1360 fn icon_set_name_material() {
1361 assert_eq!(IconSet::Material.name(), "material");
1362 }
1363
1364 #[test]
1365 fn icon_set_name_lucide() {
1366 assert_eq!(IconSet::Lucide.name(), "lucide");
1367 }
1368
1369 #[test]
1370 fn icon_set_from_name_name_round_trip() {
1371 let sets = [
1372 IconSet::SfSymbols,
1373 IconSet::SegoeIcons,
1374 IconSet::Freedesktop,
1375 IconSet::Material,
1376 IconSet::Lucide,
1377 ];
1378 for set in &sets {
1379 let name = set.name();
1380 let parsed = IconSet::from_name(name);
1381 assert_eq!(parsed, Some(*set), "Round-trip failed for {:?}", set);
1382 }
1383 }
1384
1385 #[test]
1386 fn icon_set_derives_copy_clone() {
1387 let set = IconSet::Material;
1388 let copied1 = set;
1389 let copied2 = set;
1390 assert_eq!(set, copied1);
1391 assert_eq!(set, copied2);
1392 }
1393
1394 #[test]
1395 fn icon_set_derives_hash() {
1396 use std::collections::HashSet;
1397 let mut map = HashSet::new();
1398 map.insert(IconSet::SfSymbols);
1399 map.insert(IconSet::Lucide);
1400 assert_eq!(map.len(), 2);
1401 }
1402
1403 #[test]
1404 fn icon_set_derives_debug() {
1405 let s = format!("{:?}", IconSet::Freedesktop);
1406 assert!(s.contains("Freedesktop"));
1407 }
1408
1409 #[test]
1410 fn icon_set_serde_round_trip() {
1411 let set = IconSet::SfSymbols;
1412 let json = serde_json::to_string(&set).unwrap();
1413 let deserialized: IconSet = serde_json::from_str(&json).unwrap();
1414 assert_eq!(set, deserialized);
1415 }
1416
1417 #[test]
1420 fn icon_name_sf_symbols_action_copy() {
1421 assert_eq!(
1422 icon_name(IconRole::ActionCopy, IconSet::SfSymbols),
1423 Some("doc.on.doc")
1424 );
1425 }
1426
1427 #[test]
1428 fn icon_name_segoe_action_copy() {
1429 assert_eq!(
1430 icon_name(IconRole::ActionCopy, IconSet::SegoeIcons),
1431 Some("Copy")
1432 );
1433 }
1434
1435 #[test]
1436 fn icon_name_freedesktop_action_copy() {
1437 assert_eq!(
1438 icon_name(IconRole::ActionCopy, IconSet::Freedesktop),
1439 Some("edit-copy")
1440 );
1441 }
1442
1443 #[test]
1444 fn icon_name_material_action_copy() {
1445 assert_eq!(
1446 icon_name(IconRole::ActionCopy, IconSet::Material),
1447 Some("content_copy")
1448 );
1449 }
1450
1451 #[test]
1452 fn icon_name_lucide_action_copy() {
1453 assert_eq!(
1454 icon_name(IconRole::ActionCopy, IconSet::Lucide),
1455 Some("copy")
1456 );
1457 }
1458
1459 #[test]
1460 fn icon_name_sf_symbols_dialog_warning() {
1461 assert_eq!(
1462 icon_name(IconRole::DialogWarning, IconSet::SfSymbols),
1463 Some("exclamationmark.triangle.fill")
1464 );
1465 }
1466
1467 #[test]
1469 fn icon_name_sf_symbols_folder_open_is_none() {
1470 assert_eq!(icon_name(IconRole::FolderOpen, IconSet::SfSymbols), None);
1471 }
1472
1473 #[test]
1474 fn icon_name_sf_symbols_trash_full() {
1475 assert_eq!(
1476 icon_name(IconRole::TrashFull, IconSet::SfSymbols),
1477 Some("trash.fill")
1478 );
1479 }
1480
1481 #[test]
1482 fn icon_name_sf_symbols_status_busy_is_none() {
1483 assert_eq!(icon_name(IconRole::StatusBusy, IconSet::SfSymbols), None);
1484 }
1485
1486 #[test]
1487 fn icon_name_sf_symbols_window_restore() {
1488 assert_eq!(
1489 icon_name(IconRole::WindowRestore, IconSet::SfSymbols),
1490 Some("arrow.down.right.and.arrow.up.left")
1491 );
1492 }
1493
1494 #[test]
1495 fn icon_name_segoe_dialog_success() {
1496 assert_eq!(
1497 icon_name(IconRole::DialogSuccess, IconSet::SegoeIcons),
1498 Some("CheckMark")
1499 );
1500 }
1501
1502 #[test]
1503 fn icon_name_segoe_status_busy_is_none() {
1504 assert_eq!(icon_name(IconRole::StatusBusy, IconSet::SegoeIcons), None);
1505 }
1506
1507 #[test]
1508 fn icon_name_freedesktop_notification() {
1509 assert_eq!(
1510 icon_name(IconRole::Notification, IconSet::Freedesktop),
1511 Some("notification-active")
1512 );
1513 }
1514
1515 #[test]
1516 fn icon_name_freedesktop_dialog_success() {
1517 assert_eq!(
1521 icon_name(IconRole::DialogSuccess, IconSet::Freedesktop),
1522 Some("object-select-symbolic")
1523 );
1524 }
1525
1526 #[test]
1527 fn icon_name_freedesktop_status_busy() {
1528 assert_eq!(
1532 icon_name(IconRole::StatusBusy, IconSet::Freedesktop),
1533 Some("content-loading-symbolic")
1534 );
1535 }
1536
1537 #[test]
1538 fn icon_name_material_trash_full() {
1539 assert_eq!(
1540 icon_name(IconRole::TrashFull, IconSet::Material),
1541 Some("delete")
1542 );
1543 }
1544
1545 #[test]
1546 fn icon_name_lucide_trash_full() {
1547 assert_eq!(
1548 icon_name(IconRole::TrashFull, IconSet::Lucide),
1549 Some("trash-2")
1550 );
1551 }
1552
1553 #[test]
1555 fn icon_name_spot_check_dialog_error() {
1556 assert_eq!(
1557 icon_name(IconRole::DialogError, IconSet::SfSymbols),
1558 Some("xmark.circle.fill")
1559 );
1560 assert_eq!(
1561 icon_name(IconRole::DialogError, IconSet::SegoeIcons),
1562 Some("SIID_ERROR")
1563 );
1564 assert_eq!(
1565 icon_name(IconRole::DialogError, IconSet::Freedesktop),
1566 Some("dialog-error")
1567 );
1568 assert_eq!(
1569 icon_name(IconRole::DialogError, IconSet::Material),
1570 Some("error")
1571 );
1572 assert_eq!(
1573 icon_name(IconRole::DialogError, IconSet::Lucide),
1574 Some("circle-x")
1575 );
1576 }
1577
1578 #[test]
1579 fn icon_name_spot_check_nav_home() {
1580 assert_eq!(
1581 icon_name(IconRole::NavHome, IconSet::SfSymbols),
1582 Some("house")
1583 );
1584 assert_eq!(
1585 icon_name(IconRole::NavHome, IconSet::SegoeIcons),
1586 Some("Home")
1587 );
1588 assert_eq!(
1589 icon_name(IconRole::NavHome, IconSet::Freedesktop),
1590 Some("go-home")
1591 );
1592 assert_eq!(
1593 icon_name(IconRole::NavHome, IconSet::Material),
1594 Some("home")
1595 );
1596 assert_eq!(icon_name(IconRole::NavHome, IconSet::Lucide), Some("house"));
1597 }
1598
1599 #[test]
1601 fn icon_name_sf_symbols_expected_count() {
1602 let some_count = IconRole::ALL
1604 .iter()
1605 .filter(|r| icon_name(**r, IconSet::SfSymbols).is_some())
1606 .count();
1607 assert_eq!(some_count, 40, "SF Symbols should have 40 mappings");
1608 }
1609
1610 #[test]
1611 fn icon_name_segoe_expected_count() {
1612 let some_count = IconRole::ALL
1614 .iter()
1615 .filter(|r| icon_name(**r, IconSet::SegoeIcons).is_some())
1616 .count();
1617 assert_eq!(some_count, 41, "Segoe Icons should have 41 mappings");
1618 }
1619
1620 #[test]
1621 fn icon_name_freedesktop_expected_count() {
1622 let some_count = IconRole::ALL
1624 .iter()
1625 .filter(|r| icon_name(**r, IconSet::Freedesktop).is_some())
1626 .count();
1627 assert_eq!(some_count, 42, "Freedesktop should have 42 mappings");
1628 }
1629
1630 #[test]
1631 fn icon_name_material_expected_count() {
1632 let some_count = IconRole::ALL
1634 .iter()
1635 .filter(|r| icon_name(**r, IconSet::Material).is_some())
1636 .count();
1637 assert_eq!(some_count, 42, "Material should have 42 mappings");
1638 }
1639
1640 #[test]
1641 fn icon_name_lucide_expected_count() {
1642 let some_count = IconRole::ALL
1644 .iter()
1645 .filter(|r| icon_name(**r, IconSet::Lucide).is_some())
1646 .count();
1647 assert_eq!(some_count, 42, "Lucide should have 42 mappings");
1648 }
1649
1650 #[test]
1653 #[cfg(target_os = "linux")]
1654 fn system_icon_set_returns_freedesktop_on_linux() {
1655 assert_eq!(system_icon_set(), IconSet::Freedesktop);
1656 }
1657
1658 #[test]
1659 fn system_icon_theme_returns_non_empty() {
1660 let theme = system_icon_theme();
1661 assert!(
1662 !theme.is_empty(),
1663 "system_icon_theme() should return a non-empty string"
1664 );
1665 }
1666
1667 #[test]
1670 fn icon_provider_is_object_safe() {
1671 let provider: Box<dyn IconProvider> = Box::new(IconRole::ActionCopy);
1673 let debug_str = format!("{:?}", provider);
1674 assert!(
1675 debug_str.contains("ActionCopy"),
1676 "Debug should print variant name"
1677 );
1678 }
1679
1680 #[test]
1681 fn icon_role_provider_icon_name() {
1682 let role = IconRole::ActionCopy;
1684 let name = IconProvider::icon_name(&role, IconSet::Material);
1685 assert_eq!(name, Some("content_copy"));
1686 }
1687
1688 #[test]
1689 fn icon_role_provider_icon_name_sf_symbols() {
1690 let role = IconRole::ActionCopy;
1691 let name = IconProvider::icon_name(&role, IconSet::SfSymbols);
1692 assert_eq!(name, Some("doc.on.doc"));
1693 }
1694
1695 #[test]
1696 #[cfg(feature = "material-icons")]
1697 fn icon_role_provider_icon_svg_material() {
1698 let role = IconRole::ActionCopy;
1699 let svg = IconProvider::icon_svg(&role, IconSet::Material);
1700 assert!(svg.is_some(), "Material SVG should be Some");
1701 let cow = svg.unwrap();
1702 let content = std::str::from_utf8(&cow).expect("valid UTF-8");
1703 assert!(content.contains("<svg"), "should contain <svg tag");
1704 }
1705
1706 #[test]
1707 fn icon_role_provider_icon_svg_non_bundled() {
1708 let role = IconRole::ActionCopy;
1710 let svg = IconProvider::icon_svg(&role, IconSet::SfSymbols);
1711 assert!(svg.is_none(), "SfSymbols should not have bundled SVGs");
1712 }
1713
1714 #[test]
1715 fn icon_role_provider_all_roles() {
1716 for role in IconRole::ALL {
1718 let _name = IconProvider::icon_name(&role, IconSet::Material);
1720 }
1722 }
1723
1724 #[test]
1725 fn icon_provider_dyn_dispatch() {
1726 let role = IconRole::ActionCopy;
1728 let provider: &dyn IconProvider = &role;
1729 let name = provider.icon_name(IconSet::Material);
1730 assert_eq!(name, Some("content_copy"));
1731 let svg = provider.icon_svg(IconSet::SfSymbols);
1732 assert!(svg.is_none(), "SfSymbols should not have bundled SVGs");
1733 }
1734
1735 fn known_gaps() -> &'static [(IconSet, IconRole)] {
1738 &[
1739 (IconSet::SfSymbols, IconRole::FolderOpen),
1740 (IconSet::SfSymbols, IconRole::StatusBusy),
1741 (IconSet::SegoeIcons, IconRole::StatusBusy),
1742 ]
1743 }
1744
1745 #[test]
1746 fn no_unexpected_icon_gaps() {
1747 let gaps = known_gaps();
1748 let system_sets = [
1749 IconSet::SfSymbols,
1750 IconSet::SegoeIcons,
1751 IconSet::Freedesktop,
1752 ];
1753 for &set in &system_sets {
1754 for role in IconRole::ALL {
1755 let is_known_gap = gaps.contains(&(set, role));
1756 let is_mapped = icon_name(role, set).is_some();
1757 if !is_known_gap {
1758 assert!(
1759 is_mapped,
1760 "{role:?} has no mapping for {set:?} and is not in known_gaps()"
1761 );
1762 }
1763 }
1764 }
1765 }
1766
1767 #[test]
1768 #[cfg(all(feature = "material-icons", feature = "lucide-icons"))]
1769 fn all_roles_have_bundled_svg() {
1770 use crate::bundled_icon_svg;
1771 for set in [IconSet::Material, IconSet::Lucide] {
1772 for role in IconRole::ALL {
1773 assert!(
1774 bundled_icon_svg(role, set).is_some(),
1775 "{role:?} has no bundled SVG for {set:?}"
1776 );
1777 }
1778 }
1779 }
1780
1781 #[test]
1784 fn icon_name_exhaustive_all_sets() {
1785 for role in IconRole::ALL {
1787 assert!(
1788 icon_name(role, IconSet::Material).is_some(),
1789 "Material must map {role:?} to Some"
1790 );
1791 }
1792 for role in IconRole::ALL {
1794 assert!(
1795 icon_name(role, IconSet::Lucide).is_some(),
1796 "Lucide must map {role:?} to Some"
1797 );
1798 }
1799 for role in IconRole::ALL {
1801 assert!(
1802 icon_name(role, IconSet::Freedesktop).is_some(),
1803 "Freedesktop must map {role:?} to Some"
1804 );
1805 }
1806 let segoe_count = IconRole::ALL
1808 .iter()
1809 .filter(|r| icon_name(**r, IconSet::SegoeIcons).is_some())
1810 .count();
1811 assert!(
1812 segoe_count >= 41,
1813 "Segoe should map at least 41 roles, got {segoe_count}"
1814 );
1815 let sf_count = IconRole::ALL
1817 .iter()
1818 .filter(|r| icon_name(**r, IconSet::SfSymbols).is_some())
1819 .count();
1820 assert!(
1821 sf_count >= 40,
1822 "SfSymbols should map at least 40 roles, got {sf_count}"
1823 );
1824 }
1825
1826 #[test]
1828 fn icon_name_returns_nonempty_strings() {
1829 let all_sets = [
1830 IconSet::SfSymbols,
1831 IconSet::SegoeIcons,
1832 IconSet::Freedesktop,
1833 IconSet::Material,
1834 IconSet::Lucide,
1835 ];
1836 for set in all_sets {
1837 for role in IconRole::ALL {
1838 if let Some(name) = icon_name(role, set) {
1839 assert!(
1840 !name.is_empty(),
1841 "icon_name({role:?}, {set:?}) returned empty string"
1842 );
1843 }
1844 }
1845 }
1846 }
1847
1848 #[test]
1851 fn icon_set_name_round_trip() {
1852 let all_sets = [
1855 IconSet::SfSymbols,
1856 IconSet::SegoeIcons,
1857 IconSet::Freedesktop,
1858 IconSet::Material,
1859 IconSet::Lucide,
1860 ];
1861 for set in all_sets {
1862 assert_eq!(
1863 IconSet::from_name(set.name()),
1864 Some(set),
1865 "IconSet::{:?}.name() = {:?} did not round-trip through from_name()",
1866 set,
1867 set.name()
1868 );
1869 }
1870 }
1871
1872 #[test]
1875 fn icon_role_name_returns_kebab_case() {
1876 assert_eq!(IconRole::ActionSave.name(), "action-save");
1877 assert_eq!(IconRole::DialogWarning.name(), "dialog-warning");
1878 assert_eq!(IconRole::WindowClose.name(), "window-close");
1879 assert_eq!(IconRole::NavBack.name(), "nav-back");
1880 assert_eq!(IconRole::FileGeneric.name(), "file-generic");
1881 assert_eq!(IconRole::StatusBusy.name(), "status-busy");
1882 assert_eq!(IconRole::UserAccount.name(), "user-account");
1883 assert_eq!(IconRole::Shield.name(), "shield");
1884 assert_eq!(IconRole::Notification.name(), "notification");
1885 assert_eq!(IconRole::Help.name(), "help");
1886 assert_eq!(IconRole::Lock.name(), "lock");
1887 }
1888
1889 #[test]
1890 fn icon_role_display_delegates_to_name() {
1891 for role in IconRole::ALL {
1892 assert_eq!(
1893 format!("{role}"),
1894 role.name(),
1895 "Display for IconRole::{:?} should delegate to name()",
1896 role
1897 );
1898 }
1899 }
1900
1901 #[test]
1904 fn icon_set_serde_matches_name() {
1905 let all_sets = [
1906 IconSet::SfSymbols,
1907 IconSet::SegoeIcons,
1908 IconSet::Freedesktop,
1909 IconSet::Material,
1910 IconSet::Lucide,
1911 ];
1912 for set in all_sets {
1913 let serialized = serde_json::to_string(&set)
1914 .unwrap_or_else(|e| panic!("failed to serialize {set:?}: {e}"));
1915 let trimmed = serialized.trim_matches('"');
1916 assert_eq!(
1917 trimmed,
1918 set.name(),
1919 "serde serialization of {set:?} ({trimmed:?}) does not match name() ({:?})",
1920 set.name()
1921 );
1922 }
1923 }
1924
1925 #[test]
1928 fn icon_data_bytes_accessor() {
1929 let svg = IconData::Svg(Cow::Borrowed(b"<svg/>"));
1930 assert_eq!(svg.bytes(), b"<svg/>");
1931
1932 let rgba = IconData::Rgba {
1933 width: 2,
1934 height: 2,
1935 data: vec![1, 2, 3, 4],
1936 };
1937 assert_eq!(rgba.bytes(), &[1, 2, 3, 4]);
1938 }
1939}