marine_module_interface/interface/
module_interface.rs1use serde::Serialize;
18use serde::Deserialize;
19
20#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
21pub struct FunctionSignature {
22 pub name: String,
23 pub arguments: Vec<(String, String)>,
24 pub output_types: Vec<String>,
25}
26
27use std::cmp::Ordering;
28impl PartialOrd for FunctionSignature {
29 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
30 Some(self.cmp(other))
31 }
32}
33
34impl Ord for FunctionSignature {
35 fn cmp(&self, other: &Self) -> Ordering {
36 if self.name < other.name {
37 Ordering::Less
38 } else if self == other {
39 Ordering::Equal
40 } else {
41 Ordering::Greater
42 }
43 }
44}
45
46#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
47pub struct RecordField {
48 pub name: String,
49 pub ty: String,
50}
51
52#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
53pub struct RecordType {
54 pub name: String,
55 pub id: u64,
56 pub fields: Vec<RecordField>,
57}
58
59#[derive(PartialEq, Eq, Debug, Clone, Hash, Serialize, Deserialize)]
60pub struct ModuleInterface {
61 pub function_signatures: Vec<FunctionSignature>,
62 pub record_types: Vec<RecordType>,
64}
65
66use std::fmt;
67
68impl fmt::Display for FunctionSignature {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 use itertools::Itertools;
71
72 let (designator, output) = match self.output_types.len() {
73 0 => ("", ""),
74 1 => ("->", self.output_types[0].as_str()),
75 _ => unimplemented!("more than 1 output type is unsupported"),
76 };
77
78 let args = self
79 .arguments
80 .iter()
81 .map(|(name, ty)| format!("{}: {}", name, ty))
82 .format(", ");
83 writeln!(f, "{}({}) {} {}", self.name, args, designator, output)
84 }
85}
86
87impl fmt::Display for RecordType {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 writeln!(f, "data {}:", self.name)?;
90
91 for field in self.fields.iter() {
92 writeln!(f, " {}: {}", field.name, field.ty)?;
93 }
94
95 Ok(())
96 }
97}