use serde::{Deserialize, Serialize};
use zvariant::Type;
use crate::AtspiError;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type, Hash)]
#[repr(u32)]
pub enum Role {
Invalid,
AcceleratorLabel,
Alert,
Animation,
Arrow,
Calendar,
Canvas,
CheckBox,
CheckMenuItem,
ColorChooser,
ColumnHeader,
ComboBox,
DateEditor,
DesktopIcon,
DesktopFrame,
Dial,
Dialog,
DirectoryPane,
DrawingArea,
FileChooser,
Filler,
FocusTraversable,
FontChooser,
Frame,
GlassPane,
HTMLContainer,
Icon,
Image,
InternalFrame,
Label,
LayeredPane,
List,
ListItem,
Menu,
MenuBar,
MenuItem,
OptionPane,
PageTab,
PageTabList,
Panel,
PasswordText,
PopupMenu,
ProgressBar,
PushButton,
RadioButton,
RadioMenuItem,
RootPane,
RowHeader,
ScrollBar,
ScrollPane,
Separator,
Slider,
SpinButton,
SplitPane,
StatusBar,
Table,
TableCell,
TableColumnHeader,
TableRowHeader,
TearoffMenuItem,
Terminal,
Text,
ToggleButton,
ToolBar,
ToolTip,
Tree,
TreeTable,
Unknown,
Viewport,
Window,
Extended,
Header,
Footer,
Paragraph,
Ruler,
Application,
Autocomplete,
Editbar,
Embedded,
Entry,
CHART,
Caption,
DocumentFrame,
Heading,
Page,
Section,
RedundantObject,
Form,
Link,
InputMethodWindow,
TableRow,
TreeItem,
DocumentSpreadsheet,
DocumentPresentation,
DocumentText,
DocumentWeb,
DocumentEmail,
Comment,
ListBox,
Grouping,
ImageMap,
Notification,
InfoBar,
LevelBar,
TitleBar,
BlockQuote,
Audio,
Video,
Definition,
Article,
Landmark,
Log,
Marquee,
Math,
Rating,
Timer,
Static,
MathFraction,
MathRoot,
Subscript,
Superscript,
DescriptionList,
DescriptionTerm,
DescriptionValue,
Footnote,
ContentDeletion,
ContentInsertion,
Mark,
Suggestion,
PushButtonMenu,
}
impl TryFrom<u32> for Role {
type Error = AtspiError;
#[allow(clippy::too_many_lines)]
fn try_from(value: u32) -> Result<Self, Self::Error> {
#[allow(clippy::enum_glob_use)]
use Role::*;
let res = match value {
0 => Invalid,
1 => AcceleratorLabel,
2 => Alert,
3 => Animation,
4 => Arrow,
5 => Calendar,
6 => Canvas,
7 => CheckBox,
8 => CheckMenuItem,
9 => ColorChooser,
10 => ColumnHeader,
11 => ComboBox,
12 => DateEditor,
13 => DesktopIcon,
14 => DesktopFrame,
15 => Dial,
16 => Dialog,
17 => DirectoryPane,
18 => DrawingArea,
19 => FileChooser,
20 => Filler,
21 => FocusTraversable,
22 => FontChooser,
23 => Frame,
24 => GlassPane,
25 => HTMLContainer,
26 => Icon,
27 => Image,
28 => InternalFrame,
29 => Label,
30 => LayeredPane,
31 => List,
32 => ListItem,
33 => Menu,
34 => MenuBar,
35 => MenuItem,
36 => OptionPane,
37 => PageTab,
38 => PageTabList,
39 => Panel,
40 => PasswordText,
41 => PopupMenu,
42 => ProgressBar,
43 => PushButton,
44 => RadioButton,
45 => RadioMenuItem,
46 => RootPane,
47 => RowHeader,
48 => ScrollBar,
49 => ScrollPane,
50 => Separator,
51 => Slider,
52 => SpinButton,
53 => SplitPane,
54 => StatusBar,
55 => Table,
56 => TableCell,
57 => TableColumnHeader,
58 => TableRowHeader,
59 => TearoffMenuItem,
60 => Terminal,
61 => Text,
62 => ToggleButton,
63 => ToolBar,
64 => ToolTip,
65 => Tree,
66 => TreeTable,
67 => Unknown,
68 => Viewport,
69 => Window,
70 => Extended,
71 => Header,
72 => Footer,
73 => Paragraph,
74 => Ruler,
75 => Application,
76 => Autocomplete,
77 => Editbar,
78 => Embedded,
79 => Entry,
80 => CHART,
81 => Caption,
82 => DocumentFrame,
83 => Heading,
84 => Page,
85 => Section,
86 => RedundantObject,
87 => Form,
88 => Link,
89 => InputMethodWindow,
90 => TableRow,
91 => TreeItem,
92 => DocumentSpreadsheet,
93 => DocumentPresentation,
94 => DocumentText,
95 => DocumentWeb,
96 => DocumentEmail,
97 => Comment,
98 => ListBox,
99 => Grouping,
100 => ImageMap,
101 => Notification,
102 => InfoBar,
103 => LevelBar,
104 => TitleBar,
105 => BlockQuote,
106 => Audio,
107 => Video,
108 => Definition,
109 => Article,
110 => Landmark,
111 => Log,
112 => Marquee,
113 => Math,
114 => Rating,
115 => Timer,
116 => Static,
117 => MathFraction,
118 => MathRoot,
119 => Subscript,
120 => Superscript,
121 => DescriptionList,
122 => DescriptionTerm,
123 => DescriptionValue,
124 => Footnote,
125 => ContentDeletion,
126 => ContentInsertion,
127 => Mark,
128 => Suggestion,
129 => PushButtonMenu,
_ => return Err(AtspiError::UnknownRole(value)),
};
Ok(res)
}
}
#[cfg(test)]
pub mod tests {
use super::Role;
use zvariant::{from_slice, to_bytes, EncodingContext};
const HIGHEST_ROLE_VALUE: u32 = 129;
#[test]
fn test_serialization_matches_from_impl() {
let ctxt = EncodingContext::<byteorder::LE>::new_dbus(0);
for role_num in 1..=HIGHEST_ROLE_VALUE {
let from_role = Role::try_from(role_num)
.unwrap_or_else(|_| panic!("Unable to convert {role_num} into Role"));
let encoded = to_bytes(ctxt, &from_role).expect("Unable to encode {from_role}");
println!("ENCODED: {encoded:?}");
let zbus_role: Role =
from_slice(&encoded, ctxt).expect("Unable to convert {encoded} into Role");
assert_eq!(from_role, zbus_role, "The serde zvariant::from_slice(...) and From<u32> implementations have produced different results. The number used was {role_num}, it produced a Role of {from_role}, but the from_slice(...) implementation produced {zbus_role}");
assert_eq!(
from_role as u32, role_num,
"The role number {role_num} does not match the representation of the role {}",
from_role as u32
);
}
}
}
const ROLE_NAMES: &[&str] = &[
"invalid",
"accelerator label",
"alert",
"animation",
"arrow",
"calendar",
"canvas",
"check box",
"check menu item",
"color chooser",
"column header",
"combo box",
"date editor",
"desktop icon",
"desktop frame",
"dial",
"dialog",
"directory pane",
"drawing area",
"file chooser",
"filler",
"focus traversable",
"font chooser",
"frame",
"glass pane",
"html container",
"icon",
"image",
"internal frame",
"label",
"layered pane",
"list",
"list item",
"menu",
"menu bar",
"menu item",
"option pane",
"page tab",
"page tab list",
"panel",
"password text",
"popup menu",
"progress bar",
"push button",
"radio button",
"radio menu item",
"root pane",
"row header",
"scroll bar",
"scroll pane",
"separator",
"slider",
"spin button",
"split pane",
"status bar",
"table",
"table cell",
"table column header",
"table row header",
"tearoff menu item",
"terminal",
"text",
"toggle button",
"tool bar",
"tool tip",
"tree",
"tree table",
"unknown",
"viewport",
"window",
"extended",
"header",
"footer",
"paragraph",
"ruler",
"application",
"autocomplete",
"editbar",
"embedded",
"entry",
"chart",
"caption",
"document frame",
"heading",
"page",
"section",
"redundant object",
"form",
"link",
"input method window",
"table row",
"tree item",
"document spreadsheet",
"document presentation",
"document text",
"document web",
"document email",
"comment",
"list box",
"grouping",
"image map",
"notification",
"info bar",
"level bar",
"title bar",
"block quote",
"audio",
"video",
"definition",
"article",
"landmark",
"log",
"marquee",
"math",
"rating",
"timer",
"static",
"math fraction",
"math root",
"subscript",
"superscript",
"description list",
"description term",
"description value",
"footnote",
"content deletion",
"content insertion",
"mark",
"suggestion",
"push button menu",
];
impl Role {
#[must_use]
pub fn name(&self) -> &'static str {
ROLE_NAMES[*self as usize]
}
}
impl std::fmt::Display for Role {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}