Skip to content

Commit 06f8102

Browse files
chore: clean up code
Signed-off-by: Henry Gressmann <mail@henrygressmann.de>
1 parent d33a0c6 commit 06f8102

37 files changed

+365
-366
lines changed

Cargo.lock

Lines changed: 33 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cli/src/args.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use tinywasm::types::WasmValue;
55
pub struct WasmArg(WasmValue);
66

77
pub fn to_wasm_args(args: Vec<WasmArg>) -> Vec<WasmValue> {
8-
args.into_iter().map(|a| a.into()).collect()
8+
args.into_iter().map(Into::into).collect()
99
}
1010

1111
impl From<WasmArg> for WasmValue {
@@ -18,14 +18,14 @@ impl FromStr for WasmArg {
1818
type Err = String;
1919
fn from_str(s: &str) -> std::prelude::v1::Result<Self, Self::Err> {
2020
let [ty, val]: [&str; 2] =
21-
s.split(':').collect::<Vec<_>>().try_into().map_err(|e| format!("invalid arguments: {:?}", e))?;
21+
s.split(':').collect::<Vec<_>>().try_into().map_err(|e| format!("invalid arguments: {e:?}"))?;
2222

2323
let arg: WasmValue = match ty {
2424
"i32" => val.parse::<i32>().map_err(|e| format!("invalid argument value for i32: {e:?}"))?.into(),
2525
"i64" => val.parse::<i64>().map_err(|e| format!("invalid argument value for i64: {e:?}"))?.into(),
2626
"f32" => val.parse::<f32>().map_err(|e| format!("invalid argument value for f32: {e:?}"))?.into(),
2727
"f64" => val.parse::<f64>().map_err(|e| format!("invalid argument value for f64: {e:?}"))?.into(),
28-
t => return Err(format!("Invalid arg type: {}", t)),
28+
t => return Err(format!("Invalid arg type: {t}")),
2929
};
3030

3131
Ok(WasmArg(arg))

crates/cli/src/bin.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod util;
1414
mod wat;
1515

1616
#[derive(FromArgs)]
17-
/// TinyWasm CLI
17+
/// `TinyWasm` CLI
1818
struct TinyWasmCli {
1919
#[argh(subcommand)]
2020
nested: TinyWasmSubcommand,
@@ -40,7 +40,7 @@ impl FromStr for Engine {
4040
fn from_str(s: &str) -> Result<Self, Self::Err> {
4141
match s {
4242
"main" => Ok(Self::Main),
43-
_ => Err(format!("unknown engine: {}", s)),
43+
_ => Err(format!("unknown engine: {s}")),
4444
}
4545
}
4646
}
@@ -98,19 +98,19 @@ fn main() -> Result<()> {
9898
};
9999

100100
match engine {
101-
Engine::Main => run(module, func, to_wasm_args(args)),
101+
Engine::Main => run(module, func, &to_wasm_args(args)),
102102
}
103103
}
104104
}
105105
}
106106

107-
fn run(module: Module, func: Option<String>, args: Vec<WasmValue>) -> Result<()> {
107+
fn run(module: Module, func: Option<String>, args: &[WasmValue]) -> Result<()> {
108108
let mut store = tinywasm::Store::default();
109109
let instance = module.instantiate(&mut store, None)?;
110110

111111
if let Some(func) = func {
112112
let func = instance.exported_func_untyped(&store, &func)?;
113-
let res = func.call(&mut store, &args)?;
113+
let res = func.call(&mut store, args)?;
114114
info!("{res:?}");
115115
}
116116

crates/parser/src/conversion.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(crate) fn convert_module_element(element: wasmparser::Element<'_>) -> Result
3838
.collect::<Result<Vec<_>>>()?
3939
.into_boxed_slice();
4040

41-
Ok(tinywasm_types::Element { kind, items, ty: convert_reftype(&ty), range: element.range })
41+
Ok(tinywasm_types::Element { kind, items, ty: convert_reftype(ty), range: element.range })
4242
}
4343
}
4444
}
@@ -76,23 +76,23 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
7676
kind: match import.ty {
7777
wasmparser::TypeRef::Func(ty) => ImportKind::Function(ty),
7878
wasmparser::TypeRef::Table(ty) => ImportKind::Table(TableType {
79-
element_type: convert_reftype(&ty.element_type),
79+
element_type: convert_reftype(ty.element_type),
8080
size_initial: ty.initial.try_into().map_err(|_| {
8181
crate::ParseError::UnsupportedOperator(format!("Table size initial is too large: {}", ty.initial))
8282
})?,
8383
size_max: match ty.maximum {
8484
Some(max) => Some(max.try_into().map_err(|_| {
85-
crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {}", max))
85+
crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {max}"))
8686
})?),
8787
None => None,
8888
},
8989
}),
90-
wasmparser::TypeRef::Memory(ty) => ImportKind::Memory(convert_module_memory(ty)?),
90+
wasmparser::TypeRef::Memory(ty) => ImportKind::Memory(convert_module_memory(ty)),
9191
wasmparser::TypeRef::Global(ty) => {
9292
ImportKind::Global(GlobalType { mutable: ty.mutable, ty: convert_valtype(&ty.content_type) })
9393
}
9494
wasmparser::TypeRef::Tag(ty) => {
95-
return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported import kind: {:?}", ty)))
95+
return Err(crate::ParseError::UnsupportedOperator(format!("Unsupported import kind: {ty:?}")))
9696
}
9797
},
9898
})
@@ -101,18 +101,15 @@ pub(crate) fn convert_module_import(import: wasmparser::Import<'_>) -> Result<Im
101101
pub(crate) fn convert_module_memories<T: IntoIterator<Item = wasmparser::Result<wasmparser::MemoryType>>>(
102102
memory_types: T,
103103
) -> Result<Vec<MemoryType>> {
104-
memory_types.into_iter().map(|memory| convert_module_memory(memory?)).collect::<Result<Vec<_>>>()
104+
memory_types.into_iter().map(|memory| Ok(convert_module_memory(memory?))).collect::<Result<Vec<_>>>()
105105
}
106106

107-
pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> Result<MemoryType> {
108-
Ok(MemoryType {
109-
arch: match memory.memory64 {
110-
true => MemoryArch::I64,
111-
false => MemoryArch::I32,
112-
},
107+
pub(crate) fn convert_module_memory(memory: wasmparser::MemoryType) -> MemoryType {
108+
MemoryType {
109+
arch: if memory.memory64 { MemoryArch::I64 } else { MemoryArch::I32 },
113110
page_count_initial: memory.initial,
114111
page_count_max: memory.maximum,
115-
})
112+
}
116113
}
117114

118115
pub(crate) fn convert_module_tables<'a, T: IntoIterator<Item = wasmparser::Result<wasmparser::Table<'a>>>>(
@@ -129,12 +126,12 @@ pub(crate) fn convert_module_table(table: wasmparser::Table<'_>) -> Result<Table
129126
let size_max = match table.ty.maximum {
130127
Some(max) => Some(
131128
max.try_into()
132-
.map_err(|_| crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {}", max)))?,
129+
.map_err(|_| crate::ParseError::UnsupportedOperator(format!("Table size max is too large: {max}")))?,
133130
),
134131
None => None,
135132
};
136133

137-
Ok(TableType { element_type: convert_reftype(&table.ty.element_type), size_initial, size_max })
134+
Ok(TableType { element_type: convert_reftype(table.ty.element_type), size_initial, size_max })
138135
}
139136

140137
pub(crate) fn convert_module_globals(
@@ -185,11 +182,11 @@ pub(crate) fn convert_module_code(
185182

186183
for i in 0..validator.len_locals() {
187184
match validator.get_local_type(i) {
188-
Some(wasmparser::ValType::I32) | Some(wasmparser::ValType::F32) => {
185+
Some(wasmparser::ValType::I32 | wasmparser::ValType::F32) => {
189186
local_addr_map.push(local_counts.c32);
190187
local_counts.c32 += 1;
191188
}
192-
Some(wasmparser::ValType::I64) | Some(wasmparser::ValType::F64) => {
189+
Some(wasmparser::ValType::I64 | wasmparser::ValType::F64) => {
193190
local_addr_map.push(local_counts.c64);
194191
local_counts.c64 += 1;
195192
}
@@ -225,7 +222,7 @@ pub(crate) fn convert_module_type(ty: wasmparser::RecGroup) -> Result<FuncType>
225222
Ok(FuncType { params, results })
226223
}
227224

228-
pub(crate) fn convert_reftype(reftype: &wasmparser::RefType) -> ValType {
225+
pub(crate) fn convert_reftype(reftype: wasmparser::RefType) -> ValType {
229226
match reftype {
230227
_ if reftype.is_func_ref() => ValType::RefFunc,
231228
_ if reftype.is_extern_ref() => ValType::RefExtern,
@@ -240,7 +237,7 @@ pub(crate) fn convert_valtype(valtype: &wasmparser::ValType) -> ValType {
240237
wasmparser::ValType::F32 => ValType::F32,
241238
wasmparser::ValType::F64 => ValType::F64,
242239
wasmparser::ValType::V128 => ValType::V128,
243-
wasmparser::ValType::Ref(r) => convert_reftype(r),
240+
wasmparser::ValType::Ref(r) => convert_reftype(*r),
244241
}
245242
}
246243

@@ -260,7 +257,7 @@ pub(crate) fn process_const_operators(ops: OperatorsReader<'_>) -> Result<ConstI
260257
wasmparser::Operator::F32Const { value } => Ok(ConstInstruction::F32Const(f32::from_bits(value.bits()))),
261258
wasmparser::Operator::F64Const { value } => Ok(ConstInstruction::F64Const(f64::from_bits(value.bits()))),
262259
wasmparser::Operator::GlobalGet { global_index } => Ok(ConstInstruction::GlobalGet(*global_index)),
263-
op => Err(crate::ParseError::UnsupportedOperator(format!("Unsupported const instruction: {:?}", op))),
260+
op => Err(crate::ParseError::UnsupportedOperator(format!("Unsupported const instruction: {op:?}"))),
264261
}
265262
}
266263

crates/parser/src/error.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ impl Display for ParseError {
4242
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
4343
match self {
4444
Self::InvalidType => write!(f, "invalid type"),
45-
Self::UnsupportedSection(section) => write!(f, "unsupported section: {}", section),
46-
Self::DuplicateSection(section) => write!(f, "duplicate section: {}", section),
47-
Self::EmptySection(section) => write!(f, "empty section: {}", section),
48-
Self::UnsupportedOperator(operator) => write!(f, "unsupported operator: {}", operator),
45+
Self::UnsupportedSection(section) => write!(f, "unsupported section: {section}"),
46+
Self::DuplicateSection(section) => write!(f, "duplicate section: {section}"),
47+
Self::EmptySection(section) => write!(f, "empty section: {section}"),
48+
Self::UnsupportedOperator(operator) => write!(f, "unsupported operator: {operator}"),
4949
Self::ParseError { message, offset } => {
50-
write!(f, "error parsing module: {} at offset {}", message, offset)
50+
write!(f, "error parsing module: {message} at offset {offset}")
5151
}
52-
Self::InvalidEncoding(encoding) => write!(f, "invalid encoding: {:?}", encoding),
52+
Self::InvalidEncoding(encoding) => write!(f, "invalid encoding: {encoding:?}"),
5353
Self::InvalidLocalCount { expected, actual } => {
54-
write!(f, "invalid local count: expected {}, actual {}", expected, actual)
54+
write!(f, "invalid local count: expected {expected}, actual {actual}")
5555
}
5656
Self::EndNotReached => write!(f, "end of module not reached"),
57-
Self::Other(message) => write!(f, "unknown error: {}", message),
57+
Self::Other(message) => write!(f, "unknown error: {message}"),
5858
}
5959
}
6060
}

0 commit comments

Comments
 (0)