carta-core 0.0.7

Shared conversion options, error types, and text/attribute helpers.
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
//! Rendering a parsed [`Template`] against a [`Value`] context.
//!
//! ## Indentation
//!
//! When a variable, partial, or loop-item value spans multiple lines and the current output line so
//! far is entirely ASCII spaces, those spaces become the indent prefixed to every continuation line
//! of the value. A line prefix containing anything else (a tab, any non-space character) suppresses
//! the indent. Literal template text is always emitted verbatim.

use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;

use super::node::{Expr, Node, Template};
use super::pipe;
use super::{TemplateError, Value};

/// Guards against unbounded partial recursion (a partial that includes itself).
const MAX_DEPTH: usize = 64;

/// A loop binding: the current element, and the bare name it is reachable under (besides `$it$`).
/// The element borrows into the root context whenever the iterated value did, so a loop over a
/// context list never copies its elements.
struct Scope<'a> {
    bind: Option<String>,
    value: Cow<'a, Value>,
}

/// A parsed-partial cache layered over the caller's name→source resolver. A partial referenced more
/// than once — across the template or from inside a loop body — is read and parsed a single time,
/// and later references reuse the parsed tree. A name that resolves to no source is remembered as
/// absent.
struct Partials<'a> {
    resolve: &'a dyn Fn(&str) -> Option<String>,
    cache: RefCell<BTreeMap<String, Option<Rc<Template>>>>,
}

impl<'a> Partials<'a> {
    fn new(resolve: &'a dyn Fn(&str) -> Option<String>) -> Self {
        Self {
            resolve,
            cache: RefCell::new(BTreeMap::new()),
        }
    }

    /// The parsed partial named `name`, or `None` when no source resolves for it. The parsed tree is
    /// cached so repeated references parse the source once; a parse error in the source propagates.
    fn get(&self, name: &str) -> Result<Option<Rc<Template>>, TemplateError> {
        if let Some(cached) = self.cache.borrow().get(name) {
            return Ok(cached.clone());
        }
        let parsed = match (self.resolve)(name) {
            // A partial drops a single trailing newline from its source, so the line a `$name()$`
            // sits on is not forced open by the partial file's own final newline.
            Some(source) => Some(Rc::new(Template::parse(
                source.strip_suffix('\n').unwrap_or(&source),
            )?)),
            None => None,
        };
        self.cache
            .borrow_mut()
            .insert(name.to_owned(), parsed.clone());
        Ok(parsed)
    }
}

/// The growing output, plus a one-bit memory of how the last text reached it.
///
/// A block-level value (a rendered body or block metadata) carries a trailing blank line. When such
/// a value sits on its own line in the template — `$body$` followed by a newline — that line's own
/// break would stack onto the value's trailing blank line and open an extra empty line. So a value
/// that ends in a newline absorbs every newline that immediately follows it: its own trailing blank
/// line stands, and the line break (or blank lines) written after it in the template are dropped.
/// Literal template text never arms this, so blank lines an author writes between literals are
/// preserved exactly.
#[derive(Default)]
struct Sink {
    buf: String,
    absorb_newline: bool,
}

impl Sink {
    /// Append literal template text verbatim, save that a preceding value's trailing newline first
    /// swallows any leading newlines of this text.
    fn push_literal(&mut self, text: &str) {
        let text = self.take_absorbed(text);
        self.buf.push_str(text);
        self.absorb_newline = false;
    }

    /// Append an interpolated value, indenting its continuation lines to a space-only current-line
    /// prefix. A preceding value's trailing newline swallows any leading newlines of this value; a
    /// value that ends in a newline arms the same rule for whatever follows.
    fn push_value(&mut self, text: &str) {
        let text = self.take_absorbed(text);
        let ends_with_newline = text.ends_with('\n');
        let indent = self.current_indent();
        if indent == 0 || !text.contains('\n') {
            self.buf.push_str(text);
        } else {
            let pad = " ".repeat(indent);
            let mut lines = text.split('\n');
            if let Some(first) = lines.next() {
                self.buf.push_str(first);
            }
            for line in lines {
                self.buf.push('\n');
                // A blank line stays blank: indenting it would leave trailing spaces on an otherwise
                // empty line, so the prefix is applied only to lines that carry content.
                if !line.is_empty() {
                    self.buf.push_str(&pad);
                    self.buf.push_str(line);
                }
            }
        }
        self.absorb_newline = ends_with_newline;
    }

    /// Drop every leading newline from `text` when a preceding value armed the rule.
    fn take_absorbed<'a>(&self, text: &'a str) -> &'a str {
        if self.absorb_newline {
            text.trim_start_matches('\n')
        } else {
            text
        }
    }

    /// The indentation to apply to a value's continuation lines: the current line's width when it is
    /// all spaces, else zero.
    fn current_indent(&self) -> usize {
        let line = match self.buf.rfind('\n') {
            Some(k) => self.buf.get(k + 1..).unwrap_or(""),
            None => self.buf.as_str(),
        };
        if !line.is_empty() && line.bytes().all(|b| b == b' ') {
            line.len()
        } else {
            0
        }
    }
}

impl Template {
    /// Render the template against `context`. `resolve_partial` maps a partial name to its source
    /// text; pass a closure returning `None` for templates that use no partials.
    ///
    /// # Errors
    /// [`TemplateError`] when a referenced partial cannot be resolved (`resolve_partial` returns
    /// `None` for a name the template actually uses).
    pub fn render(
        &self,
        context: &Value,
        resolve_partial: &dyn Fn(&str) -> Option<String>,
    ) -> Result<String, TemplateError> {
        let partials = Partials::new(resolve_partial);
        let mut sink = Sink::default();
        let mut scopes = Vec::new();
        render_nodes(&self.nodes, context, &mut scopes, &partials, 0, &mut sink)?;
        Ok(sink.buf)
    }
}

fn render_nodes<'a>(
    nodes: &[Node],
    ctx: &'a Value,
    scopes: &mut Vec<Scope<'a>>,
    partials: &Partials<'_>,
    depth: usize,
    out: &mut Sink,
) -> Result<(), TemplateError> {
    for node in nodes {
        render_node(node, ctx, scopes, partials, depth, out)?;
    }
    Ok(())
}

fn render_node<'a>(
    node: &Node,
    ctx: &'a Value,
    scopes: &mut Vec<Scope<'a>>,
    partials: &Partials<'_>,
    depth: usize,
    out: &mut Sink,
) -> Result<(), TemplateError> {
    match node {
        Node::Literal(text) => out.push_literal(text),
        Node::Var(expr) => {
            if let Some(value) = eval(expr, ctx, scopes) {
                out.push_value(&pipe::stringify(&value));
            } else if !expr.pipes.is_empty() {
                // An absent path is an empty value; its pipe chain still applies, so `$x/length$`
                // on a missing `x` yields `0` rather than vanishing.
                let mut value = Value::Str(String::new());
                for filter in &expr.pipes {
                    value = pipe::apply(&value, filter);
                }
                out.push_value(&pipe::stringify(&value));
            }
        }
        Node::If {
            branches,
            otherwise,
        } => {
            for (cond, body) in branches {
                if eval(cond, ctx, scopes)
                    .as_deref()
                    .is_some_and(Value::is_truthy)
                {
                    return render_nodes(body, ctx, scopes, partials, depth, out);
                }
            }
            render_nodes(otherwise, ctx, scopes, partials, depth, out)?;
        }
        Node::For {
            expr,
            bind,
            body,
            sep,
        } => render_for(
            expr,
            bind.as_ref(),
            body,
            sep,
            ctx,
            scopes,
            partials,
            depth,
            out,
        )?,
        Node::Partial {
            name,
            map_over,
            sep,
        } => render_partial(
            name,
            map_over.as_ref(),
            sep.as_ref(),
            ctx,
            scopes,
            partials,
            depth,
            out,
        )?,
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn render_for<'a>(
    expr: &Expr,
    bind: Option<&String>,
    body: &[Node],
    sep: &[Node],
    ctx: &'a Value,
    scopes: &mut Vec<Scope<'a>>,
    partials: &Partials<'_>,
    depth: usize,
    out: &mut Sink,
) -> Result<(), TemplateError> {
    let Some(items) = eval_items(expr, ctx, scopes).map(into_items) else {
        return Ok(());
    };
    for (i, item) in items.into_iter().enumerate() {
        if i > 0 {
            render_nodes(sep, ctx, scopes, partials, depth, out)?;
        }
        scopes.push(Scope {
            bind: bind.cloned(),
            value: item,
        });
        let result = render_nodes(body, ctx, scopes, partials, depth, out);
        scopes.pop();
        result?;
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn render_partial<'a>(
    name: &str,
    map_over: Option<&Expr>,
    sep: Option<&String>,
    ctx: &'a Value,
    scopes: &mut Vec<Scope<'a>>,
    partials: &Partials<'_>,
    depth: usize,
    out: &mut Sink,
) -> Result<(), TemplateError> {
    if depth >= MAX_DEPTH {
        return Ok(());
    }
    let Some(template) = partials.get(name)? else {
        return Err(TemplateError::new(format!(
            "partial `{name}` could not be found"
        )));
    };
    match map_over {
        None => {
            let rendered = render_to_string(&template.nodes, ctx, scopes, partials, depth + 1)?;
            out.push_value(&rendered);
        }
        Some(expr) => {
            let Some(items) = eval_items(expr, ctx, scopes).map(into_items) else {
                return Ok(());
            };
            let separator = sep.cloned().unwrap_or_default();
            let mut pieces = Vec::new();
            for item in items {
                scopes.push(Scope {
                    bind: None,
                    value: item,
                });
                let result = render_to_string(&template.nodes, ctx, scopes, partials, depth + 1);
                scopes.pop();
                pieces.push(result?);
            }
            out.push_value(&pieces.join(&separator));
        }
    }
    Ok(())
}

/// Render `nodes` into an independent string, used for a partial's body before it is interpolated as
/// a single value into the surrounding output.
fn render_to_string<'a>(
    nodes: &[Node],
    ctx: &'a Value,
    scopes: &mut Vec<Scope<'a>>,
    partials: &Partials<'_>,
    depth: usize,
) -> Result<String, TemplateError> {
    let mut sink = Sink::default();
    render_nodes(nodes, ctx, scopes, partials, depth, &mut sink)?;
    Ok(sink.buf)
}

/// A scalar or map iterates as a single element; a list iterates its elements — by reference when
/// the list itself was borrowed, so its elements are not copied into their loop scopes.
fn into_items(value: Cow<'_, Value>) -> Vec<Cow<'_, Value>> {
    match value {
        Cow::Borrowed(Value::List(items)) => items.iter().map(Cow::Borrowed).collect(),
        Cow::Borrowed(other) => vec![Cow::Borrowed(other)],
        Cow::Owned(Value::List(items)) => items.into_iter().map(Cow::Owned).collect(),
        Cow::Owned(other) => vec![Cow::Owned(other)],
    }
}

/// Resolve an expression to a value, applying its pipes. `None` means the path is absent.
fn eval<'a>(expr: &Expr, ctx: &'a Value, scopes: &'a [Scope<'_>]) -> Option<Cow<'a, Value>> {
    let base = lookup(&expr.path, ctx, scopes)?;
    if expr.pipes.is_empty() {
        return Some(Cow::Borrowed(base));
    }
    let mut value = Cow::Borrowed(base);
    for filter in &expr.pipes {
        value = Cow::Owned(pipe::apply(value.as_ref(), filter));
    }
    Some(value)
}

/// Resolve an expression for iteration. Unlike [`eval`], the returned borrow is independent of the
/// scope stack, so per-element loop scopes can be pushed while the elements stay borrowed: a
/// pipe-free path whose chain stays within borrowed values borrows straight into the root context,
/// and a chain passing through an owned loop element clones its result instead.
fn eval_items<'a>(expr: &Expr, ctx: &'a Value, scopes: &[Scope<'a>]) -> Option<Cow<'a, Value>> {
    let mut value = lookup_long(&expr.path, ctx, scopes)?;
    for filter in &expr.pipes {
        value = Cow::Owned(pipe::apply(value.as_ref(), filter));
    }
    Some(value)
}

/// Walk a dotted path. The head segment resolves against loop scopes (`it`, then bound names) before
/// the root context; the rest descends through maps.
fn lookup<'a>(path: &[String], ctx: &'a Value, scopes: &'a [Scope<'_>]) -> Option<&'a Value> {
    let (head, rest) = path.split_first()?;
    let base = if head == "it"
        && let Some(scope) = scopes.last()
    {
        scope.value.as_ref()
    } else if let Some(scope) = scopes
        .iter()
        .rev()
        .find(|s| s.bind.as_deref() == Some(head.as_str()))
    {
        scope.value.as_ref()
    } else if let Value::Map(map) = ctx {
        map.get(head)?
    } else {
        return None;
    };
    descend(base, rest)
}

/// Walk a dotted path like [`lookup`], but return a value whose borrow outlives the scope stack.
fn lookup_long<'a>(
    path: &[String],
    ctx: &'a Value,
    scopes: &[Scope<'a>],
) -> Option<Cow<'a, Value>> {
    let (head, rest) = path.split_first()?;
    let scope_base = if head == "it"
        && let Some(scope) = scopes.last()
    {
        Some(&scope.value)
    } else {
        scopes
            .iter()
            .rev()
            .find(|s| s.bind.as_deref() == Some(head.as_str()))
            .map(|scope| &scope.value)
    };
    match scope_base {
        Some(Cow::Borrowed(base)) => descend(base, rest).map(Cow::Borrowed),
        Some(Cow::Owned(base)) => descend(base, rest).map(|found| Cow::Owned(found.clone())),
        None => match ctx {
            Value::Map(map) => descend(map.get(head)?, rest).map(Cow::Borrowed),
            _ => None,
        },
    }
}

/// Descend through nested maps, one path segment at a time.
fn descend<'v>(mut current: &'v Value, rest: &[String]) -> Option<&'v Value> {
    for segment in rest {
        match current {
            Value::Map(map) => current = map.get(segment)?,
            _ => return None,
        }
    }
    Some(current)
}