anathema_widgets/layout/
display.rs1use anathema_value_resolver::ValueKind;
2
3pub const DISPLAY: &str = "display";
4
5#[derive(Debug, Copy, Clone, Default, PartialEq)]
6pub enum Display {
7 #[default]
8 Show,
9 Hide,
10 Exclude,
11}
12
13impl TryFrom<&ValueKind<'_>> for Display {
14 type Error = ();
15
16 fn try_from(value: &ValueKind<'_>) -> Result<Self, Self::Error> {
17 let Some(s) = value.as_str() else { return Err(()) };
18 let disp = match s {
19 "show" => Self::Show,
20 "hide" => Self::Hide,
21 "exclude" => Self::Exclude,
22 _ => return Err(()),
23 };
24 Ok(disp)
25 }
26}
27
28impl From<Display> for ValueKind<'_> {
29 fn from(value: Display) -> Self {
30 let value = match value {
31 Display::Show => "show",
32 Display::Hide => "hide",
33 Display::Exclude => "exclude",
34 };
35 ValueKind::Str(value.into())
36 }
37}
38
39#[cfg(test)]
40mod test {
41 use anathema_value_resolver::Attributes;
42
43 use super::*;
44
45 #[test]
46 fn to_and_from_attributes() {
47 let mut attribs = Attributes::empty();
48 attribs.set("disp", Display::Show);
49 assert_eq!(Display::Show, attribs.get_as::<Display>("disp").unwrap());
50
51 attribs.set("disp", Display::Hide);
52 assert_eq!(Display::Hide, attribs.get_as::<Display>("disp").unwrap());
53
54 attribs.set("disp", Display::Exclude);
55 assert_eq!(Display::Exclude, attribs.get_as::<Display>("disp").unwrap());
56 }
57}