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
use crate::{Annotation, Block, FunctionInput, Identifier, Node, Span, Type};
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Serialize, Deserialize)]
pub struct Function {
pub annotations: Vec<Annotation>,
pub identifier: Identifier,
pub input: Vec<FunctionInput>,
pub output: Option<Type>,
pub block: Block,
pub span: Span,
}
impl PartialEq for Function {
fn eq(&self, other: &Self) -> bool {
self.identifier == other.identifier
}
}
impl Eq for Function {}
impl Function {
pub fn get_name(&self) -> &str {
&self.identifier.name
}
pub fn contains_self(&self) -> bool {
self.input.iter().any(|param| param.is_self())
}
pub fn contains_mut_self(&self) -> bool {
self.input.iter().any(|param| param.is_mut_self())
}
pub fn filter_self_inputs(&self) -> impl Iterator<Item = &FunctionInput> {
self.input.iter().filter(|input| !input.is_self())
}
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "function {}", self.identifier)?;
let parameters = self.input.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(",");
let returns = self.output.as_ref().map(|type_| type_.to_string());
if returns.is_none() {
write!(f, "({}) {}", parameters, self.block)
} else {
write!(f, "({}) -> {} {}", parameters, returns.unwrap(), self.block)
}
}
}
impl fmt::Display for Function {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f)
}
}
impl fmt::Debug for Function {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f)
}
}
impl Node for Function {
fn span(&self) -> &Span {
&self.span
}
fn set_span(&mut self, span: Span) {
self.span = span;
}
}