1use super::Expression;
2use crate::format;
3use crate::Identifier;
4use serde::Deserialize;
5use std::fmt;
6
7#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
9pub struct FuncName {
10 pub namespace: Vec<Identifier>,
12 pub name: Identifier,
14}
15
16impl FuncName {
17 pub fn new(name: impl Into<Identifier>) -> FuncName {
19 FuncName {
20 namespace: Vec::new(),
21 name: name.into(),
22 }
23 }
24
25 pub fn with_namespace<I>(mut self, namespace: I) -> FuncName
27 where
28 I: IntoIterator,
29 I::Item: Into<Identifier>,
30 {
31 self.namespace = namespace.into_iter().map(Into::into).collect();
32 self
33 }
34
35 pub fn is_namespaced(&self) -> bool {
37 !self.namespace.is_empty()
38 }
39}
40
41impl<T> From<T> for FuncName
42where
43 T: Into<Identifier>,
44{
45 fn from(name: T) -> Self {
46 FuncName {
47 namespace: Vec::new(),
48 name: name.into(),
49 }
50 }
51}
52
53impl fmt::Display for FuncName {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 let formatted = format::to_string(self).expect("a FuncName failed to format unexpectedly");
57 f.write_str(&formatted)
58 }
59}
60
61#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
63pub struct FuncCall {
64 pub name: FuncName,
66 pub args: Vec<Expression>,
68 pub expand_final: bool,
71}
72
73impl FuncCall {
74 pub fn new<T>(name: T) -> FuncCall
76 where
77 T: Into<FuncName>,
78 {
79 FuncCall {
80 name: name.into(),
81 args: Vec::new(),
82 expand_final: false,
83 }
84 }
85
86 pub fn builder<T>(name: T) -> FuncCallBuilder
88 where
89 T: Into<FuncName>,
90 {
91 FuncCallBuilder {
92 f: FuncCall::new(name),
93 }
94 }
95}
96
97#[derive(Debug)]
99pub struct FuncCallBuilder {
100 f: FuncCall,
101}
102
103impl FuncCallBuilder {
104 pub fn arg<T>(mut self, arg: T) -> FuncCallBuilder
106 where
107 T: Into<Expression>,
108 {
109 self.f.args.push(arg.into());
110 self
111 }
112
113 pub fn expand_final(mut self, yes: bool) -> FuncCallBuilder {
116 self.f.expand_final = yes;
117 self
118 }
119
120 pub fn build(self) -> FuncCall {
122 self.f
123 }
124}