android_manifest/
var_or_bool.rs1use serde::{
2 de::{self, Visitor},
3 Deserialize, Deserializer, Serialize, Serializer,
4};
5use std::fmt;
6use std::io::{Read, Write};
7use yaserde::{YaDeserialize, YaSerialize};
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 ToString for VarOrBool {
45 fn to_string(&self) -> String {
46 match self {
47 Self::Var(r) => r.to_string(),
48 Self::Bool(v) => v.to_string(),
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 YaSerialize for VarOrBool {
66 fn serialize<W: Write>(&self, writer: &mut yaserde::ser::Serializer<W>) -> Result<(), String> {
67 match self {
68 VarOrBool::Var(variable) => {
69 let _ret = writer.write(xml::writer::XmlEvent::characters(variable));
70 }
71 VarOrBool::Bool(value) => {
72 let _ret = writer.write(xml::writer::XmlEvent::characters(&value.to_string()));
73 }
74 }
75 Ok(())
76 }
77
78 fn serialize_attributes(
79 &self,
80 attributes: Vec<xml::attribute::OwnedAttribute>,
81 namespace: xml::namespace::Namespace,
82 ) -> Result<
83 (
84 Vec<xml::attribute::OwnedAttribute>,
85 xml::namespace::Namespace,
86 ),
87 String,
88 > {
89 Ok((attributes, namespace))
90 }
91}
92
93struct VarOrBoolVisitor;
94
95impl<'de> Visitor<'de> for VarOrBoolVisitor {
96 type Value = VarOrBool;
97
98 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
99 formatter.write_str("a boolean value or a variable in the \"${variable}\" format")
100 }
101
102 fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
103 where
104 E: de::Error,
105 {
106 Ok(v.into())
107 }
108
109 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
110 where
111 E: de::Error,
112 {
113 if v.is_empty() {
114 return Err(E::custom("value of attribute is empty"));
115 };
116 if v.starts_with("${") && v.ends_with('}') {
117 Ok(VarOrBool::var(v))
118 } else {
119 Ok(VarOrBool::Bool(v.parse().map_err(|_| {
120 E::custom(format!("value `{v}` is not a valid boolean"))
121 })?))
122 }
123 }
124}
125
126impl<'de> Deserialize<'de> for VarOrBool {
127 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
128 where
129 D: Deserializer<'de>,
130 {
131 deserializer.deserialize_string(VarOrBoolVisitor)
132 }
133}
134
135impl YaDeserialize for VarOrBool {
136 fn deserialize<R: Read>(reader: &mut yaserde::de::Deserializer<R>) -> Result<Self, String> {
137 loop {
138 match reader.next_event()? {
139 xml::reader::XmlEvent::StartElement { .. } => {}
140 xml::reader::XmlEvent::Characters(text_content) => {
141 if text_content.is_empty() {
142 return Err("value of attribute is empty".to_string());
143 };
144 if text_content.starts_with("${") && text_content.ends_with('}') {
145 return Ok(VarOrBool::Var(text_content));
146 } else {
147 return Ok(VarOrBool::Bool(text_content.parse().map_err(|_| {
148 format!("value {text_content} is not a valid boolean")
149 })?));
150 }
151 }
152 _ => {
153 break;
154 }
155 }
156 }
157 Err("Unable to parse attribute".to_string())
158 }
159}