toasty_core/stmt/
expr_arg.rs

1use super::Expr;
2
3/// A positional argument placeholder.
4///
5/// Represents a reference to an input value by position. During substitution,
6/// `arg(n)` is replaced with the nth value from the input.
7///
8/// # Examples
9///
10/// ```text
11/// arg(0)  // refers to the first input value
12/// arg(1)  // refers to the second input value
13/// ```
14#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
15pub struct ExprArg {
16    /// The zero-based position of the argument.
17    pub position: usize,
18
19    /// Which "argument scope" this references. This is the number of scopes up
20    /// from the current scope. Scopes are created by functional expressions
21    /// like Expr::Map.
22    pub nesting: usize,
23}
24
25impl Expr {
26    /// Creates an argument expression from a value convertible to [`ExprArg`].
27    ///
28    /// A `usize` can be passed directly: `Expr::arg(0)` creates `arg(position=0, nesting=0)`.
29    pub fn arg(expr_arg: impl Into<ExprArg>) -> Self {
30        Self::Arg(expr_arg.into())
31    }
32}
33
34impl ExprArg {
35    /// Creates a new argument at the given position with `nesting = 0`.
36    pub fn new(position: usize) -> ExprArg {
37        ExprArg {
38            position,
39            nesting: 0,
40        }
41    }
42}
43
44impl From<usize> for ExprArg {
45    fn from(value: usize) -> Self {
46        Self {
47            position: value,
48            nesting: 0,
49        }
50    }
51}
52
53impl From<ExprArg> for Expr {
54    fn from(value: ExprArg) -> Self {
55        Self::Arg(value)
56    }
57}