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
use super::Expr;
/// A positional argument placeholder.
///
/// Represents a reference to an input value by position. During substitution,
/// `arg(n)` is replaced with the nth value from the input.
///
/// # Examples
///
/// ```text
/// arg(0) // refers to the first input value
/// arg(1) // refers to the second input value
/// ```
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub struct ExprArg {
/// The zero-based position of the argument.
pub position: usize,
/// Which "argument scope" this references. This is the number of scopes up
/// from the current scope. Scopes are created by functional expressions
/// like Expr::Map.
pub nesting: usize,
}
impl Expr {
/// Creates an argument expression from a value convertible to [`ExprArg`].
///
/// A `usize` can be passed directly: `Expr::arg(0)` creates `arg(position=0, nesting=0)`.
pub fn arg(expr_arg: impl Into<ExprArg>) -> Self {
Self::Arg(expr_arg.into())
}
}
impl ExprArg {
/// Creates a new argument at the given position with `nesting = 0`.
pub fn new(position: usize) -> ExprArg {
ExprArg {
position,
nesting: 0,
}
}
}
impl From<usize> for ExprArg {
fn from(value: usize) -> Self {
Self {
position: value,
nesting: 0,
}
}
}
impl From<ExprArg> for Expr {
fn from(value: ExprArg) -> Self {
Self::Arg(value)
}
}