android_manifest/
var_or_bool.rs1use crate::xml::{XmlDeserialize, XmlSerialize};
2use serde::{
3 Deserialize, Deserializer, Serialize, Serializer,
4 de::{self, Visitor},
5};
6use std::fmt;
7use std::io::{Read, Write};
8
9#[derive(Debug, PartialEq, Eq, Clone)]
11pub enum VarOrBool {
12 Var(String),
13 Bool(bool),
14}
15
16impl Default for VarOrBool {
17 fn default() -> Self {
18 Self::bool(false)
19 }
20}
21
22impl From<bool> for VarOrBool {
23 fn from(value: bool) -> Self {
24 Self::bool(value)
25 }
26}
27
28impl From<&str> for VarOrBool {
29 fn from(value: &str) -> Self {
30 Self::var(value)
31 }
32}
33
34impl VarOrBool {
35 pub fn var(name: impl Into<String>) -> VarOrBool {
36 Self::Var(name.into())
37 }
38
39 pub fn bool(s: bool) -> VarOrBool {
40 Self::Bool(s)
41 }
42}
43
44impl fmt::Display for VarOrBool {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match self {
47 Self::Var(r) => write!(f, "{}", r),
48 Self::Bool(v) => write!(f, "{}", v),
49 }
50 }
51}
52
53impl Serialize for VarOrBool {
54 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
55 where
56 S: Serializer,
57 {
58 match self {
59 VarOrBool::Var(variable) => Serialize::serialize(&variable, serializer),
60 VarOrBool::Bool(value) => serializer.serialize_bool(*value),
61 }
62 }
63}
64
65impl XmlSerialize for VarOrBool {
66 fn serialize<W: Write>(
67 &self,
68 writer: &mut crate::xml::ser::Serializer<W>,
69 ) -> Result<(), String> {
70 writer
71 .write(xml::writer::XmlEvent::characters(&self.to_string()))
72 .map_err(|error| error.to_string())
73 }
74}
75
76struct VarOrBoolVisitor;
77
78impl<'de> Visitor<'de> for VarOrBoolVisitor {
79 type Value = VarOrBool;
80
81 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
82 formatter.write_str("a boolean value or a variable in the \"${variable}\" format")
83 }
84
85 fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
86 where
87 E: de::Error,
88 {
89 Ok(v.into())
90 }
91
92 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
93 where
94 E: de::Error,
95 {
96 if v.is_empty() {
97 return Err(E::custom("value of attribute is empty"));
98 };
99 if v.starts_with("${") && v.ends_with('}') {
100 Ok(VarOrBool::var(v))
101 } else {
102 Ok(VarOrBool::Bool(v.parse().map_err(|_| {
103 E::custom(format!("value `{v}` is not a valid boolean"))
104 })?))
105 }
106 }
107}
108
109impl<'de> Deserialize<'de> for VarOrBool {
110 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
111 where
112 D: Deserializer<'de>,
113 {
114 deserializer.deserialize_any(VarOrBoolVisitor)
115 }
116}
117
118impl XmlDeserialize for VarOrBool {
119 fn deserialize<R: Read>(reader: &mut crate::xml::de::Deserializer<R>) -> Result<Self, String> {
120 loop {
121 match reader.next_event()? {
122 xml::reader::XmlEvent::StartElement { .. } => {}
123 xml::reader::XmlEvent::Characters(text_content) => {
124 if text_content.is_empty() {
125 return Err("value of attribute is empty".to_string());
126 };
127 if text_content.starts_with("${") && text_content.ends_with('}') {
128 return Ok(VarOrBool::Var(text_content));
129 } else {
130 return Ok(VarOrBool::Bool(text_content.parse().map_err(|_| {
131 format!("value {text_content} is not a valid boolean")
132 })?));
133 }
134 }
135 _ => {
136 break;
137 }
138 }
139 }
140 Err("Unable to parse attribute".to_string())
141 }
142}