1use std::str::FromStr;
2
3use toml_datetime::Datetime;
4
5use crate::array_of_tables::ArrayOfTables;
6use crate::table::TableLike;
7use crate::{Array, InlineTable, Table, Value};
8
9#[derive(Debug, Default)]
11pub enum Item {
12 #[default]
14 None,
15 Value(Value),
17 Table(Table),
19 ArrayOfTables(ArrayOfTables),
21}
22
23impl Item {
24 pub fn or_insert(&mut self, item: Item) -> &mut Item {
27 if self.is_none() {
28 *self = item;
29 }
30 self
31 }
32}
33
34impl Item {
37 pub fn type_name(&self) -> &'static str {
39 match self {
40 Item::None => "none",
41 Item::Value(v) => v.type_name(),
42 Item::Table(..) => "table",
43 Item::ArrayOfTables(..) => "array of tables",
44 }
45 }
46
47 pub fn get<I: crate::index::Index>(&self, index: I) -> Option<&Item> {
58 index.index(self)
59 }
60
61 pub fn get_mut<I: crate::index::Index>(&mut self, index: I) -> Option<&mut Item> {
72 index.index_mut(self)
73 }
74
75 pub fn as_value(&self) -> Option<&Value> {
77 match *self {
78 Item::Value(ref v) => Some(v),
79 _ => None,
80 }
81 }
82 pub fn as_table(&self) -> Option<&Table> {
84 match *self {
85 Item::Table(ref t) => Some(t),
86 _ => None,
87 }
88 }
89 pub fn as_array_of_tables(&self) -> Option<&ArrayOfTables> {
91 match *self {
92 Item::ArrayOfTables(ref a) => Some(a),
93 _ => None,
94 }
95 }
96 pub fn as_value_mut(&mut self) -> Option<&mut Value> {
98 match *self {
99 Item::Value(ref mut v) => Some(v),
100 _ => None,
101 }
102 }
103 pub fn as_table_mut(&mut self) -> Option<&mut Table> {
105 match *self {
106 Item::Table(ref mut t) => Some(t),
107 _ => None,
108 }
109 }
110 pub fn as_array_of_tables_mut(&mut self) -> Option<&mut ArrayOfTables> {
112 match *self {
113 Item::ArrayOfTables(ref mut a) => Some(a),
114 _ => None,
115 }
116 }
117 pub fn into_value(self) -> Result<Value, Self> {
119 match self {
120 Item::None => Err(self),
121 Item::Value(v) => Ok(v),
122 Item::Table(v) => {
123 let v = v.into_inline_table();
124 Ok(Value::InlineTable(v))
125 }
126 Item::ArrayOfTables(v) => {
127 let v = v.into_array();
128 Ok(Value::Array(v))
129 }
130 }
131 }
132 pub fn make_value(&mut self) {
134 let other = std::mem::take(self);
135 let other = other.into_value().map(Item::Value).unwrap_or(Item::None);
136 *self = other;
137 }
138 pub fn into_table(self) -> Result<Table, Self> {
140 match self {
141 Item::Table(t) => Ok(t),
142 Item::Value(Value::InlineTable(t)) => Ok(t.into_table()),
143 _ => Err(self),
144 }
145 }
146 pub fn into_array_of_tables(self) -> Result<ArrayOfTables, Self> {
148 match self {
149 Item::ArrayOfTables(a) => Ok(a),
150 Item::Value(Value::Array(a)) => {
151 if a.is_empty() {
152 Err(Item::Value(Value::Array(a)))
153 } else if a.iter().all(|v| v.is_inline_table()) {
154 let mut aot = ArrayOfTables::new();
155 aot.values = a.values;
156 for value in aot.values.iter_mut() {
157 value.make_item();
158 }
159 Ok(aot)
160 } else {
161 Err(Item::Value(Value::Array(a)))
162 }
163 }
164 _ => Err(self),
165 }
166 }
167 pub(crate) fn make_item(&mut self) {
169 let other = std::mem::take(self);
170 let other = match other.into_table().map(Item::Table) {
171 Ok(i) => i,
172 Err(i) => i,
173 };
174 let other = match other.into_array_of_tables().map(Item::ArrayOfTables) {
175 Ok(i) => i,
176 Err(i) => i,
177 };
178 *self = other;
179 }
180 pub fn is_value(&self) -> bool {
182 self.as_value().is_some()
183 }
184 pub fn is_table(&self) -> bool {
186 self.as_table().is_some()
187 }
188 pub fn is_array_of_tables(&self) -> bool {
190 self.as_array_of_tables().is_some()
191 }
192 pub fn is_none(&self) -> bool {
194 matches!(*self, Item::None)
195 }
196
197 pub fn as_integer(&self) -> Option<i64> {
201 self.as_value().and_then(Value::as_integer)
202 }
203
204 pub fn is_integer(&self) -> bool {
206 self.as_integer().is_some()
207 }
208
209 pub fn as_float(&self) -> Option<f64> {
211 self.as_value().and_then(Value::as_float)
212 }
213
214 pub fn is_float(&self) -> bool {
216 self.as_float().is_some()
217 }
218
219 pub fn as_bool(&self) -> Option<bool> {
221 self.as_value().and_then(Value::as_bool)
222 }
223
224 pub fn is_bool(&self) -> bool {
226 self.as_bool().is_some()
227 }
228
229 pub fn as_str(&self) -> Option<&str> {
231 self.as_value().and_then(Value::as_str)
232 }
233
234 pub fn is_str(&self) -> bool {
236 self.as_str().is_some()
237 }
238
239 pub fn as_datetime(&self) -> Option<&Datetime> {
241 self.as_value().and_then(Value::as_datetime)
242 }
243
244 pub fn is_datetime(&self) -> bool {
246 self.as_datetime().is_some()
247 }
248
249 pub fn as_array(&self) -> Option<&Array> {
251 self.as_value().and_then(Value::as_array)
252 }
253
254 pub fn as_array_mut(&mut self) -> Option<&mut Array> {
256 self.as_value_mut().and_then(Value::as_array_mut)
257 }
258
259 pub fn is_array(&self) -> bool {
261 self.as_array().is_some()
262 }
263
264 pub fn as_inline_table(&self) -> Option<&InlineTable> {
266 self.as_value().and_then(Value::as_inline_table)
267 }
268
269 pub fn as_inline_table_mut(&mut self) -> Option<&mut InlineTable> {
271 self.as_value_mut().and_then(Value::as_inline_table_mut)
272 }
273
274 pub fn is_inline_table(&self) -> bool {
276 self.as_inline_table().is_some()
277 }
278
279 pub fn as_table_like(&self) -> Option<&dyn TableLike> {
281 self.as_table()
282 .map(|t| t as &dyn TableLike)
283 .or_else(|| self.as_inline_table().map(|t| t as &dyn TableLike))
284 }
285
286 pub fn as_table_like_mut(&mut self) -> Option<&mut dyn TableLike> {
288 match self {
289 Item::Table(t) => Some(t as &mut dyn TableLike),
290 Item::Value(Value::InlineTable(t)) => Some(t as &mut dyn TableLike),
291 _ => None,
292 }
293 }
294
295 pub fn is_table_like(&self) -> bool {
297 self.as_table_like().is_some()
298 }
299
300 pub fn span(&self) -> Option<std::ops::Range<usize>> {
304 match self {
305 Item::None => None,
306 Item::Value(v) => v.span(),
307 Item::Table(v) => v.span(),
308 Item::ArrayOfTables(v) => v.span(),
309 }
310 }
311
312 pub(crate) fn despan(&mut self, input: &str) {
313 match self {
314 Item::None => {}
315 Item::Value(v) => v.despan(input),
316 Item::Table(v) => v.despan(input),
317 Item::ArrayOfTables(v) => v.despan(input),
318 }
319 }
320}
321
322impl Clone for Item {
323 #[inline(never)]
324 fn clone(&self) -> Self {
325 match self {
326 Item::None => Item::None,
327 Item::Value(v) => Item::Value(v.clone()),
328 Item::Table(v) => Item::Table(v.clone()),
329 Item::ArrayOfTables(v) => Item::ArrayOfTables(v.clone()),
330 }
331 }
332}
333
334#[cfg(feature = "parse")]
335impl FromStr for Item {
336 type Err = crate::TomlError;
337
338 fn from_str(s: &str) -> Result<Self, Self::Err> {
340 let value = s.parse::<Value>()?;
341 Ok(Item::Value(value))
342 }
343}
344
345impl<'b> From<&'b Item> for Item {
346 fn from(s: &'b Item) -> Self {
347 s.clone()
348 }
349}
350
351impl<V: Into<Value>> From<V> for Item {
352 fn from(s: V) -> Self {
353 Item::Value(s.into())
354 }
355}
356
357#[cfg(feature = "display")]
358impl std::fmt::Display for Item {
359 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
360 match &self {
361 Item::None => Ok(()),
362 Item::Value(v) => v.fmt(f),
363 Item::Table(v) => v.fmt(f),
364 Item::ArrayOfTables(v) => v.fmt(f),
365 }
366 }
367}
368
369pub fn value<V: Into<Value>>(v: V) -> Item {
396 Item::Value(v.into())
397}
398
399pub fn table() -> Item {
401 Item::Table(Table::new())
402}
403
404pub fn array() -> Item {
406 Item::ArrayOfTables(ArrayOfTables::new())
407}
408
409#[test]
410#[cfg(feature = "parse")]
411#[cfg(feature = "display")]
412fn string_roundtrip() {
413 value("hello").to_string().parse::<Item>().unwrap();
414}