corn/
lib.rs

1use indexmap::IndexMap;
2use serde::Serialize;
3use std::borrow::Cow;
4use std::collections::HashMap;
5use std::fmt::{Display, Formatter};
6
7pub use crate::de::{from_slice, from_str};
8pub use crate::parser::{parse, Rule};
9
10pub mod error;
11mod parser;
12
13mod de;
14#[cfg(any(
15    feature = "lua51",
16    feature = "lua52",
17    feature = "lua53",
18    feature = "lua54",
19    feature = "luajit",
20    feature = "luajit52"
21))]
22mod lua;
23#[cfg(feature = "wasm")]
24mod wasm;
25
26/// A map of input names and values.
27/// The names include their `$` prefix.
28pub type Inputs<'a> = HashMap<&'a str, Value<'a>>;
29
30/// A map of keys to their values.
31pub type Object<'a> = IndexMap<Cow<'a, str>, Value<'a>>;
32
33#[derive(Serialize, Debug, Clone)]
34#[serde(untagged)]
35pub enum Value<'a> {
36    /// Key/value map. Values can be mixed types.
37    Object(Object<'a>),
38    /// Array of values, can be mixed types.
39    Array(Vec<Value<'a>>),
40    /// UTF-8 string
41    String(Cow<'a, str>),
42    /// 64-bit signed integer.
43    Integer(i64),
44    /// 64-bit (double precision) floating point number.
45    Float(f64),
46    /// true or false
47    Boolean(bool),
48    /// `null` literal.
49    ///
50    /// Takes an optional unit type as the `toml` crate
51    /// errors when encountering unit types,
52    /// but can handle `None` types.
53    Null(Option<()>),
54}
55
56impl<'a> Display for Value<'a> {
57    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
58        write!(
59            f,
60            "{}",
61            match self {
62                Value::Object(_) => "object",
63                Value::Array(_) => "array",
64                Value::String(_) => "string",
65                Value::Integer(_) => "integer",
66                Value::Float(_) => "float",
67                Value::Boolean(_) => "boolean",
68                Value::Null(_) => "null",
69            }
70        )
71    }
72}