1use crate::filter::Filter;
2use crate::Spanned;
3use alloc::{string::String, vec::Vec};
4use core::ops::Deref;
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct Call<A = Arg, N = String> {
12 pub name: N,
14 pub args: Vec<A>,
16}
17
18impl<A, N> Call<A, N> {
19 pub fn map_args<B>(self, f: impl FnMut(A) -> B) -> Call<B, N> {
21 Call {
22 name: self.name,
23 args: self.args.into_iter().map(f).collect(),
24 }
25 }
26}
27
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
30#[derive(Clone, Debug)]
31pub struct Def<Rhs = Main> {
32 pub lhs: Call,
34 pub rhs: Rhs,
36}
37
38#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub enum Arg<V = String, F = V> {
51 Var(V),
53 Fun(F),
55}
56
57impl<T> Arg<T, T> {
58 pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Arg<U, U> {
60 match self {
61 Self::Var(x) => Arg::Var(f(x)),
62 Self::Fun(x) => Arg::Fun(f(x)),
63 }
64 }
65}
66
67impl<V, F> Arg<V, F> {
68 pub fn as_ref(&self) -> Arg<&V, &F> {
70 match self {
71 Self::Var(x) => Arg::Var(x),
72 Self::Fun(x) => Arg::Fun(x),
73 }
74 }
75}
76
77impl<V: Deref, F: Deref> Arg<V, F> {
78 pub fn as_deref(&self) -> Arg<&<V as Deref>::Target, &<F as Deref>::Target> {
80 match self {
81 Self::Var(x) => Arg::Var(x),
82 Self::Fun(x) => Arg::Fun(x),
83 }
84 }
85}
86
87impl<V, F> Arg<V, F> {
89 pub fn new_var(name: V) -> Self {
91 Self::Var(name)
92 }
93
94 pub fn new_filter(name: F) -> Self {
96 Self::Fun(name)
97 }
98
99 pub fn is_var(&self) -> bool {
101 matches!(self, Self::Var(_))
102 }
103}
104
105impl<V: Deref, F: Deref> Arg<V, F> {
107 pub fn get_var(&self) -> Option<&<V as Deref>::Target> {
109 match self {
110 Self::Var(v) => Some(v),
111 Self::Fun(_) => None,
112 }
113 }
114
115 pub fn get_filter(&self) -> Option<&<F as Deref>::Target> {
117 match self {
118 Self::Var(_) => None,
119 Self::Fun(f) => Some(f),
120 }
121 }
122}
123
124#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
126#[derive(Clone, Debug)]
127pub struct Main<F = Filter> {
128 pub defs: Vec<Def<Self>>,
130 pub body: Spanned<F>,
132}