Repository: bytecodealliance/wasm-tools
Pull request #1694 (2024-07-21 15:53:14)

Escape more keywords in wit-encoder


Patch:
diff --git a/crates/wit-encoder/src/enum_.rs b/crates/wit-encoder/src/enum_.rs
index 73f17eb3..6132ebb6 100644
--- a/crates/wit-encoder/src/enum_.rs
+++ b/crates/wit-encoder/src/enum_.rs
@@ -65,7 +65,19 @@ impl EnumCase {
         }
     }
 
-    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
+    pub fn name(&mut self) -> &Ident {
+        &self.name
+    }
+
+    pub fn set_name(&mut self, name: impl Into<Ident>) {
+        self.name = name.into();
+    }
+
+    pub fn docs(&mut self) -> &Option<Docs> {
+        &self.docs
+    }
+
+    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
         self.docs = docs.map(|d| d.into());
     }
 }
diff --git a/crates/wit-encoder/src/flags.rs b/crates/wit-encoder/src/flags.rs
index 69735cbb..2829b85d 100644
--- a/crates/wit-encoder/src/flags.rs
+++ b/crates/wit-encoder/src/flags.rs
@@ -14,6 +14,10 @@ impl Flags {
         }
     }
 
+    pub fn flag(&mut self, flag: impl Into<Flag>) {
+        self.flags.push(flag.into());
+    }
+
     pub fn flags(&self) -> &[Flag] {
         &self.flags
     }
@@ -39,7 +43,19 @@ impl Flag {
         }
     }
 
-    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
+    pub fn name(&mut self) -> &Ident {
+        &self.name
+    }
+
+    pub fn set_name(&mut self, name: impl Into<Ident>) {
+        self.name = name.into();
+    }
+
+    pub fn docs(&mut self) -> &Option<Docs> {
+        &self.docs
+    }
+
+    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
         self.docs = docs.map(|d| d.into());
     }
 }
@@ -60,7 +76,7 @@ where
 {
     fn into(self) -> Flag {
         let mut flag = Flag::new(self.0);
-        flag.docs(Some(self.1));
+        flag.set_docs(Some(self.1));
         flag
     }
 }
diff --git a/crates/wit-encoder/src/function.rs b/crates/wit-encoder/src/function.rs
index d00a8752..a86d153b 100644
--- a/crates/wit-encoder/src/function.rs
+++ b/crates/wit-encoder/src/function.rs
@@ -53,6 +53,10 @@ impl Params {
         self.items.push((name.into(), ty));
     }
 
+    pub fn item(&mut self, name: impl Into<Ident>, item: impl Into<Type>) {
+        self.items.push((name.into(), item.into()));
+    }
+
     pub fn items(&self) -> &Vec<(Ident, Type)> {
         &self.items
     }
@@ -164,6 +168,10 @@ impl StandaloneFunc {
         }
     }
 
+    pub fn set_name(&self) -> &Ident {
+        &self.name
+    }
+
     pub fn name(&self) -> &Ident {
         &self.name
     }
@@ -172,15 +180,23 @@ impl StandaloneFunc {
         &mut self.name
     }
 
-    pub fn params(&mut self, params: impl Into<Params>) {
+    pub fn set_params(&mut self, params: impl Into<Params>) {
         self.params = params.into();
     }
 
+    pub fn params(&self) -> &Params {
+        &self.params
+    }
+
     pub fn params_mut(&mut self) -> &mut Params {
         &mut self.params
     }
 
-    pub fn results(&mut self, results: impl Into<Results>) {
+    pub fn results(&self) -> &Results {
+        &self.results
+    }
+
+    pub fn set_results(&mut self, results: impl Into<Results>) {
         self.results = results.into();
     }
 
@@ -188,9 +204,13 @@ impl StandaloneFunc {
         &mut self.results
     }
 
-    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
+    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
         self.docs = docs.map(|d| d.into());
     }
+
+    pub fn docs(&self) -> &Option<Docs> {
+        &self.docs
+    }
 }
 
 #[cfg(test)]
diff --git a/crates/wit-encoder/src/ident.rs b/crates/wit-encoder/src/ident.rs
index dbf31bdf..c22848e8 100644
--- a/crates/wit-encoder/src/ident.rs
+++ b/crates/wit-encoder/src/ident.rs
@@ -45,7 +45,8 @@ fn is_keyword(name: &str) -> bool {
         "u8" | "u16" | "u32" | "u64" | "s8" | "s16" | "s32" | "s64" | "f32" | "f64" | "char"
         | "bool" | "string" | "tuple" | "list" | "option" | "result" | "use" | "type"
         | "resource" | "func" | "record" | "enum" | "flags" | "variant" | "static"
-        | "interface" | "world" | "import" | "export" | "package" | "own" | "borrow" => true,
+        | "interface" | "world" | "import" | "export" | "package" | "own" | "borrow" | "future"
+        | "stream" | "constructor" | "with" | "from" | "as" | "include" => true,
         _ => false,
     }
 }
diff --git a/crates/wit-encoder/src/interface.rs b/crates/wit-encoder/src/interface.rs
index a4b87611..1485bcad 100644
--- a/crates/wit-encoder/src/interface.rs
+++ b/crates/wit-encoder/src/interface.rs
@@ -41,6 +41,10 @@ impl Interface {
         self.items.push(InterfaceItem::Use(use_));
     }
 
+    pub fn item(&mut self, item: impl Into<InterfaceItem>) {
+        self.items.push(item.into());
+    }
+
     pub fn items(&self) -> &[InterfaceItem] {
         &self.items
     }
@@ -50,9 +54,13 @@ impl Interface {
     }
 
     /// Set the documentation of this interface.
-    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
+    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
         self.docs = docs.map(|d| d.into());
     }
+
+    pub fn docs(&self) -> &Option<Docs> {
+        &self.docs
+    }
 }
 
 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
diff --git a/crates/wit-encoder/src/package.rs b/crates/wit-encoder/src/package.rs
index 74fa9623..749fd650 100644
--- a/crates/wit-encoder/src/package.rs
+++ b/crates/wit-encoder/src/package.rs
@@ -39,6 +39,10 @@ impl Package {
         self.items.push(PackageItem::World(world))
     }
 
+    pub fn item(&mut self, item: impl Into<PackageItem>) {
+        self.items.push(item.into());
+    }
+
     pub fn items(&self) -> &[PackageItem] {
         &self.items
     }
diff --git a/crates/wit-encoder/src/record.rs b/crates/wit-encoder/src/record.rs
index 35c7f677..6b2dca0a 100644
--- a/crates/wit-encoder/src/record.rs
+++ b/crates/wit-encoder/src/record.rs
@@ -41,10 +41,22 @@ impl Field {
         }
     }
 
-    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
+    pub fn name(&mut self) -> &Ident {
+        &self.name
+    }
+
+    pub fn set_name(&mut self, name: impl Into<Ident>) {
+        self.name = name.into();
+    }
+
+    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
         self.docs = docs.map(|d| d.into());
     }
 
+    pub fn docs(&self) -> &Option<Docs> {
+        &self.docs
+    }
+
     pub fn ty(&self) -> &Type {
         &self.ty
     }
@@ -70,7 +82,7 @@ where
 {
     fn into(self) -> Field {
         let mut field = Field::new(self.0, self.1);
-        field.docs(Some(self.2.into()));
+        field.set_docs(Some(self.2.into()));
         field
     }
 }
diff --git a/crates/wit-encoder/src/resource.rs b/crates/wit-encoder/src/resource.rs
index 85d51714..e59e9417 100644
--- a/crates/wit-encoder/src/resource.rs
+++ b/crates/wit-encoder/src/resource.rs
@@ -68,7 +68,7 @@ impl ResourceFunc {
         }
     }
 
-    pub fn name(&mut self, name: impl Into<Ident>) {
+    pub fn set_name(&mut self, name: impl Into<Ident>) {
         match &self.kind {
             ResourceFuncKind::Method(_, results) => {
                 self.kind = ResourceFuncKind::Method(name.into(), results.clone())
@@ -80,15 +80,23 @@ impl ResourceFunc {
         }
     }
 
-    pub fn params(&mut self, params: impl Into<Params>) {
+    pub fn kind(&self) -> &ResourceFuncKind {
+        &self.kind
+    }
+
+    pub fn set_params(&mut self, params: impl Into<Params>) {
         self.params = params.into();
     }
 
+    pub fn params(&self) -> &Params {
+        &self.params
+    }
+
     pub fn params_mut(&mut self) -> &mut Params {
         &mut self.params
     }
 
-    pub fn results(&mut self, results: impl Into<Results>) {
+    pub fn set_results(&mut self, results: impl Into<Results>) {
         match &self.kind {
             ResourceFuncKind::Method(name, _) => {
                 self.kind = ResourceFuncKind::Method(name.clone(), results.into())
@@ -100,7 +108,11 @@ impl ResourceFunc {
         }
     }
 
-    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
+    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
         self.docs = docs.map(|d| d.into());
     }
+
+    pub fn docs(&self) -> &Option<Docs> {
+        &self.docs
+    }
 }
diff --git a/crates/wit-encoder/src/tuple.rs b/crates/wit-encoder/src/tuple.rs
index ee3c9a4c..1e43f16b 100644
--- a/crates/wit-encoder/src/tuple.rs
+++ b/crates/wit-encoder/src/tuple.rs
@@ -14,6 +14,10 @@ impl Tuple {
         Default::default()
     }
 
+    pub fn type_(&mut self, type_: impl Into<Type>) {
+        self.types.push(type_.into());
+    }
+
     pub fn types(&self) -> &[Type] {
         &self.types
     }
diff --git a/crates/wit-encoder/src/ty.rs b/crates/wit-encoder/src/ty.rs
index 95b72682..434868a5 100644
--- a/crates/wit-encoder/src/ty.rs
+++ b/crates/wit-encoder/src/ty.rs
@@ -129,6 +129,7 @@ impl VariantCase {
             docs: None,
         }
     }
+
     pub fn value(name: impl Into<Ident>, ty: Type) -> Self {
         Self {
             name: name.into(),
@@ -136,17 +137,22 @@ impl VariantCase {
             docs: None,
         }
     }
-    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
-        self.docs = docs.map(|d| d.into());
-    }
 
-    pub fn ty(&self) -> Option<&Type> {
+    pub fn type_(&self) -> Option<&Type> {
         self.ty.as_ref()
     }
 
-    pub fn ty_mut(&mut self) -> &mut Option<Type> {
+    pub fn type_mut(&mut self) -> &mut Option<Type> {
         &mut self.ty
     }
+
+    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
+        self.docs = docs.map(|d| d.into());
+    }
+
+    pub fn docs(&self) -> &Option<Docs> {
+        &self.docs
+    }
 }
 
 impl<N> Into<VariantCase> for (N,)
@@ -174,7 +180,7 @@ where
 {
     fn into(self) -> VariantCase {
         let mut field = VariantCase::value(self.0, self.1);
-        field.docs(Some(self.2.into()));
+        field.set_docs(Some(self.2.into()));
         field
     }
 }
@@ -273,7 +279,7 @@ impl TypeDef {
         &mut self.kind
     }
 
-    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
+    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
         self.docs = docs.map(|d| d.into());
     }
 }
diff --git a/crates/wit-encoder/src/world.rs b/crates/wit-encoder/src/world.rs
index d380f62b..320d890c 100644
--- a/crates/wit-encoder/src/world.rs
+++ b/crates/wit-encoder/src/world.rs
@@ -27,10 +27,14 @@ impl World {
     }
 
     /// Add a `name` to the world
-    pub fn name(&mut self, name: impl Into<Ident>) {
+    pub fn set_name(&mut self, name: impl Into<Ident>) {
         self.name = name.into();
     }
 
+    pub fn name(&mut self) -> &Ident {
+        &self.name
+    }
+
     /// Add an import or export to the world
     pub fn item(&mut self, item: WorldItem) {
         self.items.push(item);
@@ -62,7 +66,7 @@ impl World {
     }
 
     /// Set the documentation
-    pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
+    pub fn set_docs(&mut self, docs: Option<impl Into<Docs>>) {
         self.docs = docs.map(|d| d.into());
     }
 }


Test patch:
diff --git a/crates/wit-encoder/tests/functions.rs b/crates/wit-encoder/tests/functions.rs
index b65a7b45..53cf4751 100644
--- a/crates/wit-encoder/tests/functions.rs
+++ b/crates/wit-encoder/tests/functions.rs
@@ -31,54 +31,54 @@ fn concrete_types() {
         interface.function(StandaloneFunc::new("f1"));
         interface.function({
             let mut func = StandaloneFunc::new("f2");
-            func.params(Params::from_iter([("a", Type::U32)]));
+            func.set_params(Params::from_iter([("a", Type::U32)]));
             func
         });
         interface.function({
             let mut func = StandaloneFunc::new("f4");
-            func.results(Results::anon(Type::U32));
+            func.set_results(Results::anon(Type::U32));
             func
         });
         interface.function({
             let mut func = StandaloneFunc::new("f6");
-            func.results(Results::anon(Type::tuple(vec![Type::U32, Type::U32])));
+            func.set_results(Results::anon(Type::tuple(vec![Type::U32, Type::U32])));
             func
         });
         interface.function({
             let mut func = StandaloneFunc::new("f7");
-            func.params(Params::from_iter([("a", Type::F32), ("b", Type::F32)]));
-            func.results(Type::tuple(vec![Type::U32, Type::U32]));
+            func.set_params(Params::from_iter([("a", Type::F32), ("b", Type::F32)]));
+            func.set_results(Type::tuple(vec![Type::U32, Type::U32]));
             func
         });
         interface.function({
             let mut func = StandaloneFunc::new("f8");
-            func.params(Params::from_iter([("a", Type::option(Type::U32))]));
-            func.results(Type::result(Result_::both(Type::U32, Type::F32)));
+            func.set_params(Params::from_iter([("a", Type::option(Type::U32))]));
+            func.set_results(Type::result(Result_::both(Type::U32, Type::F32)));
             func
         });
         interface.function({
             let mut func = StandaloneFunc::new("f9");
-            func.results(Results::named(vec![("u", Type::U32), ("f", Type::F32)]));
+            func.set_results(Results::named(vec![("u", Type::U32), ("f", Type::F32)]));
             func
         });
         interface.function({
             let mut func = StandaloneFunc::new("f10");
-            func.results(Results::named(vec![("u", Type::U32)]));
+            func.set_results(Results::named(vec![("u", Type::U32)]));
             func
         });
         interface.function({
             let mut func = StandaloneFunc::new("f11");
-            func.results(Type::result(Result_::ok(Type::F32)));
+            func.set_results(Type::result(Result_::ok(Type::F32)));
             func
         });
         interface.function({
             let mut func = StandaloneFunc::new("f12");
-            func.results(Type::result(Result_::err(Type::F32)));
+            func.set_results(Type::result(Result_::err(Type::F32)));
             func
         });
         interface.function({
             let mut func = StandaloneFunc::new("f13");
-            func.results(Type::result(Result_::empty()));
+            func.set_results(Type::result(Result_::empty()));
             func
         });
         interface
diff --git a/crates/wit-encoder/tests/type_defs.rs b/crates/wit-encoder/tests/type_defs.rs
index 2f96a9a6..5e0f2820 100644
--- a/crates/wit-encoder/tests/type_defs.rs
+++ b/crates/wit-encoder/tests/type_defs.rs
@@ -125,7 +125,7 @@ fn types() {
     let mut package = wit_encoder::Package::new(name);
     package.interface({
         let mut interface = wit_encoder::Interface::new("type-defs");
-        interface.docs(Some("interface documentation"));
+        interface.set_docs(Some("interface documentation"));
         interface.type_def(TypeDef::type_("t1", Type::U8));
         interface.type_def(TypeDef::type_("t2", Type::U16));
         interface.type_def(TypeDef::type_("t3", Type::U32));
@@ -150,7 +150,7 @@ fn types() {
         interface.type_def(TypeDef::type_("t17", Type::result(Result_::ok(Type::U32))));
         interface.type_def({
             let mut type_ = TypeDef::type_("t18", Type::result(Result_::empty()));
-            type_.docs(Some("this is a documentation comment"));
+            type_.set_docs(Some("this is a documentation comment"));
             type_
         });
 
@@ -220,25 +220,25 @@ fn types() {
             [
                 {
                     let mut func = ResourceFunc::constructor();
-                    func.params(Params::from_iter([("a", Type::U32)]));
-                    func.docs(Some("create a new t51"));
+                    func.set_params(Params::from_iter([("a", Type::U32)]));
+                    func.set_docs(Some("create a new t51"));
                     func
                 },
                 {
                     let mut func = ResourceFunc::method("set-a");
-                    func.params(Params::from_iter([("a", Type::U32)]));
-                    func.docs(Some("set a"));
+                    func.set_params(Params::from_iter([("a", Type::U32)]));
+                    func.set_docs(Some("set a"));
                     func
                 },
                 {
                     let mut func = ResourceFunc::method("get-a");
-                    func.results(Results::anon(Type::U32));
-                    func.docs(Some("get a"));
+                    func.set_results(Results::anon(Type::U32));
+                    func.set_docs(Some("get a"));
                     func
                 },
                 {
                     let mut func = ResourceFunc::static_("b");
-                    func.docs(Some("do b"));
+                    func.set_docs(Some("do b"));
                     func
                 },
             ],
diff --git a/crates/wit-encoder/tests/world.rs b/crates/wit-encoder/tests/world.rs
index 83777cc0..ca629059 100644
--- a/crates/wit-encoder/tests/world.rs
+++ b/crates/wit-encoder/tests/world.rs
@@ -30,24 +30,24 @@ fn worlds() {
         let mut world = wit_encoder::World::new("world");
         world.inline_interface_export({
             let mut interface = Interface::new("example");
-            interface.docs(Some("inline interface"));
+            interface.set_docs(Some("inline interface"));
             interface.function({
                 let mut func = StandaloneFunc::new("do-nothing");
-                func.docs(Some("func docs"));
+                func.set_docs(Some("func docs"));
                 func
             });
             interface
         });
         world.function_export({
             let mut func = StandaloneFunc::new("scan");
-            func.results(Type::list(Type::U8));
-            func.docs(Some("scan stuff"));
+            func.set_results(Type::list(Type::U8));
+            func.set_docs(Some("scan stuff"));
             func
         });
         world.named_interface_import("error-reporter");
         world.function_import({
             let mut func: StandaloneFunc = StandaloneFunc::new("print");
-            func.params(("s", Type::String));
+            func.set_params(("s", Type::String));
             func
         });
         world