Skip to main content

copperlace/render/
state.rs

1use std::collections::HashMap;
2
3use super::ruleset::RuleSet;
4
5/// Initial variable bindings for one render operation.
6///
7/// Values in this map are available before top-level `context` defaults and
8/// named rules. A render may still update them with overwrite bindings such as
9/// `{alias:=rule}`.
10pub type RenderContext = HashMap<String, String>;
11
12/// Mutable state for one render operation.
13///
14/// `RuleSet::render_rule` creates a fresh state for each call. The state tracks
15/// per-render bindings, the rule call stack used for cycle detection, and the
16/// random number generator used by choice nodes.
17pub struct RenderState<'a> {
18    pub(crate) ruleset: &'a RuleSet,
19    pub(crate) context: RenderContext,
20    pub(crate) call_stack: Vec<String>,
21    pub(crate) rng: rand::rngs::ThreadRng,
22}
23
24impl<'a> RenderState<'a> {
25    /// Creates an empty render state for a ruleset.
26    pub fn new(ruleset: &'a RuleSet) -> Self {
27        Self::with_context(ruleset, RenderContext::new())
28    }
29
30    /// Creates a render state with initial variable bindings.
31    pub fn with_context(ruleset: &'a RuleSet, context: RenderContext) -> Self {
32        RenderState {
33            ruleset,
34            context,
35            call_stack: Vec::new(),
36            rng: rand::rngs::ThreadRng::default(),
37        }
38    }
39}