Skip to content

Commit

Permalink
A few smaller improvements to wit-encoder (bytecodealliance#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
MendyBerger authored Jul 8, 2024
1 parent 6fc1601 commit b92dd79
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 6 deletions.
8 changes: 8 additions & 0 deletions crates/wit-encoder/src/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ where
}

impl Enum {
pub fn empty() -> Self {
Self::default()
}

pub fn case(&mut self, case: impl Into<EnumCase>) {
self.cases.push(case.into());
}

pub fn cases(&self) -> &[EnumCase] {
&self.cases
}
Expand Down
22 changes: 21 additions & 1 deletion crates/wit-encoder/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ impl Params {
Self::default()
}

pub fn push(&mut self, name: impl Into<Ident>, ty: Type) {
self.items.push((name.into(), ty));
}

pub fn items(&self) -> &Vec<(Ident, Type)> {
&self.items
}
Expand Down Expand Up @@ -115,7 +119,7 @@ impl Results {
Results::Anon(type_)
}

pub fn named(types: impl IntoIterator<Item = (impl Into<String>, Type)>) -> Results {
pub fn named(types: impl IntoIterator<Item = (impl Into<Ident>, Type)>) -> Results {
Results::Named(
types
.into_iter()
Expand Down Expand Up @@ -154,14 +158,30 @@ impl StandaloneFunc {
}
}

pub fn name(&self) -> &Ident {
&self.name
}

pub fn name_mut(&mut self) -> &mut Ident {
&mut self.name
}

pub fn params(&mut self, params: impl Into<Params>) {
self.params = params.into();
}

pub fn params_mut(&mut self) -> &mut Params {
&mut self.params
}

pub fn results(&mut self, results: impl Into<Results>) {
self.results = results.into();
}

pub fn results_mut(&mut self) -> &mut Results {
&mut self.results
}

pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
self.docs = docs.map(|d| d.into());
}
Expand Down
8 changes: 7 additions & 1 deletion crates/wit-encoder/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ impl fmt::Display for Ident {
}
}

impl AsRef<str> for Ident {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}

fn is_keyword(name: &str) -> bool {
match name {
"u8" | "u16" | "u32" | "u64" | "s8" | "s16" | "s32" | "s64" | "float32" | "float64"
| "char" | "bool" | "string" | "tuple" | "list" | "option" | "result" | "use" | "type"
| "resource" | "func" | "record" | "enum" | "flags" | "variant" | "static"
| "interface" | "world" | "import" | "export" | "package" => true,
| "interface" | "world" | "import" | "export" | "package" | "own" | "borrow" => true,
_ => false,
}
}
2 changes: 1 addition & 1 deletion crates/wit-encoder/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Interface {
&self.items
}

pub fn functions_mut(&mut self) -> &mut Vec<InterfaceItem> {
pub fn items_mut(&mut self) -> &mut Vec<InterfaceItem> {
&mut self.items
}

Expand Down
8 changes: 8 additions & 0 deletions crates/wit-encoder/src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ impl Package {
pub fn world(&mut self, world: World) {
self.items.push(PackageItem::World(world))
}

pub fn items(&self) -> &[PackageItem] {
&self.items
}

pub fn items_mut(&mut self) -> &mut Vec<PackageItem> {
&mut self.items
}
}

impl Render for Package {
Expand Down
8 changes: 8 additions & 0 deletions crates/wit-encoder/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ impl Field {
pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
self.docs = docs.map(|d| d.into());
}

pub fn ty(&self) -> &Type {
&self.ty
}

pub fn ty_mut(&mut self) -> &mut Type {
&mut self.ty
}
}

impl<N> Into<Field> for (N, Type)
Expand Down
6 changes: 5 additions & 1 deletion crates/wit-encoder/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{ident::Ident, Docs, Params, Results};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Resource {
pub funcs: Vec<ResourceFunc>,
pub(crate) funcs: Vec<ResourceFunc>,
}

impl Resource {
Expand Down Expand Up @@ -78,6 +78,10 @@ impl ResourceFunc {
self.params = params.into();
}

pub fn params_mut(&mut self) -> &mut Params {
&mut self.params
}

pub fn results(&mut self, results: impl Into<Results>) {
match &self.kind {
ResourceFuncKind::Method(name, _) => {
Expand Down
6 changes: 5 additions & 1 deletion crates/wit-encoder/src/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ use std::fmt::Display;

use crate::Type;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Tuple {
pub(crate) types: Vec<Type>,
}

impl Tuple {
pub fn empty() -> Self {
Default::default()
}

pub fn types(&self) -> &[Type] {
&self.types
}
Expand Down
8 changes: 8 additions & 0 deletions crates/wit-encoder/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ impl VariantCase {
pub fn docs(&mut self, docs: Option<impl Into<Docs>>) {
self.docs = docs.map(|d| d.into());
}

pub fn ty(&self) -> Option<&Type> {
self.ty.as_ref()
}

pub fn ty_mut(&mut self) -> &mut Option<Type> {
&mut self.ty
}
}

impl<N> Into<VariantCase> for (N,)
Expand Down
10 changes: 9 additions & 1 deletion crates/wit-encoder/src/variant.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use crate::VariantCase;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Variant {
pub(crate) cases: Vec<VariantCase>,
}

impl Variant {
pub fn empty() -> Self {
Self::default()
}

pub fn case(&mut self, case: impl Into<VariantCase>) {
self.cases.push(case.into());
}

pub fn cases(&self) -> &[VariantCase] {
&self.cases
}
Expand Down

0 comments on commit b92dd79

Please sign in to comment.