1use crate::{Identifier, Node, NodeID, Type, indent_display::Indent};
18use leo_span::{Span, Symbol};
19use serde::{Deserialize, Serialize};
20use std::fmt;
21
22pub use prototypes::{FunctionPrototype, MappingPrototype, RecordPrototype, StorageVariablePrototype};
23
24mod prototypes;
25
26#[derive(Clone, Default, Serialize, Deserialize)]
28pub struct Interface {
29 pub identifier: Identifier,
31 pub parents: Vec<(Span, Type)>,
33 pub span: Span,
35 pub id: NodeID,
37 pub functions: Vec<(Symbol, FunctionPrototype)>,
39 pub records: Vec<(Symbol, RecordPrototype)>,
41 pub mappings: Vec<MappingPrototype>,
43 pub storages: Vec<StorageVariablePrototype>,
45}
46
47impl Interface {
48 pub fn name(&self) -> Symbol {
49 self.identifier.name
50 }
51
52 pub fn is_record_type(&self, ty: &Type) -> bool {
54 if let Type::Composite(ct) = ty
55 && let Some(loc) = ct.path.try_global_location()
56 && let Some(&name) = loc.path.first()
57 {
58 return self.records.iter().any(|(n, _)| *n == name);
59 }
60 false
61 }
62}
63
64impl PartialEq for Interface {
65 fn eq(&self, other: &Self) -> bool {
66 self.identifier == other.identifier
67 }
68}
69
70impl Eq for Interface {}
71
72impl fmt::Debug for Interface {
73 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74 write!(f, "{self}")
75 }
76}
77
78impl fmt::Display for Interface {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 writeln!(
81 f,
82 "interface {}{} {{",
83 self.identifier,
84 if self.parents.is_empty() {
85 String::new()
86 } else {
87 format!(" : {}", self.parents.iter().map(|(_, p)| p.to_string()).collect::<Vec<_>>().join(" + "))
88 }
89 )?;
90 for (_, fun_prot) in &self.functions {
91 writeln!(f, "{}", Indent(fun_prot))?;
92 }
93 for (_, rec_prot) in &self.records {
94 writeln!(f, "{}", Indent(rec_prot))?;
95 }
96 write!(f, "}}")
97 }
98}
99
100crate::simple_node_impl!(Interface);