# toml.rs - source
Source of the Rust file `/home/justine/.cargo/registry/src/github.com-1ecc6299db9ec823/config-0.13.3/src/file/format/toml.rs`.
Keywords: rust, rustlang, rust-lang

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49

    use std::error::Error;

    use crate::map::Map;
    use crate::value::{Value, ValueKind};

    pub fn parse(
        uri: Option<&String>,
        text: &str,
    ) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
        // Parse a TOML value from the provided text
        // TODO: Have a proper error fire if the root of a file is ever not a Table
        let value = from_toml_value(uri, &toml::from_str(text)?);
        match value.kind {
            ValueKind::Table(map) => Ok(map),

            _ => Ok(Map::new()),
        }
    }

    fn from_toml_value(uri: Option<&String>, value: &toml::Value) -> Value {
        match *value {
            toml::Value::String(ref value) => Value::new(uri, value.to_string()),
            toml::Value::Float(value) => Value::new(uri, value),
            toml::Value::Integer(value) => Value::new(uri, value),
            toml::Value::Boolean(value) => Value::new(uri, value),

            toml::Value::Table(ref table) => {
                let mut m = Map::new();

                for (key, value) in table {
                    m.insert(key.clone(), from_toml_value(uri, value));
                }

                Value::new(uri, m)
            }

            toml::Value::Array(ref array) => {
                let mut l = Vec::new();

                for value in array {
                    l.push(from_toml_value(uri, value));
                }

                Value::new(uri, l)
            }

            toml::Value::Datetime(ref datetime) => Value::new(uri, datetime.to_string()),
        }
    }