simple_toml_parser/
lib.rs

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TOMLKey<'a> {
    Slice(&'a str),
    Index(usize),
}

#[derive(Debug, PartialEq, Eq)]
pub enum RootTOMLValue<'a> {
    String(&'a str),
    Number(&'a str),
    Boolean(bool),
    Null,
}

#[derive(Debug)]
pub enum TOMLParseErrorReason {
    ExpectedColon,
    ExpectedEndOfValue,
    ExpectedBracket,
    ExpectedTrueFalseNull,
    ExpectedValue,
}

#[derive(Debug)]
pub struct TOMLParseError {
    pub at: usize,
    pub reason: TOMLParseErrorReason,
}

impl std::error::Error for TOMLParseError {}

impl std::fmt::Display for TOMLParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        f.write_fmt(format_args!(
            "TOMLParseError: {:?} at {:?}",
            self.reason, self.at
        ))
    }
}

/// If you want to return early (not parse the whole input) use [`parse_with_exit_signal`]
///
/// # Errors
/// Returns an error if it tries to parse invalid TOML input
pub fn parse<'a>(
    on: &'a str,
    mut cb: impl for<'b> FnMut(&'b [TOMLKey<'a>], RootTOMLValue<'a>),
) -> Result<(), TOMLParseError> {
    parse_with_exit_signal(on, |k, v| {
        cb(k, v);
        false
    })
}

/// # Errors
/// Returns an error if it tries to parse invalid TOML input
/// # Panics
/// On unimplemented items
#[allow(clippy::too_many_lines)]
pub fn parse_with_exit_signal<'a>(
    on: &'a str,
    mut cb: impl for<'b> FnMut(&'b [TOMLKey<'a>], RootTOMLValue<'a>) -> bool,
) -> Result<(), TOMLParseError> {
    let mut key_chain = Vec::new();
    let lines = on.lines();

    for line in lines {
        if line.starts_with("[[") {
            let _ = key_chain.drain(..);
            assert!(line.ends_with("]]"), "TODO error");

            let idx = if let Some(TOMLKey::Index(idx)) = key_chain.last() {
                idx + 1
            } else {
                0
            };
            // TODO strings parsing here
            for part in line[1..(line.len() - "]]".len())].split('.') {
                key_chain.push(TOMLKey::Slice(part));
            }
            key_chain.push(TOMLKey::Index(idx));
        } else if line.starts_with('[') {
            let _ = key_chain.drain(..);
            assert!(line.ends_with(']'), "TODO error");
            // TODO strings parsing here
            for part in line[1..(line.len() - "]".len())].split('.') {
                key_chain.push(TOMLKey::Slice(part));
            }
        } else {
            if line.starts_with('#') || line.is_empty() {
                continue;
            }
            if let Some((key, value)) = line.split_once('=') {
                let length = key_chain.len();
                for part in key.trim().split('.') {
                    key_chain.push(TOMLKey::Slice(part));
                }

                let value = value.trim();
                let value = if let "true" | "false" = value {
                    RootTOMLValue::Boolean(value == "true")
                } else if value.starts_with(['"', '\'']) {
                    assert!(
                        value.ends_with(value.chars().next().unwrap()),
                        "TODO unclosed string"
                    );
                    RootTOMLValue::String(&value[1..(value.len() - 1)])
                } else if value.starts_with(char::is_numeric) {
                    RootTOMLValue::Number(value)
                } else {
                    eprintln!("here {value}");
                    continue;
                };

                let _result = cb(&key_chain, value);

                let _ = key_chain.drain(length..);
            } else {
                panic!("bad key")
            }
        }
    }

    Ok(())
}