Skip to content

Commit

Permalink
fix: f32, f64 encoding for wit-encoder (#1660)
Browse files Browse the repository at this point in the history
* fix: f32, f64 encoding for wit-encoder

* fixup tests

* handle escaping at write not create
  • Loading branch information
guybedford authored Jul 11, 2024
1 parent a265ec1 commit 3e58f7d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
14 changes: 6 additions & 8 deletions crates/wit-encoder/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ pub struct Ident(Cow<'static, str>);

impl Ident {
pub fn new(s: impl Into<Cow<'static, str>>) -> Self {
let s: Cow<'static, str> = s.into();
if is_keyword(&s) {
Self(Cow::Owned(format!("%{}", s)))
} else {
Self(s)
}
Self(s.into())
}
}

Expand All @@ -25,6 +20,9 @@ where

impl fmt::Display for Ident {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if is_keyword(&self.0) {
write!(f, "%")?;
}
self.0.fmt(f)
}
}
Expand All @@ -37,8 +35,8 @@ impl AsRef<str> for Ident {

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"
"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,
_ => false,
Expand Down
3 changes: 3 additions & 0 deletions crates/wit-encoder/tests/type_defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const PACKAGE: &str = indoc::indoc! {"
type t46 = t44;
type foo = bar;
type bar = u32;
type %f64 = f64;
resource t50 {
}
resource t51 {
Expand Down Expand Up @@ -211,6 +212,8 @@ fn types() {
interface.type_def(TypeDef::type_("foo", Type::named("bar")));
interface.type_def(TypeDef::type_("bar", Type::U32));

interface.type_def(TypeDef::type_("f64", Type::F64));

interface.type_def(TypeDef::resource("t50", Vec::<ResourceFunc>::new()));
interface.type_def(TypeDef::resource(
"t51",
Expand Down

0 comments on commit 3e58f7d

Please sign in to comment.