Skip to content

Commit e82fd3b

Browse files
authored
Merge pull request #198 from wasmx/module-config
Merge ModuleConfig trait into ChiselModule
2 parents 35bbe00 + f51af39 commit e82fd3b

File tree

14 files changed

+59
-78
lines changed

14 files changed

+59
-78
lines changed

libchisel/src/binaryenopt.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22

33
use parity_wasm::elements::Module;
44

5-
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator};
5+
use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator};
66

77
// FIXME: change level names
88
pub enum BinaryenOptimiser {
@@ -29,9 +29,7 @@ impl<'a> ChiselModule<'a> for BinaryenOptimiser {
2929
fn as_abstract(&'a self) -> Self::ObjectReference {
3030
self as Self::ObjectReference
3131
}
32-
}
3332

34-
impl ModuleConfig for BinaryenOptimiser {
3533
fn with_defaults() -> Result<Self, ModuleError> {
3634
Ok(BinaryenOptimiser::O2)
3735
}

libchisel/src/checkfloat.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22

33
use parity_wasm::elements::{Instruction, Module};
44

5-
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator};
5+
use super::{ChiselModule, ModuleError, ModuleKind, ModuleValidator};
66

77
/// Struct on which ModuleValidator is implemented.
88
pub struct CheckFloat {}
@@ -21,9 +21,7 @@ impl<'a> ChiselModule<'a> for CheckFloat {
2121
fn as_abstract(&'a self) -> Self::ObjectReference {
2222
self as Self::ObjectReference
2323
}
24-
}
2524

26-
impl ModuleConfig for CheckFloat {
2725
fn with_defaults() -> Result<Self, ModuleError> {
2826
Ok(CheckFloat {})
2927
}

libchisel/src/checkstartfunc.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22

33
use parity_wasm::elements::Module;
44

5-
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleValidator};
5+
use super::{ChiselModule, ModuleError, ModuleKind, ModuleValidator};
66

77
/// Struct on which ModuleValidator is implemented.
88
pub struct CheckStartFunc {
@@ -31,9 +31,7 @@ impl<'a> ChiselModule<'a> for CheckStartFunc {
3131
fn as_abstract(&'a self) -> Self::ObjectReference {
3232
self as Self::ObjectReference
3333
}
34-
}
3534

36-
impl ModuleConfig for CheckStartFunc {
3735
fn with_defaults() -> Result<Self, ModuleError> {
3836
Err(ModuleError::NotSupported)
3937
}

libchisel/src/deployer.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33
use parity_wasm::builder;
44
use parity_wasm::elements::{CustomSection, Module};
55

6-
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator};
6+
use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator};
77

88
/// Enum on which ModuleTranslator is implemented.
99
pub enum Deployer {
@@ -25,9 +25,7 @@ impl<'a> ChiselModule<'a> for Deployer {
2525
fn as_abstract(&'a self) -> Self::ObjectReference {
2626
self as Self::ObjectReference
2727
}
28-
}
2928

30-
impl ModuleConfig for Deployer {
3129
fn with_defaults() -> Result<Self, ModuleError> {
3230
Err(ModuleError::NotSupported)
3331
}

libchisel/src/dropsection.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ use std::error::Error;
33

44
use parity_wasm::elements::{Module, Section};
55

6-
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslator};
6+
use super::{ChiselModule, ModuleError, ModuleKind, ModuleTranslator};
7+
8+
impl From<std::num::ParseIntError> for ModuleError {
9+
fn from(error: std::num::ParseIntError) -> Self {
10+
ModuleError::Custom(error.description().to_string())
11+
}
12+
}
713

814
/// Enum on which ModuleTranslator is implemented.
915
#[derive(Debug)]
@@ -31,15 +37,7 @@ impl<'a> ChiselModule<'a> for DropSection {
3137
fn as_abstract(&'a self) -> Self::ObjectReference {
3238
self as Self::ObjectReference
3339
}
34-
}
35-
36-
impl From<std::num::ParseIntError> for ModuleError {
37-
fn from(error: std::num::ParseIntError) -> Self {
38-
ModuleError::Custom(error.description().to_string())
39-
}
40-
}
4140

42-
impl ModuleConfig for DropSection {
4341
fn with_defaults() -> Result<Self, ModuleError> {
4442
Err(ModuleError::NotSupported)
4543
}

libchisel/src/lib.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ pub trait ChiselModule<'a> {
4646

4747
/// Borrows the instance as a trait object.
4848
fn as_abstract(&'a self) -> Self::ObjectReference;
49+
50+
// Create instance with default settings.
51+
fn with_defaults() -> Result<Self, ModuleError>
52+
where
53+
Self: Sized;
54+
55+
// Create instance with a specific configuration.
56+
fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError>
57+
where
58+
Self: Sized;
4959
}
5060

5161
pub trait ModuleCreator {
@@ -66,23 +76,13 @@ pub trait ModuleValidator {
6676
fn validate(&self, module: &Module) -> Result<bool, ModuleError>;
6777
}
6878

79+
// TODO: remove this
6980
pub trait ModulePreset {
7081
fn with_preset(preset: &str) -> Result<Self, ModuleError>
7182
where
7283
Self: std::marker::Sized;
7384
}
7485

75-
// TODO: move this to be part of ChiselModule and retire ModulePreset
76-
pub trait ModuleConfig {
77-
fn with_defaults() -> Result<Self, ModuleError>
78-
where
79-
Self: std::marker::Sized;
80-
81-
fn with_config(config: &HashMap<String, String>) -> Result<Self, ModuleError>
82-
where
83-
Self: std::marker::Sized;
84-
}
85-
8686
impl From<String> for ModuleError {
8787
fn from(error: String) -> Self {
8888
ModuleError::Custom(error)
@@ -177,6 +177,14 @@ mod tests {
177177
fn as_abstract(&'a self) -> Self::ObjectReference {
178178
self as Self::ObjectReference
179179
}
180+
181+
fn with_defaults() -> Result<Self, ModuleError> {
182+
Err(ModuleError::NotSupported)
183+
}
184+
185+
fn with_config(_config: &HashMap<String, String>) -> Result<Self, ModuleError> {
186+
Err(ModuleError::NotSupported)
187+
}
180188
}
181189

182190
#[test]

libchisel/src/remapimports.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use std::collections::HashMap;
33
use parity_wasm::elements::{ImportEntry, ImportSection, Module};
44

55
use super::{
6-
imports::ImportList, ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset,
7-
ModuleTranslator,
6+
imports::ImportList, ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator,
87
};
98

109
pub struct RemapImports<'a> {
@@ -30,9 +29,7 @@ impl<'a> ChiselModule<'a> for RemapImports<'a> {
3029
fn as_abstract(&'a self) -> Self::ObjectReference {
3130
self as Self::ObjectReference
3231
}
33-
}
3432

35-
impl<'a> ModuleConfig for RemapImports<'a> {
3633
fn with_defaults() -> Result<Self, ModuleError> {
3734
Err(ModuleError::NotSupported)
3835
}

libchisel/src/remapstart.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22

33
use parity_wasm::elements::{ExportEntry, ExportSection, Internal, Module, Section};
44

5-
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModulePreset, ModuleTranslator};
5+
use super::{ChiselModule, ModuleError, ModuleKind, ModulePreset, ModuleTranslator};
66

77
pub struct RemapStart;
88

@@ -30,9 +30,7 @@ impl<'a> ChiselModule<'a> for RemapStart {
3030
fn as_abstract(&'a self) -> Self::ObjectReference {
3131
self as Self::ObjectReference
3232
}
33-
}
3433

35-
impl ModuleConfig for RemapStart {
3634
fn with_defaults() -> Result<Self, ModuleError> {
3735
Ok(RemapStart {})
3836
}

libchisel/src/repack.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33
use parity_wasm::builder;
44
use parity_wasm::elements::Module;
55

6-
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslator};
6+
use super::{ChiselModule, ModuleError, ModuleKind, ModuleTranslator};
77

88
pub struct Repack;
99

@@ -27,9 +27,7 @@ impl<'a> ChiselModule<'a> for Repack {
2727
fn as_abstract(&'a self) -> Self::ObjectReference {
2828
self as Self::ObjectReference
2929
}
30-
}
3130

32-
impl ModuleConfig for Repack {
3331
fn with_defaults() -> Result<Self, ModuleError> {
3432
Ok(Repack::new())
3533
}

libchisel/src/snip.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@ use std::collections::HashMap;
22

33
use parity_wasm::elements::Module;
44

5-
use super::{ChiselModule, ModuleConfig, ModuleError, ModuleKind, ModuleTranslator};
5+
use super::{ChiselModule, ModuleError, ModuleKind, ModuleTranslator};
6+
7+
// TODO: consider making this a generic helper?
8+
fn check_bool_option(config: &HashMap<String, String>, option: &str, default: bool) -> bool {
9+
if let Some(value) = config.get(option) {
10+
value == "true"
11+
} else {
12+
default
13+
}
14+
}
615

716
#[derive(Clone)]
817
pub struct Snip(wasm_snip::Options);
@@ -32,18 +41,7 @@ impl<'a> ChiselModule<'a> for Snip {
3241
fn as_abstract(&'a self) -> Self::ObjectReference {
3342
self as Self::ObjectReference
3443
}
35-
}
36-
37-
// TODO: consider making this a generic helper?
38-
fn check_bool_option(config: &HashMap<String, String>, option: &str, default: bool) -> bool {
39-
if let Some(value) = config.get(option) {
40-
value == "true"
41-
} else {
42-
default
43-
}
44-
}
4544

46-
impl ModuleConfig for Snip {
4745
fn with_defaults() -> Result<Self, ModuleError> {
4846
Ok(Snip::new())
4947
}

0 commit comments

Comments
 (0)