use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Role {
Button,
TextInput,
Heading,
Checkbox,
Label,
Link,
MenuItem,
Tab,
Image,
Slider,
Group,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SemanticsFlags {
pub checked: Option<bool>,
pub pressed: Option<bool>,
pub expanded: Option<bool>,
pub disabled: Option<bool>,
pub readonly: Option<bool>,
pub required: Option<bool>,
}
impl SemanticsFlags {
pub const EMPTY: Self = Self {
checked: None,
pressed: None,
expanded: None,
disabled: None,
readonly: None,
required: None,
};
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct SemanticsSpec {
pub role: Option<Role>,
pub label: Option<Arc<str>>,
pub description: Option<Arc<str>>,
pub value: Option<Arc<str>>,
pub flags: SemanticsFlags,
}
impl SemanticsSpec {
pub fn role(role: Role) -> Self {
Self {
role: Some(role),
..Self::default()
}
}
pub fn with_label(mut self, s: impl Into<Arc<str>>) -> Self {
self.label = Some(s.into());
self
}
pub fn with_description(mut self, s: impl Into<Arc<str>>) -> Self {
self.description = Some(s.into());
self
}
pub fn with_value(mut self, s: impl Into<Arc<str>>) -> Self {
self.value = Some(s.into());
self
}
pub fn with_checked(mut self, v: bool) -> Self {
self.flags.checked = Some(v);
self
}
pub fn with_pressed(mut self, v: bool) -> Self {
self.flags.pressed = Some(v);
self
}
pub fn with_expanded(mut self, v: bool) -> Self {
self.flags.expanded = Some(v);
self
}
pub fn with_disabled(mut self, v: bool) -> Self {
self.flags.disabled = Some(v);
self
}
pub fn with_readonly(mut self, v: bool) -> Self {
self.flags.readonly = Some(v);
self
}
pub fn with_required(mut self, v: bool) -> Self {
self.flags.required = Some(v);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_es_todo_none_y_flags_empty() {
let s = SemanticsSpec::default();
assert!(s.role.is_none());
assert!(s.label.is_none());
assert!(s.value.is_none());
assert_eq!(s.flags, SemanticsFlags::EMPTY);
}
#[test]
fn role_builder_pone_solo_el_rol() {
let s = SemanticsSpec::role(Role::Button);
assert_eq!(s.role, Some(Role::Button));
assert!(s.label.is_none());
assert!(s.value.is_none());
assert_eq!(s.flags, SemanticsFlags::EMPTY);
}
#[test]
fn with_label_y_with_value_componen() {
let s = SemanticsSpec::role(Role::Slider)
.with_label("Volumen")
.with_value("70");
assert_eq!(s.role, Some(Role::Slider));
assert_eq!(s.label.as_deref(), Some("Volumen"));
assert_eq!(s.value.as_deref(), Some("70"));
}
#[test]
fn flags_con_with_son_independientes() {
let s = SemanticsSpec::role(Role::Checkbox)
.with_checked(true)
.with_required(true);
assert_eq!(s.flags.checked, Some(true));
assert_eq!(s.flags.required, Some(true));
assert!(s.flags.disabled.is_none(), "no setear flags no tocados");
}
}