oak_xml/language/
value.rs1use serde::Deserialize;
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, PartialEq, Deserialize)]
8pub enum XmlValue {
9 String(String),
11 Integer(i64),
13 Float(f64),
15 Boolean(bool),
17 Null,
19 Array(XmlArray),
21 Object(XmlObject),
23}
24
25#[derive(Debug, Clone, PartialEq, Deserialize)]
27pub struct XmlArray {
28 pub list: Vec<XmlValue>,
29}
30
31#[derive(Debug, Clone, PartialEq, Deserialize)]
33pub struct XmlObject {
34 pub dict: HashMap<String, XmlValue>,
35}
36
37impl XmlValue {
38 pub fn as_str(&self) -> Option<&str> {
40 match self {
41 XmlValue::String(s) => Some(s),
42 _ => None,
43 }
44 }
45
46 pub fn as_integer(&self) -> Option<i64> {
48 match self {
49 XmlValue::Integer(i) => Some(*i),
50 _ => None,
51 }
52 }
53
54 pub fn as_float(&self) -> Option<f64> {
56 match self {
57 XmlValue::Float(f) => Some(*f),
58 _ => None,
59 }
60 }
61
62 pub fn as_bool(&self) -> Option<bool> {
64 match self {
65 XmlValue::Boolean(b) => Some(*b),
66 _ => None,
67 }
68 }
69
70 pub fn is_null(&self) -> bool {
72 matches!(self, XmlValue::Null)
73 }
74
75 pub fn as_array(&self) -> Option<&Vec<XmlValue>> {
77 match self {
78 XmlValue::Array(XmlArray { list: a }) => Some(a),
79 _ => None,
80 }
81 }
82
83 pub fn as_object(&self) -> Option<&HashMap<String, XmlValue>> {
85 match self {
86 XmlValue::Object(XmlObject { dict: o }) => Some(o),
87 _ => None,
88 }
89 }
90
91 pub fn get(&self, key: &str) -> Option<&XmlValue> {
93 match self {
94 XmlValue::Object(XmlObject { dict: o }) => o.get(key),
95 _ => None,
96 }
97 }
98}
99
100impl std::fmt::Display for XmlValue {
101 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 match self {
103 XmlValue::String(s) => write!(f, "\"{}\"", s),
104 XmlValue::Integer(i) => write!(f, "{}", i),
105 XmlValue::Float(fl) => write!(f, "{}", fl),
106 XmlValue::Boolean(b) => write!(f, "{}", b),
107 XmlValue::Null => write!(f, "null"),
108 XmlValue::Array(XmlArray { list: a }) => {
109 write!(f, "[")?;
110 for (i, item) in a.iter().enumerate() {
111 if i > 0 {
112 write!(f, ", ")?;
113 }
114 write!(f, "{}", item)?;
115 }
116 write!(f, "]")
117 }
118 XmlValue::Object(XmlObject { dict: o }) => {
119 write!(f, "{{")?;
120 for (i, (key, value)) in o.iter().enumerate() {
121 if i > 0 {
122 write!(f, ", ")?;
123 }
124 write!(f, "\"{}\": {}", key, value)?;
125 }
126 write!(f, "}}")
127 }
128 }
129 }
130}