nadi_core 0.8.1

Core library for Nadi systems, for use by plugins
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
use crate::attrs::{Attribute, HasAttributes};
use std::str::FromStr;

/// String template to use for NADI DSL
///
/// The template can contain variable parts inside `{}` with optional
/// format strings after `:`
#[derive(Clone, Debug)]
pub struct Template {
    pub parts: Vec<TemplatePart>,
    pub positions: Vec<usize>,
    pub original: String,
}

impl FromStr for Template {
    type Err = TemplateError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_template(s)
    }
}

impl std::cmp::PartialEq for Template {
    fn eq(&self, other: &Self) -> bool {
        std::cmp::PartialEq::eq(&self.original, &other.original)
    }
}

impl Template {
    pub fn original(&self) -> &str {
        &self.original
    }

    pub fn has_variables(&self) -> bool {
        self.parts
            .iter()
            .any(|p| matches!(p, TemplatePart::Variable(..)))
    }

    pub fn lit(&self) -> Option<String> {
        self.parts
            .iter()
            .map(TemplatePart::lit)
            .collect::<Option<Vec<&str>>>()
            .map(|v| v.join(""))
    }
}

impl Template {
    /// Render the given template using the attribute values
    ///
    /// The attributes will be available to be used in the template
    /// based on the following rules:
    /// - [TODO] String attributes will be quoted, extra variable with `_`
    ///   prefix will be available to use unquoted string variables,
    /// - nested variables will be available using the `.` separator
    /// - all other variables will be available with their name,
    ///   their value will be their string representation.
    pub fn render<T: HasAttributes>(&self, attrmap: &T) -> Result<String, TemplateError> {
        let mut res = String::new();
        for (part, pos) in self.parts.iter().zip(&self.positions) {
            match part {
                TemplatePart::Literal(s) => res.push_str(s),
                TemplatePart::Variable(var) => match attrmap.attr_dot(&var.name) {
                    Ok(Some(v)) => {
                        res.push_str(&var.format.as_ref().map(|f| f.format(v)).unwrap_or(v.repr()))
                    }
                    Ok(None) if var.optional => {
                        res.push_str(&var.format.as_ref().map(|f| f.empty()).unwrap_or_default())
                    }
                    Ok(None) => {
                        return Err(TemplateError {
                            pos: *pos,
                            ty: TemplateErrorType::AttributeNotFound(var.name.to_string()),
                        });
                    }
                    Err(e) => {
                        return Err(TemplateError {
                            pos: *pos,
                            ty: TemplateErrorType::AttributeError(e),
                        });
                    }
                },
            }
        }
        Ok(res)
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum TemplatePart {
    Literal(String),
    Variable(TemplateVar),
}

impl TemplatePart {
    pub fn lit(&self) -> Option<&str> {
        match self {
            Self::Literal(s) => Some(s.as_str()),
            Self::Variable(_) => None,
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct TemplateVar {
    name: String,
    optional: bool,
    format: Option<VarFormat>,
}

#[derive(Default, Debug, Clone, PartialEq)]
pub struct VarFormat {
    len: Option<usize>,
    sig: Option<usize>,
    pad: Option<char>,
    align: Align,
    quote: Option<char>,
}

impl VarFormat {
    fn empty(&self) -> String {
        self.quote(self.align(String::new()))
    }

    fn quote(&self, s: String) -> String {
        if let Some(q) = self.quote {
            format!("{q}{}{q}", s.replace(q, &format!("\\{q}")))
        } else {
            s
        }
    }

    fn align(&self, s: String) -> String {
        if let Some(width) = self.len {
            if width <= s.len() {
                return s;
            }
            let pad_len = width - s.len();
            let pad_char = self.pad.unwrap_or(' ');
            let pad = pad_char.to_string().repeat(pad_len);
            match self.align {
                Align::Left => format!("{s}{pad}"),
                Align::Right => format!("{pad}{s}"),
                Align::Center => {
                    // Split padding evenly on both sides
                    let left = pad_len / 2;
                    let right = pad_len - left;
                    format!(
                        "{}{}{}",
                        pad_char.to_string().repeat(left),
                        s,
                        pad_char.to_string().repeat(right)
                    )
                }
            }
        } else {
            s
        }
    }

    fn format(&self, val: &Attribute) -> String {
        match val {
            Attribute::Bool(b) => self.quote(b.to_string()),
            Attribute::Integer(i) => {
                let num = if let Some(s) = &self.sig {
                    format!("{i}.{}", "0".repeat(*s))
                } else {
                    i.to_string()
                };
                self.quote(self.align(num))
            }
            Attribute::Float(f) => {
                let num = if let Some(s) = &self.sig {
                    format!("{f:.s$}")
                } else {
                    f.to_string()
                };
                self.quote(self.align(num))
            }
            Attribute::String(v) => self.quote(self.align(v.to_string())),
            Attribute::Date(v) => self.quote(self.align(v.to_string())),
            Attribute::Time(v) => self.quote(self.align(v.to_string())),
            Attribute::DateTime(v) => self.quote(self.align(v.to_string())),
            x => x.repr(),
        }
    }
}

#[derive(Default, Debug, Clone, PartialEq)]
pub enum Align {
    Left,
    #[default]
    Right,
    Center,
}

#[derive(PartialEq, Debug, Clone)]
pub struct TemplateError {
    pub pos: usize,
    pub ty: TemplateErrorType,
}

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

impl std::fmt::Display for TemplateError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "TemplateError at pos {}: {}", self.pos, self.ty)
    }
}

#[derive(PartialEq, Clone, Debug)]
pub enum TemplateErrorType {
    Incomplete,
    InvalidChar(char),
    AttributeNotFound(String),
    AttributeError(String),
}

impl std::fmt::Display for TemplateErrorType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Incomplete => write!(f, "Incomplete Template"),
            Self::InvalidChar(c) => write!(f, "Invalid Character '{}'", c),
            Self::AttributeNotFound(s) => write!(f, "Attribute {:?} not found", s),
            Self::AttributeError(s) => write!(f, "Attribute Error: {}", s),
        }
    }
}

#[derive(PartialEq)]
enum ParseState {
    Literal,
    Variable,
    Format,
}

fn parse_template(val: &str) -> Result<Template, TemplateError> {
    let mut state = ParseState::Literal;
    let mut chars = val.chars().peekable();
    let mut data = String::new();
    let mut parts = Vec::new();
    let mut positions = Vec::new();
    let mut pos: usize = 0;
    let mut lastpos: usize = 0;
    let mut fmt = None;
    while let Some(c) = chars.next() {
        pos += 1;
        match c {
            '\\' => {
                if let Some(x) = chars.next() {
                    data.push(x);
                } else {
                    return Err(TemplateError {
                        pos,
                        ty: TemplateErrorType::Incomplete,
                    });
                }
            }
            '{' => match state {
                ParseState::Literal => {
                    if !data.is_empty() {
                        parts.push(TemplatePart::Literal(data.clone()));
                        positions.push(lastpos);
                    }
                    data.clear();
                    state = ParseState::Variable;
                    lastpos = pos;
                }
                ParseState::Variable | ParseState::Format => {
                    return Err(TemplateError {
                        pos,
                        ty: TemplateErrorType::InvalidChar(c),
                    });
                }
            },
            '}' => match state {
                ParseState::Variable | ParseState::Format => {
                    if !data.is_empty() {
                        let (name, opt) = if let Some(data) = data.strip_suffix("?") {
                            (data.to_string(), true)
                        } else {
                            (data.clone(), false)
                        };
                        parts.push(TemplatePart::Variable(TemplateVar {
                            name,
                            optional: opt,
                            format: fmt.take(),
                        }));
                        positions.push(lastpos);
                    }
                    data.clear();
                    state = ParseState::Literal;
                    lastpos = pos;
                }
                ParseState::Literal => {
                    return Err(TemplateError {
                        pos,
                        ty: TemplateErrorType::InvalidChar(c),
                    });
                }
            },
            ':' if state == ParseState::Variable => {
                fmt = Some(VarFormat::default());
                state = ParseState::Format;
            }
            _ => {
                if matches!(state, ParseState::Format) {
                    if let Some(f) = &mut fmt {
                        match c {
                            '"' | '\'' => f.quote = Some(c),
                            '<' => f.align = Align::Left,
                            '>' => f.align = Align::Right,
                            '^' => f.align = Align::Center,
                            '.' => {
                                let mut w = String::new();
                                while let Some(d) = chars.peek() {
                                    if d.is_ascii_digit() {
                                        w.push(*d);
                                        chars.next();
                                    } else {
                                        break;
                                    }
                                }
                                if let Ok(n) = w.parse::<usize>() {
                                    f.sig = Some(n);
                                }
                            }
                            c if c.is_ascii_digit() => {
                                // width – read all following digits
                                let mut w = String::new();
                                w.push(c);
                                while let Some(d) = chars.peek() {
                                    if d.is_ascii_digit() {
                                        w.push(*d);
                                        chars.next();
                                    } else {
                                        break;
                                    }
                                }
                                if let Ok(n) = w.parse::<usize>() {
                                    f.len = Some(n);
                                }
                            }
                            _ => {
                                return Err(TemplateError {
                                    pos,
                                    ty: TemplateErrorType::InvalidChar(c),
                                });
                            }
                        }
                    }
                } else {
                    data.push(c)
                }
            }
        }
    }
    match state {
        // if we're still reading variable it can't end
        ParseState::Literal => {
            if !data.is_empty() {
                parts.push(TemplatePart::Literal(data.clone()));
                positions.push(lastpos);
            }
        }
        ParseState::Variable | ParseState::Format => {
            return Err(TemplateError {
                pos,
                ty: TemplateErrorType::Incomplete,
            });
        }
    }
    Ok(Template {
        parts,
        positions,
        original: val.to_string(),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prelude::AttrMap;
    use nadi_core::attr_map;
    use rstest::rstest;
    use std::str::FromStr;

    #[rstest]
    #[case("")] // empty string
    #[case("hello")] // plain text
    #[case("{foo}")] // simple placeholder
    #[case("{foo:0.2}")] // placeholder with numeric format
    #[case("{foo:>10}")] // placeholder with alignment
    // #[case("{foo:?}")] // placeholder with optional render
    #[case("{foo:01}")] // placeholder with zero‑padding
    #[case("{foo:>10.3}")] // complex format
    #[case("{foo}bar{baz}")] // multiple placeholders
    #[case("{foo}  {bar}")] // placeholders separated by spaces
    #[case("{foo}  ")] // trailing spaces
    #[case("{foo} {bar} ")] // leading/trailing spaces
    #[case("{foo}{bar}")] // back‑to‑back placeholders
    #[case("{foo}{bar} baz")] // placeholder followed by text
    #[case("{foo?}baz{bar}")] // text between placeholders
    #[case("{foo}{bar}{baz}")] // three placeholders
    #[case("{foo:}")] // placeholder with an *empty* format
    #[case("{foo?:0}")] // placeholder with zero format
    // #[case("{foo: >10}")] // alignment with space
    #[case("{foo:>10}")] // alignment to the right
    #[case("{foo?:<10}")] // alignment to the left
    #[case("{foo:0.3}")] // float format
    #[case("{foo:06}")] // zero padding
    #[case("{foo:5}")] // minimal width
    #[case("{foo:<5.2}")] // left‑aligned, precision
    #[case("{foo:>5.2}")] // right‑aligned, precision
    fn is_valid_template(#[case] templ: &str) {
        Template::from_str(templ).unwrap();
    }

    #[rstest]
    #[case("{foo")] // missing closing brace
    #[case("foo}")] // missing opening brace
    #[case("{{foo}")] // unclosed nested brace
    #[case("{foo}{bar")] // missing closing brace
    #[case("{foo:}bar}")] // stray closing brace
    #[case("{foo:}bar}}")] // double stray brace
    #[case("{foo::}")] // double colon
    #[case("{foo:0x:}")] // two format specifiers
    #[case("{foo:??}")] // unknown format specifier
    #[case("{foo::}")] // colon with nothing after
    #[case("{foo}bar{")] // trailing open brace
    #[case("{foo}bar}")] // stray closing brace at end
    #[case("{foo}bar{baz}}")] // mismatched brackets
    #[case("{foo}{bar}baz{")] // unmatched opening brace
    #[case("{{{")] // too many opening braces
    #[case("}}}")] // too many closing braces
    #[case("{foo}{{bar}")] // escape attempt but wrong
    fn test_invalid_template(#[case] templ: &str) {
        assert!(Template::from_str(templ).is_err());
    }

    #[rstest]
    #[case("", attr_map!(), "")]
    #[case("\\{", attr_map!(), "{")]
    #[case("\\}", attr_map!(), "}")]
    #[case("something", attr_map!(), "something")]
    #[case("{sething}", attr_map!(sething => 0), "0")]
    #[case("som{eng}", attr_map!(eng => 21), "som21")]
    #[case("SOME{THING}", attr_map!(THING => "what"), "SOMEwhat")]
    #[case("{So:0.2}Me", attr_map!(So => 1.2343), "1.23Me")]
    #[case("{someng:}", attr_map!(someng => true), "true")]
    #[case(" {SoMe} ", attr_map!(SoMe => 1), " 1 ")]
    #[case("{foo}",  attr_map!(foo => "bar"), "bar")]
    #[case("{foo}",  attr_map!(foo => 42),   "42")]
    #[case("{foo}",  attr_map!(foo => 3.11), "3.11")]
    #[case("{foo}",  attr_map!(foo => true), "true")]
    #[case("{foo}",  attr_map!(foo => vec![1,2,3]), "[1, 2, 3]")]
    #[case("{foo:0.2}", attr_map!(foo => 3.123), "3.12")]
    #[case("{foo:0.1}", attr_map!(foo => 3.123), "3.1")]
    #[case("{foo:0.0}", attr_map!(foo => 3.123), "3")]
    #[case("{foo:>10}", attr_map!(foo => "hi"), "        hi")]
    #[case("{foo:<10}", attr_map!(foo => "hi"), "hi        ")]
    #[case("{foo:^10}", attr_map!(foo => "hi"), "    hi    ")]
    // #[case("{foo:0?}", attr_map!(foo => "test"), "\"test\"")]
    #[case("{foo:1.2}", attr_map!(foo => 1.2345), "1.23")]
    #[case("{foo:0.2}", attr_map!(foo => 1), "1.00")]
    // Zero pad not implemented
    // #[case("{foo:02}", attr_map!(foo => 7), "07")]
    // #[case("{foo:02}", attr_map!(foo => 42), "42")]
    #[case("{foo:>5}", attr_map!(foo => 3), "    3")]
    #[case("{foo:>5.2}", attr_map!(foo => 3.1), " 3.10")]
    #[case("{foo:>5.2}", attr_map!(foo => 3.1615), " 3.16")]
    #[should_panic]
    #[case("{foo}", attr_map!(), "???")] // placeholder missing – expected placeholder‑token
    #[should_panic]
    #[case("{foo:0.2}", attr_map!(), "???")] // missing placeholder with format
    #[case("{foo}{bar}", attr_map!(foo => "a", bar => "b"), "ab")]
    #[should_panic]
    #[case("{foo}{bar}", attr_map!(foo => "a"), "a???")] // bar missing
    #[should_panic]
    #[case("{foo}{bar}", attr_map!(bar => "b"), "???b")] // foo missing
    #[case("{foo}{bar}{baz}", attr_map!(foo => "x", bar => "y", baz => "z"), "xyz")]
    #[should_panic]
    #[case("{foo}{bar}{baz}", attr_map!(foo => "x", bar => "y"), "xy???")]
    #[case("{foo}  {bar}", attr_map!(foo => "x", bar => "y"), "x  y")]
    #[case("{foo}{bar} ", attr_map!(foo => "x", bar => "y"), "xy ")]
    #[case("{foo:0.2} {bar:0.2}", attr_map!(foo => 1.2345, bar => 2.3456), "1.23 2.35")]
    #[case("{foo:>10}bar{bar}", attr_map!(foo => "hi", bar => "z"), "        hibarz")]
    #[case("\\{{foo}\\}", attr_map!(foo => "baz"), "{baz}")] // literal opening brace
    #[case("{foo}\\{bar\\}", attr_map!(foo => "x", bar => "y"), "x{bar}")] // test nested escape? (implementation dependent)
    // Make sure different syntax can be used
    #[case::typst("#image(\"corrs/{NAME}.png\", height: 1in)", attr_map!(NAME => "x"), "#image(\"corrs/x.png\", height: 1in)")]
    fn render_template(#[case] templ: &str, #[case] values: AttrMap, #[case] res: String) {
        let templ = Template::from_str(templ).unwrap();
        let rend = templ.render(&values).unwrap();
        assert_eq!(rend, res);
    }

    #[rstest]
    #[case("\\{{foo?}\\}", attr_map!(foo => "baz"), "{baz}")]
    #[case("\\{{foo?}\\}", attr_map!(), "{}")]
    #[case("{foo?}", attr_map!(bar => "baz"), "")]
    #[case("{foo?}\\{bar\\}", attr_map!(foo => "x", bar => "y"), "x{bar}")]
    fn render_optional_template(#[case] templ: &str, #[case] values: AttrMap, #[case] res: String) {
        let templ = Template::from_str(templ).unwrap();
        let rend = templ.render(&values).unwrap();
        assert_eq!(rend, res);
    }
}