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 pub fn arg(expr_arg: impl Into<ExprArg>) -> Self {
27 Self::Arg(expr_arg.into())
28 }
29}
30
31impl ExprArg {
32 pub fn new(position: usize) -> ExprArg {
33 ExprArg {
34 position,
35 nesting: 0,
36 }
37 }
38}
39
40impl From<usize> for ExprArg {
41 fn from(value: usize) -> Self {
42 Self {
43 position: value,
44 nesting: 0,
45 }
46 }
47}
48
49impl From<ExprArg> for Expr {
50 fn from(value: ExprArg) -> Self {
51 Self::Arg(value)
52 }
53}