1use std::iter::FromIterator;
2use std::str::FromStr;
3
4use toml_datetime::{Date, Datetime, Time};
5
6use crate::key::Key;
7use crate::repr::{Decor, Formatted};
8use crate::{Array, InlineTable, InternalString, RawString};
9
10#[derive(Debug, Clone)]
13pub enum Value {
14 String(Formatted<String>),
16 Integer(Formatted<i64>),
18 Float(Formatted<f64>),
20 Boolean(Formatted<bool>),
22 Datetime(Formatted<Datetime>),
24 Array(Array),
26 InlineTable(InlineTable),
28}
29
30impl Value {
32 pub fn type_name(&self) -> &'static str {
34 match self {
35 Value::String(..) => "string",
36 Value::Integer(..) => "integer",
37 Value::Float(..) => "float",
38 Value::Boolean(..) => "boolean",
39 Value::Datetime(..) => "datetime",
40 Value::Array(..) => "array",
41 Value::InlineTable(..) => "inline table",
42 }
43 }
44
45 pub fn as_str(&self) -> Option<&str> {
47 match *self {
48 Value::String(ref value) => Some(value.value()),
49 _ => None,
50 }
51 }
52
53 pub fn is_str(&self) -> bool {
55 self.as_str().is_some()
56 }
57
58 pub fn as_integer(&self) -> Option<i64> {
60 match *self {
61 Value::Integer(ref value) => Some(*value.value()),
62 _ => None,
63 }
64 }
65
66 pub fn is_integer(&self) -> bool {
68 self.as_integer().is_some()
69 }
70
71 pub fn as_float(&self) -> Option<f64> {
73 match *self {
74 Value::Float(ref value) => Some(*value.value()),
75 _ => None,
76 }
77 }
78
79 pub fn is_float(&self) -> bool {
81 self.as_float().is_some()
82 }
83
84 pub fn as_bool(&self) -> Option<bool> {
86 match *self {
87 Value::Boolean(ref value) => Some(*value.value()),
88 _ => None,
89 }
90 }
91
92 pub fn is_bool(&self) -> bool {
94 self.as_bool().is_some()
95 }
96
97 pub fn as_datetime(&self) -> Option<&Datetime> {
99 match *self {
100 Value::Datetime(ref value) => Some(value.value()),
101 _ => None,
102 }
103 }
104
105 pub fn is_datetime(&self) -> bool {
107 self.as_datetime().is_some()
108 }
109
110 pub fn as_array(&self) -> Option<&Array> {
112 match *self {
113 Value::Array(ref value) => Some(value),
114 _ => None,
115 }
116 }
117
118 pub fn as_array_mut(&mut self) -> Option<&mut Array> {
120 match *self {
121 Value::Array(ref mut value) => Some(value),
122 _ => None,
123 }
124 }
125
126 pub fn is_array(&self) -> bool {
128 self.as_array().is_some()
129 }
130
131 pub fn as_inline_table(&self) -> Option<&InlineTable> {
133 match *self {
134 Value::InlineTable(ref value) => Some(value),
135 _ => None,
136 }
137 }
138
139 pub fn as_inline_table_mut(&mut self) -> Option<&mut InlineTable> {
141 match *self {
142 Value::InlineTable(ref mut value) => Some(value),
143 _ => None,
144 }
145 }
146
147 pub fn is_inline_table(&self) -> bool {
149 self.as_inline_table().is_some()
150 }
151}
152
153impl Value {
154 pub fn decor_mut(&mut self) -> &mut Decor {
161 match self {
162 Value::String(f) => f.decor_mut(),
163 Value::Integer(f) => f.decor_mut(),
164 Value::Float(f) => f.decor_mut(),
165 Value::Boolean(f) => f.decor_mut(),
166 Value::Datetime(f) => f.decor_mut(),
167 Value::Array(a) => a.decor_mut(),
168 Value::InlineTable(t) => t.decor_mut(),
169 }
170 }
171
172 pub fn decor(&self) -> &Decor {
179 match *self {
180 Value::String(ref f) => f.decor(),
181 Value::Integer(ref f) => f.decor(),
182 Value::Float(ref f) => f.decor(),
183 Value::Boolean(ref f) => f.decor(),
184 Value::Datetime(ref f) => f.decor(),
185 Value::Array(ref a) => a.decor(),
186 Value::InlineTable(ref t) => t.decor(),
187 }
188 }
189
190 pub fn decorated(mut self, prefix: impl Into<RawString>, suffix: impl Into<RawString>) -> Self {
201 self.decorate(prefix, suffix);
202 self
203 }
204
205 pub(crate) fn decorate(&mut self, prefix: impl Into<RawString>, suffix: impl Into<RawString>) {
206 let decor = self.decor_mut();
207 *decor = Decor::new(prefix, suffix);
208 }
209
210 pub fn span(&self) -> Option<std::ops::Range<usize>> {
214 match self {
215 Value::String(f) => f.span(),
216 Value::Integer(f) => f.span(),
217 Value::Float(f) => f.span(),
218 Value::Boolean(f) => f.span(),
219 Value::Datetime(f) => f.span(),
220 Value::Array(a) => a.span(),
221 Value::InlineTable(t) => t.span(),
222 }
223 }
224
225 pub(crate) fn despan(&mut self, input: &str) {
226 match self {
227 Value::String(f) => f.despan(input),
228 Value::Integer(f) => f.despan(input),
229 Value::Float(f) => f.despan(input),
230 Value::Boolean(f) => f.despan(input),
231 Value::Datetime(f) => f.despan(input),
232 Value::Array(a) => a.despan(input),
233 Value::InlineTable(t) => t.despan(input),
234 }
235 }
236}
237
238#[cfg(feature = "parse")]
239impl FromStr for Value {
240 type Err = crate::TomlError;
241
242 fn from_str(s: &str) -> Result<Self, Self::Err> {
244 crate::parser::parse_value(s)
245 }
246}
247
248impl<'b> From<&'b Value> for Value {
249 fn from(s: &'b Value) -> Self {
250 s.clone()
251 }
252}
253
254impl<'b> From<&'b str> for Value {
255 fn from(s: &'b str) -> Self {
256 s.to_owned().into()
257 }
258}
259
260impl<'b> From<&'b String> for Value {
261 fn from(s: &'b String) -> Self {
262 s.to_owned().into()
263 }
264}
265
266impl From<String> for Value {
267 fn from(s: String) -> Self {
268 Value::String(Formatted::new(s))
269 }
270}
271
272impl<'b> From<&'b InternalString> for Value {
273 fn from(s: &'b InternalString) -> Self {
274 s.as_str().into()
275 }
276}
277
278impl From<InternalString> for Value {
279 fn from(s: InternalString) -> Self {
280 s.as_str().into()
281 }
282}
283
284impl From<i64> for Value {
285 fn from(i: i64) -> Self {
286 Value::Integer(Formatted::new(i))
287 }
288}
289
290impl From<f64> for Value {
291 fn from(f: f64) -> Self {
292 Value::Float(Formatted::new(f))
294 }
295}
296
297impl From<bool> for Value {
298 fn from(b: bool) -> Self {
299 Value::Boolean(Formatted::new(b))
300 }
301}
302
303impl From<Datetime> for Value {
304 fn from(d: Datetime) -> Self {
305 Value::Datetime(Formatted::new(d))
306 }
307}
308
309impl From<Date> for Value {
310 fn from(d: Date) -> Self {
311 let d: Datetime = d.into();
312 d.into()
313 }
314}
315
316impl From<Time> for Value {
317 fn from(d: Time) -> Self {
318 let d: Datetime = d.into();
319 d.into()
320 }
321}
322
323impl From<Array> for Value {
324 fn from(array: Array) -> Self {
325 Value::Array(array)
326 }
327}
328
329impl From<InlineTable> for Value {
330 fn from(table: InlineTable) -> Self {
331 Value::InlineTable(table)
332 }
333}
334
335impl<V: Into<Value>> FromIterator<V> for Value {
336 fn from_iter<I>(iter: I) -> Self
337 where
338 I: IntoIterator<Item = V>,
339 {
340 let array: Array = iter.into_iter().collect();
341 Value::Array(array)
342 }
343}
344
345impl<K: Into<Key>, V: Into<Value>> FromIterator<(K, V)> for Value {
346 fn from_iter<I>(iter: I) -> Self
347 where
348 I: IntoIterator<Item = (K, V)>,
349 {
350 let table: InlineTable = iter.into_iter().collect();
351 Value::InlineTable(table)
352 }
353}
354
355#[cfg(feature = "display")]
356impl std::fmt::Display for Value {
357 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
358 crate::encode::encode_value(self, f, None, ("", ""))
359 }
360}
361
362pub(crate) const DEFAULT_VALUE_DECOR: (&str, &str) = (" ", "");
364pub(crate) const DEFAULT_TRAILING_VALUE_DECOR: (&str, &str) = (" ", " ");
366pub(crate) const DEFAULT_LEADING_VALUE_DECOR: (&str, &str) = ("", "");
368
369#[cfg(test)]
370#[cfg(feature = "parse")]
371#[cfg(feature = "display")]
372mod tests {
373 use super::*;
374
375 #[test]
376 fn from_iter_formatting() {
377 let features = ["node".to_owned(), "mouth".to_owned()];
378 let features: Value = features.iter().cloned().collect();
379 assert_eq!(features.to_string(), r#"["node", "mouth"]"#);
380 }
381}
382
383#[test]
384#[cfg(feature = "parse")]
385#[cfg(feature = "display")]
386fn string_roundtrip() {
387 Value::from("hello").to_string().parse::<Value>().unwrap();
388}