weaveconfig 0.1.2

A unified configuration tool for monorepos
Documentation
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
mod integer;
mod segment;

use integer::parse_integer;
use segment::{parse_segment, ParseSegmentError};
use serde_json::{Map, Value};
use thiserror::Error;

/// Enum representing possible errors during template rendering.
#[derive(Debug, Error)]
pub enum TemplateError {
    #[error("{0}")]
    VariableError(VariableError),
    #[error("Syntax error: {0}")]
    SyntaxError(String),
}

#[derive(Debug, Error)]
pub enum VariableError {
    #[error("Missing variable: {0}")]
    MissingVariable(String),
    #[error("Key not found: {0}")]
    KeyNotFound(String),
    #[error("Index out of bounds, tried to access {0} but array length is {1}")]
    IndexOutOfBounds(usize, usize),
    #[error("Invalid type, expected {0}, got {1}")]
    InvalidType(String, String),
}

fn value_type(value: &Value) -> String {
    match value {
        Value::String(_) => "string",
        Value::Number(_) => "number",
        Value::Bool(_) => "boolean",
        Value::Array(_) => "array",
        Value::Object(_) => "object",
        Value::Null => "null",
    }
    .to_string()
}

fn render_variable(
    variable: &Variable,
    variables: &Map<String, Value>,
) -> Result<String, VariableError> {
    let mut value = variables
        .get(&variable.base)
        .ok_or(VariableError::MissingVariable(variable.base.clone()))?;

    for modifier in variable.modifiers.iter() {
        match modifier {
            Modifier::Index(index) => {
                value = match value {
                    Value::Array(array) => array.get(*index as usize).ok_or_else(|| {
                        VariableError::IndexOutOfBounds(*index as usize, array.len())
                    })?,
                    _ => {
                        return Err(VariableError::InvalidType(
                            "array".to_string(),
                            value_type(value),
                        ))
                    }
                }
            }
            Modifier::Key(key) => {
                value = match value {
                    Value::Object(object) => object
                        .get(key)
                        .ok_or(VariableError::KeyNotFound(key.clone()))?,
                    _ => {
                        return Err(VariableError::InvalidType(
                            "object".to_string(),
                            value_type(value),
                        ))
                    }
                }
            }
        }
    }

    Ok(match value {
        Value::String(s) => s.to_string(),
        Value::Number(n) => n.to_string(),
        Value::Bool(b) => b.to_string(),
        Value::Null => "null".to_string(),
        _ => serde_json::to_string(value).unwrap(),
    })
}

pub fn template_file(
    content: &str,
    variables: &Map<String, Value>,
) -> Result<String, TemplateError> {
    enum State {
        Text,
        Brace,
        Escape,
        DoubleEscape,
        EscapeBrace1,
        VariableEnd1,
        VariableEnd2,
    }

    let mut state = State::Text;
    let mut output = String::new();
    let mut input = content;

    while let Some((char, rest)) = take_first(input) {
        input = rest;
        match state {
            State::Text => match char {
                '{' => {
                    state = State::Brace;
                }
                '\\' => {
                    state = State::Escape;
                }
                _ => output.push(char),
            },
            State::Brace => match char {
                '{' => {
                    let rest = strip_whitespace_left(input);
                    let (var, rest) = parse_variable(rest)?;
                    input = rest;
                    output.push_str(
                        &render_variable(&var, variables)
                            .map_err(|e| TemplateError::VariableError(e))?,
                    );
                    state = State::VariableEnd1;
                }
                '\\' => {
                    output.push('{');
                    state = State::Escape;
                }
                _ => {
                    state = State::Text;
                    output.push('{');
                    output.push(char);
                }
            },
            State::Escape => match char {
                '{' => {
                    state = State::EscapeBrace1;
                }
                '\\' => {
                    state = State::DoubleEscape;
                }
                _ => {
                    output.push('\\');
                    output.push(char);
                    state = State::Text;
                }
            },
            State::DoubleEscape => match char {
                '{' => {
                    output.push('\\');
                    state = State::Brace;
                }
                _ => {
                    output.push('\\');
                    output.push('\\');
                    output.push(char);
                    state = State::Text;
                }
            },
            State::EscapeBrace1 => match char {
                '{' => {
                    output.push('{');
                    output.push('{');
                    state = State::Text;
                }
                _ => {
                    output.push('\\');
                    output.push('{');
                    output.push(char);
                    state = State::Text;
                }
            },
            State::VariableEnd1 => match char {
                '}' => {
                    state = State::VariableEnd2;
                }
                char if char.is_whitespace() => {}
                _ => {
                    return Err(TemplateError::SyntaxError(format!(
                        "Unexpected character: {}",
                        char
                    )));
                }
            },
            State::VariableEnd2 => match char {
                '}' => {
                    state = State::Text;
                }
                char if char.is_whitespace() => {}
                _ => {
                    return Err(TemplateError::SyntaxError(format!(
                        "Unexpected character: {}",
                        char
                    )));
                }
            },
        }
    }

    match state {
        State::Text => Ok(output),
        State::Brace => {
            output.push('{');
            Ok(output)
        }
        State::Escape => {
            output.push('\\');
            Ok(output)
        }
        State::DoubleEscape => {
            output.push('\\');
            output.push('\\');
            Ok(output)
        }
        State::EscapeBrace1 => {
            output.push('\\');
            output.push('{');
            Ok(output)
        }
        State::VariableEnd1 | State::VariableEnd2 => {
            Err(TemplateError::SyntaxError("Unclosed variable".to_string()))
        }
    }
}

fn take_first(span: &str) -> Option<(char, &str)> {
    let mut chars = span.chars();
    let first = chars.next()?;
    let rest = chars.as_str();
    Some((first, rest))
}

fn strip_whitespace_left(span: &str) -> &str {
    let index = span.find(|c: char| !c.is_whitespace());
    match index {
        Some(index) => &span[index..],
        None => "",
    }
}

struct Variable {
    base: String,
    modifiers: Vec<Modifier>,
}

enum Modifier {
    Index(u64),
    Key(String),
}

fn parse_variable(input: &str) -> Result<(Variable, &str), TemplateError> {
    let (segment, input) = parse_segment(input).map_err(|e| match e {
        ParseSegmentError::UnclosedQuote => {
            TemplateError::SyntaxError("Unclosed quote".to_string())
        }
        ParseSegmentError::NoSegment => TemplateError::SyntaxError("Missing segment".to_string()),
    })?;
    let (modifiers, input) = parse_modifiers(input)?;
    Ok((
        Variable {
            base: segment,
            modifiers,
        },
        input,
    ))
}

fn parse_segment_template(input: &str) -> Result<(String, &str), TemplateError> {
    parse_segment(input).map_err(|e| match e {
        ParseSegmentError::UnclosedQuote => {
            TemplateError::SyntaxError("Unclosed quote".to_string())
        }
        ParseSegmentError::NoSegment => TemplateError::SyntaxError("Missing segment".to_string()),
    })
}

fn parse_modifiers(input: &str) -> Result<(Vec<Modifier>, &str), TemplateError> {
    let mut modifiers = Vec::new();
    let mut span = input;

    while let Ok((modifier, rest)) = parse_modifier(span) {
        modifiers.push(modifier);
        span = rest;
    }

    Ok((modifiers, span))
}

fn parse_modifier(input: &str) -> Result<(Modifier, &str), TemplateError> {
    match take_first(input) {
        Some(('.', input)) => {
            let (segment, input) = parse_segment_template(input)?;
            Ok((Modifier::Key(segment), input))
        }
        Some(('[', input)) => {
            let (modifier, input) = parse_access(input)?;
            if let Some((char, input)) = take_first(input) {
                if char == ']' {
                    Ok((modifier, input))
                } else {
                    Err(TemplateError::SyntaxError(format!(
                        "Unexpected character: {}",
                        char
                    )))
                }
            } else {
                Err(TemplateError::SyntaxError("Unexpected EOF".to_string()))
            }
        }
        Some((char, _)) => Err(TemplateError::SyntaxError(format!(
            "Unexpected character: {}",
            char
        ))),
        None => Err(TemplateError::SyntaxError("Unexpected EOF".to_string())),
    }
}

fn parse_access(input: &str) -> Result<(Modifier, &str), TemplateError> {
    match parse_integer(input) {
        Ok((index, input)) => {
            if index < 0 {
                Err(TemplateError::SyntaxError("Negative index".to_string()))
            } else {
                Ok((Modifier::Index(index as u64), input))
            }
        }
        Err(_) => {
            let (segment, input) = parse_segment_template(input)?;
            Ok((Modifier::Key(segment), input))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use serde_json::Map;

    fn map(vars: &[(&str, Value)]) -> Map<String, Value> {
        vars.iter()
            .map(|(k, v)| (k.to_string(), v.clone()))
            .collect()
    }

    #[test]
    fn test_variable_interpolation() {
        let content = "Hello, {{ user.name }}!";
        let variables = map(&[("user", json!({"name": "Alice"}))]);

        assert_eq!(template_file(content, &variables).unwrap(), "Hello, Alice!");
    }

    #[test]
    fn test_nested_variable_interpolation() {
        let content = "User age: {{ user.details.age }}";
        let variables = map(&[("user", json!({"details": {"age": 30}}))]);

        assert_eq!(template_file(content, &variables).unwrap(), "User age: 30");
    }

    #[test]
    fn test_array_indexing() {
        let content = "First item: {{ items[0] }}";
        let variables = map(&[("items", json!(["apple", "banana"]))]);

        assert_eq!(
            template_file(content, &variables).unwrap(),
            "First item: apple"
        );
    }

    #[test]
    fn test_complex_key() {
        let content = "Complex key: {{ object[\"complex key\"] }}";
        let variables = map(&[("object", json!({"complex key": "value"}))]);

        assert_eq!(
            template_file(content, &variables).unwrap(),
            "Complex key: value"
        );
    }

    #[test]
    fn test_escaped_double_braces() {
        let content = "\\{{ not_a_variable }}";
        let variables = map(&[]);

        assert_eq!(
            template_file(content, &variables).unwrap(),
            "{{ not_a_variable }}"
        );
    }

    #[test]
    fn test_literal_backslash() {
        let content = "A literal backslash: \\";
        let variables = map(&[]);

        assert_eq!(
            template_file(content, &variables).unwrap(),
            "A literal backslash: \\"
        );
    }

    #[test]
    fn test_unescaped_single_brace() {
        let content = "Something { with single braces }";
        let variables = map(&[]);

        assert_eq!(
            template_file(content, &variables).unwrap(),
            "Something { with single braces }"
        );
    }

    #[test]
    fn test_unclosed_variable() {
        let content = "Unclosed {{ variable";
        let variables = map(&[]);

        assert!(template_file(content, &variables).is_err());
    }

    #[test]
    fn test_invalid_array_index() {
        let content = "Invalid index: {{ array[-1] }}";
        let variables = map(&[("array", json!([1, 2, 3]))]);

        assert!(template_file(content, &variables).is_err());
    }

    #[test]
    fn test_text_with_no_variable() {
        let content = "Just plain text.";
        let variables = map(&[]);

        assert_eq!(
            template_file(content, &variables).unwrap(),
            "Just plain text."
        );
    }

    #[test]
    fn test_missing_variable_error() {
        let content = "Missing variable: {{ missing_var }}";
        let variables = map(&[("present_var", json!("exists"))]);

        assert!(matches!(
            template_file(content, &variables).unwrap_err(),
            TemplateError::VariableError(VariableError::MissingVariable(_))
        ));
    }

    #[test]
    fn test_type_error_on_indexing_non_array() {
        let content = "Invalid access: {{ object[0] }}";
        let variables = map(&[("object", json!({"key": "value"}))]);

        assert!(matches!(
            template_file(content, &variables).unwrap_err(),
            TemplateError::VariableError(VariableError::InvalidType(_, _))
        ));
    }

    #[test]
    fn test_escaped_variable() {
        let content = "\\\\{{ some_var }}";
        let variables = map(&[("some_var", json!("some_value"))]);

        assert_eq!(template_file(content, &variables).unwrap(), "\\some_value");
    }

    #[test]
    fn test_double_escape() {
        let content = "  \\\\";
        let variables = map(&[]);

        assert_eq!(template_file(content, &variables).unwrap(), "  \\\\");
    }
}