use serde::{Deserialize, Serialize};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct A11yInfo {
#[serde(default)]
pub a11y_role: Option<String>,
#[serde(default)]
pub a11y_label: Option<String>,
}
impl A11yInfo {
pub fn new() -> Self {
Self::default()
}
pub fn with_role(mut self, role: impl Into<String>) -> Self {
self.a11y_role = Some(role.into());
self
}
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.a11y_label = Some(label.into());
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_empty() {
let info = A11yInfo::default();
assert_eq!(info.a11y_role, None);
assert_eq!(info.a11y_label, None);
}
#[test]
fn builders_set_fields() {
let info = A11yInfo::new().with_role("button").with_label("Save");
assert_eq!(info.a11y_role.as_deref(), Some("button"));
assert_eq!(info.a11y_label.as_deref(), Some("Save"));
}
#[test]
fn deserializes_from_empty_json() {
let info: A11yInfo = serde_json::from_str("{}").unwrap();
assert_eq!(info, A11yInfo::default());
}
#[test]
fn round_trips_through_json() {
let info = A11yInfo::new().with_role("tab").with_label("Untitled-1");
let json = serde_json::to_string(&info).unwrap();
let back: A11yInfo = serde_json::from_str(&json).unwrap();
assert_eq!(info, back);
}
}