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
use crate::filter::Filter;
use crate::Spanned;
use alloc::{string::String, vec::Vec};
use core::ops::Deref;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Call to a filter identified by a name type `N` with arguments of type `A`.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Call<A = Arg, N = String> {
    /// Name of the filter, e.g. `map`
    pub name: N,
    /// Arguments of the filter, e.g. `["f"]`
    pub args: Vec<A>,
}

impl<A, N> Call<A, N> {
    /// Apply a function to the call arguments.
    pub fn map_args<B>(self, f: impl FnMut(A) -> B) -> Call<B, N> {
        Call {
            name: self.name,
            args: self.args.into_iter().map(f).collect(),
        }
    }
}

/// A definition, such as `def map(f): [.[] | f];`.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct Def<Rhs = Main> {
    /// left-hand side, i.e. what shall be defined, e.g. `map(f)`
    pub lhs: Call,
    /// right-hand side, i.e. what the LHS should be defined as, e.g. `[.[] | f]`
    pub rhs: Rhs,
}

/// Argument of a definition, such as `$v` or `f` in `def foo($v; f): ...`.
///
/// In jq, we can bind filters in three different ways:
///
/// 1. `f as $x | ...`
/// 2. `def g($x): ...; g(f)`
/// 3. `def g(fx): ...; g(f)`
///
/// In the first two cases, we bind the outputs of `f` to a variable `$x`.
/// In the third case, we bind `f` to a filter `fx`
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Arg<V = String, F = V> {
    /// binding to a variable
    Var(V),
    /// binding to a filter
    Fun(F),
}

impl<T> Arg<T, T> {
    /// Apply a function to both binding types.
    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Arg<U, U> {
        match self {
            Self::Var(x) => Arg::Var(f(x)),
            Self::Fun(x) => Arg::Fun(f(x)),
        }
    }
}

impl<V, F> Arg<V, F> {
    /// Move references inward.
    pub fn as_ref(&self) -> Arg<&V, &F> {
        match self {
            Self::Var(x) => Arg::Var(x),
            Self::Fun(x) => Arg::Fun(x),
        }
    }
}

impl<V: Deref, F: Deref> Arg<V, F> {
    /// Move references inward, while deferencing content.
    pub fn as_deref(&self) -> Arg<&<V as Deref>::Target, &<F as Deref>::Target> {
        match self {
            Self::Var(x) => Arg::Var(x),
            Self::Fun(x) => Arg::Fun(x),
        }
    }
}

// TODO for v2.0: remove this
impl<V, F> Arg<V, F> {
    /// Create a variable argument with given name (without leading "$").
    pub fn new_var(name: V) -> Self {
        Self::Var(name)
    }

    /// Create a filter argument with given name.
    pub fn new_filter(name: F) -> Self {
        Self::Fun(name)
    }

    /// True if the argument is a variable.
    pub fn is_var(&self) -> bool {
        matches!(self, Self::Var(_))
    }
}

// TODO for v2.0: remove this
impl<V: Deref, F: Deref> Arg<V, F> {
    /// If the argument is a variable, return its name without leading "$", otherwise `None`.
    pub fn get_var(&self) -> Option<&<V as Deref>::Target> {
        match self {
            Self::Var(v) => Some(v),
            Self::Fun(_) => None,
        }
    }

    /// If the argument is a filter, return its name, otherwise `None`.
    pub fn get_filter(&self) -> Option<&<F as Deref>::Target> {
        match self {
            Self::Var(_) => None,
            Self::Fun(f) => Some(f),
        }
    }
}

/// (Potentially empty) sequence of definitions, followed by a filter.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct Main<F = Filter> {
    /// Definitions at the top of the filter
    pub defs: Vec<Def<Self>>,
    /// Body of the filter, e.g. `[.[] | f`.
    pub body: Spanned<F>,
}