Skip to content

Commit b92dd79

Browse files
authored
A few smaller improvements to wit-encoder (#1648)
1 parent 6fc1601 commit b92dd79

File tree

10 files changed

+80
-6
lines changed

10 files changed

+80
-6
lines changed

crates/wit-encoder/src/enum_.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ where
1818
}
1919

2020
impl Enum {
21+
pub fn empty() -> Self {
22+
Self::default()
23+
}
24+
25+
pub fn case(&mut self, case: impl Into<EnumCase>) {
26+
self.cases.push(case.into());
27+
}
28+
2129
pub fn cases(&self) -> &[EnumCase] {
2230
&self.cases
2331
}

crates/wit-encoder/src/function.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ impl Params {
4747
Self::default()
4848
}
4949

50+
pub fn push(&mut self, name: impl Into<Ident>, ty: Type) {
51+
self.items.push((name.into(), ty));
52+
}
53+
5054
pub fn items(&self) -> &Vec<(Ident, Type)> {
5155
&self.items
5256
}
@@ -115,7 +119,7 @@ impl Results {
115119
Results::Anon(type_)
116120
}
117121

118-
pub fn named(types: impl IntoIterator<Item = (impl Into<String>, Type)>) -> Results {
122+
pub fn named(types: impl IntoIterator<Item = (impl Into<Ident>, Type)>) -> Results {
119123
Results::Named(
120124
types
121125
.into_iter()
@@ -154,14 +158,30 @@ impl StandaloneFunc {
154158
}
155159
}
156160

161+
pub fn name(&self) -> &Ident {
162+
&self.name
163+
}
164+
165+
pub fn name_mut(&mut self) -> &mut Ident {
166+
&mut self.name
167+
}
168+
157169
pub fn params(&mut self, params: impl Into<Params>) {
158170
self.params = params.into();
159171
}
160172

173+
pub fn params_mut(&mut self) -> &mut Params {
174+
&mut self.params
175+
}
176+
161177
pub fn results(&mut self, results: impl Into<Results>) {
162178
self.results = results.into();
163179
}
164180

181+
pub fn results_mut(&mut self) -> &mut Results {
182+
&mut self.results
183+
}
184+
165185
pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
166186
self.docs = docs.map(|d| d.into());
167187
}

crates/wit-encoder/src/ident.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@ impl fmt::Display for Ident {
2929
}
3030
}
3131

32+
impl AsRef<str> for Ident {
33+
fn as_ref(&self) -> &str {
34+
self.0.as_ref()
35+
}
36+
}
37+
3238
fn is_keyword(name: &str) -> bool {
3339
match name {
3440
"u8" | "u16" | "u32" | "u64" | "s8" | "s16" | "s32" | "s64" | "float32" | "float64"
3541
| "char" | "bool" | "string" | "tuple" | "list" | "option" | "result" | "use" | "type"
3642
| "resource" | "func" | "record" | "enum" | "flags" | "variant" | "static"
37-
| "interface" | "world" | "import" | "export" | "package" => true,
43+
| "interface" | "world" | "import" | "export" | "package" | "own" | "borrow" => true,
3844
_ => false,
3945
}
4046
}

crates/wit-encoder/src/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Interface {
3838
&self.items
3939
}
4040

41-
pub fn functions_mut(&mut self) -> &mut Vec<InterfaceItem> {
41+
pub fn items_mut(&mut self) -> &mut Vec<InterfaceItem> {
4242
&mut self.items
4343
}
4444

crates/wit-encoder/src/package.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ impl Package {
3636
pub fn world(&mut self, world: World) {
3737
self.items.push(PackageItem::World(world))
3838
}
39+
40+
pub fn items(&self) -> &[PackageItem] {
41+
&self.items
42+
}
43+
44+
pub fn items_mut(&mut self) -> &mut Vec<PackageItem> {
45+
&mut self.items
46+
}
3947
}
4048

4149
impl Render for Package {

crates/wit-encoder/src/record.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ impl Field {
4040
pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
4141
self.docs = docs.map(|d| d.into());
4242
}
43+
44+
pub fn ty(&self) -> &Type {
45+
&self.ty
46+
}
47+
48+
pub fn ty_mut(&mut self) -> &mut Type {
49+
&mut self.ty
50+
}
4351
}
4452

4553
impl<N> Into<Field> for (N, Type)

crates/wit-encoder/src/resource.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{ident::Ident, Docs, Params, Results};
22

33
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
44
pub struct Resource {
5-
pub funcs: Vec<ResourceFunc>,
5+
pub(crate) funcs: Vec<ResourceFunc>,
66
}
77

88
impl Resource {
@@ -78,6 +78,10 @@ impl ResourceFunc {
7878
self.params = params.into();
7979
}
8080

81+
pub fn params_mut(&mut self) -> &mut Params {
82+
&mut self.params
83+
}
84+
8185
pub fn results(&mut self, results: impl Into<Results>) {
8286
match &self.kind {
8387
ResourceFuncKind::Method(name, _) => {

crates/wit-encoder/src/tuple.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ use std::fmt::Display;
22

33
use crate::Type;
44

5-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
66
pub struct Tuple {
77
pub(crate) types: Vec<Type>,
88
}
99

1010
impl Tuple {
11+
pub fn empty() -> Self {
12+
Default::default()
13+
}
14+
1115
pub fn types(&self) -> &[Type] {
1216
&self.types
1317
}

crates/wit-encoder/src/ty.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ impl VariantCase {
135135
pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
136136
self.docs = docs.map(|d| d.into());
137137
}
138+
139+
pub fn ty(&self) -> Option<&Type> {
140+
self.ty.as_ref()
141+
}
142+
143+
pub fn ty_mut(&mut self) -> &mut Option<Type> {
144+
&mut self.ty
145+
}
138146
}
139147

140148
impl<N> Into<VariantCase> for (N,)

crates/wit-encoder/src/variant.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
use crate::VariantCase;
22

3-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
44
pub struct Variant {
55
pub(crate) cases: Vec<VariantCase>,
66
}
77

88
impl Variant {
9+
pub fn empty() -> Self {
10+
Self::default()
11+
}
12+
13+
pub fn case(&mut self, case: impl Into<VariantCase>) {
14+
self.cases.push(case.into());
15+
}
16+
917
pub fn cases(&self) -> &[VariantCase] {
1018
&self.cases
1119
}

0 commit comments

Comments
 (0)