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#[derive(Serialize, Debug, Clone, PartialEq, Eq, Hash)]
31#[serde(untagged)]
32pub enum Key<'a> {
33    String(Cow<'a, str>),
34    Integer(i64),
35}
36
37impl Display for Key<'_> {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        write!(
40            f,
41            "{}",
42            match self {
43                Key::String(val) => val.to_string(),
44                Key::Integer(val) => val.to_string(),
45            }
46        )
47    }
48}
49
50impl<'a> From<Key<'a>> for Value<'a> {
51    fn from(value: Key<'a>) -> Self {
52        match value {
53            Key::String(val) => Value::String(val),
54            Key::Integer(val) => Value::Integer(val),
55        }
56    }
57}
58
59/// A map of keys to their values.
60pub type Object<'a> = IndexMap<Key<'a>, Value<'a>>;
61
62#[derive(Serialize, Debug, Clone)]
63#[serde(untagged)]
64pub enum Value<'a> {
65    /// Key/value map. Values can be mixed types.
66    Object(Object<'a>),
67    /// Array of values, can be mixed types.
68    Array(Vec<Value<'a>>),
69    /// UTF-8 string
70    String(Cow<'a, str>),
71    /// 64-bit signed integer.
72    Integer(i64),
73    /// 64-bit (double precision) floating point number.
74    Float(f64),
75    /// true or false
76    Boolean(bool),
77    /// `null` literal.
78    ///
79    /// Takes an optional unit type as the `toml` crate
80    /// errors when encountering unit types,
81    /// but can handle `None` types.
82    Null(Option<()>),
83}
84
85impl Display for Value<'_> {
86    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
87        write!(
88            f,
89            "{}",
90            match self {
91                Value::Object(_) => "object",
92                Value::Array(_) => "array",
93                Value::String(_) => "string",
94                Value::Integer(_) => "integer",
95                Value::Float(_) => "float",
96                Value::Boolean(_) => "boolean",
97                Value::Null(_) => "null",
98            }
99        )
100    }
101}