linera_witty/wit_generation/
mod.rs1mod stub_instance;
7
8use std::collections::BTreeMap;
9
10use genawaiter::{rc::gen, yield_};
11
12pub use self::stub_instance::StubInstance;
13pub use crate::type_traits::RegisterWitTypes;
14
15pub trait WitInterface {
17 type Dependencies: RegisterWitTypes;
19
20 fn wit_package() -> &'static str;
22
23 fn wit_name() -> &'static str;
25
26 fn wit_functions() -> Vec<String>;
28}
29
30#[derive(Clone, Debug)]
32pub struct WitInterfaceWriter {
33 package: &'static str,
34 name: &'static str,
35 types: BTreeMap<String, String>,
36 functions: Vec<String>,
37}
38
39impl WitInterfaceWriter {
40 pub fn new<Interface>() -> Self
42 where
43 Interface: WitInterface,
44 {
45 let mut types = BTreeMap::new();
46
47 Interface::Dependencies::register_wit_types(&mut types);
48
49 WitInterfaceWriter {
50 package: Interface::wit_package(),
51 name: Interface::wit_name(),
52 types,
53 functions: Interface::wit_functions(),
54 }
55 }
56
57 pub fn generate_file_contents(&self) -> impl Iterator<Item = &str> {
59 gen!({
60 yield_!("package ");
61 yield_!(self.package);
62 yield_!(";\n\n");
63
64 yield_!("interface ");
65 yield_!(self.name);
66 yield_!(" {\n");
67
68 for function in &self.functions {
69 yield_!(&function);
70 yield_!("\n");
71 }
72
73 for type_declaration in self.types.values() {
74 if !type_declaration.is_empty() {
75 yield_!("\n");
76 yield_!(&type_declaration);
77 }
78 }
79
80 yield_!("}\n");
81 })
82 .into_iter()
83 }
84}
85
86#[derive(Clone, Debug)]
89pub struct WitWorldWriter {
90 package: Option<&'static str>,
91 name: String,
92 imports: Vec<&'static str>,
93 exports: Vec<&'static str>,
94}
95
96impl WitWorldWriter {
97 pub fn new(package: impl Into<Option<&'static str>>, name: impl Into<String>) -> Self {
99 WitWorldWriter {
100 package: package.into(),
101 name: name.into(),
102 imports: Vec::new(),
103 exports: Vec::new(),
104 }
105 }
106
107 pub fn import<Interface>(mut self) -> Self
109 where
110 Interface: WitInterface,
111 {
112 self.imports.push(Interface::wit_name());
113 self
114 }
115
116 pub fn export<Interface>(mut self) -> Self
118 where
119 Interface: WitInterface,
120 {
121 self.exports.push(Interface::wit_name());
122 self
123 }
124
125 pub fn generate_file_contents(&self) -> impl Iterator<Item = &str> {
128 gen!({
129 if let Some(package) = &self.package {
130 yield_!("package ");
131 yield_!(package);
132 yield_!(";\n\n");
133 }
134
135 yield_!("world ");
136 yield_!(&self.name);
137 yield_!(" {\n");
138
139 for import in &self.imports {
140 yield_!(" import ");
141 yield_!(import);
142 yield_!(";\n");
143 }
144
145 if !self.imports.is_empty() {
146 yield_!("\n");
147 }
148
149 for export in &self.exports {
150 yield_!(" export ");
151 yield_!(export);
152 yield_!(";\n");
153 }
154
155 yield_!("}\n");
156 })
157 .into_iter()
158 }
159}