config/path/
mod.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use std::str::FromStr;

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

mod parser;

#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub(crate) struct Expression {
    root: String,
    postfix: Vec<Postfix>,
}

impl Expression {
    pub(crate) fn root(root: String) -> Self {
        Self {
            root,
            postfix: Vec::new(),
        }
    }
}

impl FromStr for Expression {
    type Err = ConfigError;

    fn from_str(s: &str) -> Result<Self> {
        parser::from_str(s).map_err(|e| ConfigError::PathParse {
            cause: Box::new(ParseError::new(e)),
        })
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Hash)]
enum Postfix {
    Key(String),
    Index(isize),
}

#[derive(Debug)]
struct ParseError(String);

impl ParseError {
    fn new(inner: winnow::error::ParseError<&str, winnow::error::ContextError>) -> Self {
        Self(inner.to_string())
    }
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

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

/// Convert a relative index into an absolute index
fn abs_index(index: isize, len: usize) -> Result<usize, usize> {
    if index >= 0 {
        Ok(index as usize)
    } else if let Some(index) = len.checked_sub(index.unsigned_abs()) {
        Ok(index)
    } else {
        Err((len as isize + index).unsigned_abs())
    }
}

impl Expression {
    pub(crate) fn get(self, root: &Value) -> Option<&Value> {
        let ValueKind::Table(map) = &root.kind else {
            return None;
        };
        let mut child = map.get(&self.root)?;
        for postfix in &self.postfix {
            match postfix {
                Postfix::Key(key) => {
                    let ValueKind::Table(map) = &child.kind else {
                        return None;
                    };
                    child = map.get(key)?;
                }
                Postfix::Index(rel_index) => {
                    let ValueKind::Array(array) = &child.kind else {
                        return None;
                    };
                    let index = abs_index(*rel_index, array.len()).ok()?;
                    child = array.get(index)?;
                }
            }
        }
        Some(child)
    }

    pub(crate) fn get_mut_forcibly<'a>(&self, root: &'a mut Value) -> &'a mut Value {
        if !matches!(root.kind, ValueKind::Table(_)) {
            *root = Map::<String, Value>::new().into();
        }
        let ValueKind::Table(map) = &mut root.kind else {
            unreachable!()
        };
        let mut child = map
            .entry(self.root.clone())
            .or_insert_with(|| Value::new(None, ValueKind::Nil));
        for postfix in &self.postfix {
            match postfix {
                Postfix::Key(key) => {
                    if !matches!(child.kind, ValueKind::Table(_)) {
                        *child = Map::<String, Value>::new().into();
                    }
                    let ValueKind::Table(ref mut map) = child.kind else {
                        unreachable!()
                    };

                    child = map
                        .entry(key.clone())
                        .or_insert_with(|| Value::new(None, ValueKind::Nil));
                }
                Postfix::Index(rel_index) => {
                    if !matches!(child.kind, ValueKind::Array(_)) {
                        *child = Vec::<Value>::new().into();
                    }
                    let ValueKind::Array(ref mut array) = child.kind else {
                        unreachable!()
                    };

                    let uindex = match abs_index(*rel_index, array.len()) {
                        Ok(uindex) => {
                            if uindex >= array.len() {
                                array.resize(uindex + 1, Value::new(None, ValueKind::Nil));
                            }
                            uindex
                        }
                        Err(insertion) => {
                            array.splice(
                                0..0,
                                (0..insertion).map(|_| Value::new(None, ValueKind::Nil)),
                            );
                            0
                        }
                    };

                    child = &mut array[uindex];
                }
            }
        }
        child
    }

    pub(crate) fn set(&self, root: &mut Value, value: Value) {
        let parent = self.get_mut_forcibly(root);
        match value.kind {
            ValueKind::Table(ref incoming_map) => {
                // If the parent is not a table, overwrite it, treating it as a
                // table
                if !matches!(parent.kind, ValueKind::Table(_)) {
                    *parent = Map::<String, Value>::new().into();
                }

                // Continue the deep merge
                for (key, val) in incoming_map {
                    Self::root(key.clone()).set(parent, val.clone());
                }
            }
            _ => {
                *parent = value;
            }
        }
    }
}