Skip to content

Commit 3e58f7d

Browse files
author
Guy Bedford
authored
fix: f32, f64 encoding for wit-encoder (#1660)
* fix: f32, f64 encoding for wit-encoder * fixup tests * handle escaping at write not create
1 parent a265ec1 commit 3e58f7d

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

crates/wit-encoder/src/ident.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ pub struct Ident(Cow<'static, str>);
55

66
impl Ident {
77
pub fn new(s: impl Into<Cow<'static, str>>) -> Self {
8-
let s: Cow<'static, str> = s.into();
9-
if is_keyword(&s) {
10-
Self(Cow::Owned(format!("%{}", s)))
11-
} else {
12-
Self(s)
13-
}
8+
Self(s.into())
149
}
1510
}
1611

@@ -25,6 +20,9 @@ where
2520

2621
impl fmt::Display for Ident {
2722
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23+
if is_keyword(&self.0) {
24+
write!(f, "%")?;
25+
}
2826
self.0.fmt(f)
2927
}
3028
}
@@ -37,8 +35,8 @@ impl AsRef<str> for Ident {
3735

3836
fn is_keyword(name: &str) -> bool {
3937
match name {
40-
"u8" | "u16" | "u32" | "u64" | "s8" | "s16" | "s32" | "s64" | "float32" | "float64"
41-
| "char" | "bool" | "string" | "tuple" | "list" | "option" | "result" | "use" | "type"
38+
"u8" | "u16" | "u32" | "u64" | "s8" | "s16" | "s32" | "s64" | "f32" | "f64" | "char"
39+
| "bool" | "string" | "tuple" | "list" | "option" | "result" | "use" | "type"
4240
| "resource" | "func" | "record" | "enum" | "flags" | "variant" | "static"
4341
| "interface" | "world" | "import" | "export" | "package" | "own" | "borrow" => true,
4442
_ => false,

crates/wit-encoder/tests/type_defs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const PACKAGE: &str = indoc::indoc! {"
103103
type t46 = t44;
104104
type foo = bar;
105105
type bar = u32;
106+
type %f64 = f64;
106107
resource t50 {
107108
}
108109
resource t51 {
@@ -211,6 +212,8 @@ fn types() {
211212
interface.type_def(TypeDef::type_("foo", Type::named("bar")));
212213
interface.type_def(TypeDef::type_("bar", Type::U32));
213214

215+
interface.type_def(TypeDef::type_("f64", Type::F64));
216+
214217
interface.type_def(TypeDef::resource("t50", Vec::<ResourceFunc>::new()));
215218
interface.type_def(TypeDef::resource(
216219
"t51",

0 commit comments

Comments
 (0)