add a function returning void

This commit is contained in:
Brian Hicks 2024-04-10 06:08:45 -05:00
parent 6337bfb47c
commit 514716e9b3
No known key found for this signature in database
GPG Key ID: C4F324B9CAAB0D50

View File

@ -7,6 +7,8 @@ pub enum TSType {
Scalar(&'static str),
StringScalar(String),
Union(Vec<TSType>),
/// We never need return values in interop, so everything can be void!
FunctionReturningVoid(BTreeMap<String, TSType>),
}
impl TSType {
@ -59,10 +61,26 @@ impl TSType {
out.push_str(&type_.to_source())
}
}
Self::FunctionReturningVoid(args) => {
out.push('(');
for (i, (name, type_)) in args.iter().enumerate() {
if i != 0 {
out.push_str(", ");
}
out.push_str(name);
out.push_str(": ");
out.push_str(&type_.to_source());
}
out.push_str("): void");
}
}
out
}
fn new_function(args: BTreeMap<String, TSType>) -> Self {
Self::FunctionReturningVoid(args)
}
}
#[cfg(test)]
@ -114,4 +132,17 @@ mod tests {
assert_eq!(type_.to_source(), "\"a\" | \"b\"".to_string())
}
#[test]
fn new_function() {
let type_ = TSType::new_function(BTreeMap::from([
("one".to_string(), TSType::Scalar("number")),
("two".to_string(), TSType::Scalar("string")),
]));
assert_eq!(
type_.to_source(),
"(one: number, two: string): void".to_string()
)
}
}