Skip to content

Commit 9a482eb

Browse files
Support option and result types
1 parent 4b8de6a commit 9a482eb

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/codegen/interface.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,37 @@ impl Interface {
124124
125125
import scala.scalajs.js
126126
127+
sealed trait Optional[+A] extends js.Object {{
128+
val tag: String
129+
val `val`: js.UndefOr[js.Object] = js.undefined
130+
}}
131+
object Optional {{
132+
def some[A](value: A): Optional[A] = new Optional[A] {{
133+
val tag: String = \"some\"
134+
override val `val`: js.UndefOr[js.Object] = js.Object(value)
135+
}}
136+
137+
def none: Optional[Nothing] = new Optional[Nothing] {{
138+
val tag: String = \"none\"
139+
}}
140+
}}
141+
142+
sealed trait Result[+Ok, +Err] extends js.Object {{
143+
val tag: String
144+
val `val`: js.UndefOr[js.Object] = js.undefined
145+
}}
146+
object Result {{
147+
def ok[Ok](value: Ok): Result[Ok, Nothing] = new Result[Ok, Nothing] {{
148+
val tag: String = \"ok\"
149+
override val `val`: js.UndefOr[js.Object] = js.Object(value)
150+
}}
151+
152+
def err[Err](value: Err): Result[Nothing, Err] = new Result[Nothing, Err] {{
153+
val tag: String = \"err\"
154+
override val `val`: js.UndefOr[js.Object] = js.Object(value)
155+
}}
156+
}}
157+
127158
{records}
128159
129160
{variants}

src/types/type_map.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::HashMap;
22

33
use id_arena::Id;
4-
use wit_parser::{TypeDef, TypeDefKind, UnresolvedPackage};
4+
use wit_parser::{Result_, TypeDef, TypeDefKind, UnresolvedPackage};
55

66
use super::{ConcreteName, Constructor, TypeName};
77

@@ -34,12 +34,31 @@ impl From<&UnresolvedPackage> for TypeMap {
3434
TypeDefKind::List(ty) => Some((
3535
id,
3636
TypeName::Constructor(Constructor::new(
37-
"List",
37+
"js.Array",
3838
vec![ty],
3939
&TypeMap(hash_map1.clone()),
4040
)),
4141
)),
42-
_ => todo!("Support other kinds of types"),
42+
TypeDefKind::Option(ty) => Some((
43+
id,
44+
TypeName::Constructor(Constructor::new(
45+
"Optional",
46+
vec![ty],
47+
&TypeMap(hash_map1.clone()),
48+
)),
49+
)),
50+
TypeDefKind::Result(Result_ {
51+
ok: Some(ok),
52+
err: Some(err),
53+
}) => Some((
54+
id,
55+
TypeName::Constructor(Constructor::new(
56+
"Result",
57+
vec![ok, err],
58+
&TypeMap(hash_map1.clone()),
59+
)),
60+
)),
61+
_ => todo!("Support other kinds of constructors"),
4362
}
4463
} else {
4564
None

src/types/type_name.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct Constructor {
4444
impl Constructor {
4545
pub fn new(name: &str, params: Vec<WitType>, type_map: &TypeMap) -> Self {
4646
Self {
47-
name: name.to_case(Case::UpperCamel),
47+
name: name.to_owned(),
4848
params: params
4949
.into_iter()
5050
.map(|param| Type::from_wit(param, type_map).to_string())

0 commit comments

Comments
 (0)