Skip to content

Commit cc42c6e

Browse files
committed
config(toml): introduce config
1 parent d71a7e4 commit cc42c6e

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

src/config.rs

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
use failure::{Error, ResultExt};
2+
use std::path::PathBuf;
3+
use std::{fs::File, io::Read};
4+
use toml::value::Table;
5+
use toml::Value;
6+
7+
// TODO(csmoe): introduce wasm-* config
8+
// use xxx::snip::SnipOptions as WasmSnipConfig;
9+
10+
// macro_rules! tool_config {
11+
// ($wasm_tool: ident,$config: ident) => {
12+
// #[derive(Debug, Clone)]
13+
// enum $wasm_tool {
14+
// Bool(bool),
15+
// Config($config),
16+
// }
17+
//
18+
// impl Default for $wasm_tool {
19+
// fn default() -> Self {
20+
// $wasm_tool::Bool(false)
21+
// }
22+
// }
23+
// };
24+
// }
25+
//
26+
// tool_config!(WasmOpt, WasmOptConfig);
27+
// tool_config!(WasmSnip, WasmSnipConfig);
28+
29+
#[derive(Debug, Default, Clone)]
30+
pub(crate) struct BuildConfig {
31+
pub(crate) manifest_path: Option<PathBuf>,
32+
pub(crate) build: Option<BuildMode>,
33+
}
34+
35+
#[derive(Debug, Default, Clone)]
36+
pub(crate) struct BuildMode {
37+
pub(crate) debug: Option<WasmPackConfig>,
38+
pub(crate) profiling: Option<WasmPackConfig>,
39+
pub(crate) production: Option<WasmPackConfig>,
40+
}
41+
42+
impl BuildMode {
43+
fn from_table(table: Table) -> Result<BuildMode, Error> {
44+
let mut build_mode = BuildMode::default();
45+
for (key, value) in table {
46+
match (key.as_str(), value.clone()) {
47+
("debug", Value::Table(t)) => {
48+
build_mode.debug = Some(WasmPackConfig::from_table(t)?)
49+
}
50+
("profiling", Value::Table(t)) => {
51+
build_mode.profiling = Some(WasmPackConfig::from_table(t)?);
52+
}
53+
("production", Value::Table(t)) => {
54+
build_mode.production = Some(WasmPackConfig::from_table(t)?);
55+
}
56+
(key, value) => Err(format_err!(
57+
"unexpected \
58+
`package.metadata.wasm_pack.build` key `{}` with value `{}`",
59+
key,
60+
value
61+
))?,
62+
}
63+
}
64+
Ok(build_mode)
65+
}
66+
}
67+
68+
#[derive(Debug, Default, Clone)]
69+
pub(crate) struct WasmPackConfig {
70+
pub(crate) debug: Option<bool>,
71+
pub(crate) features: Option<Vec<String>>,
72+
pub(crate) rustc_opt_level: Option<String>,
73+
// pub(crate) wasm_opt: Option<WasmOptConfig>,
74+
// pub(crate) wasm_snip: Option<WasmSnipConfig>,
75+
pub(crate) wasm_bindgen: Option<bool>,
76+
}
77+
78+
impl WasmPackConfig {
79+
fn from_table(table: Table) -> Result<WasmPackConfig, Error> {
80+
let mut wasm_pack_config = WasmPackConfig::default();
81+
for (key, value) in table {
82+
match (key.as_str(), value.clone()) {
83+
("debug", Value::Boolean(b)) => wasm_pack_config.debug = From::from(b),
84+
("features", Value::Array(a)) => {
85+
let mut features = Vec::new();
86+
for value in a {
87+
match value {
88+
Value::String(s) => features.push(s),
89+
_ => Err(format_err!("features must be a list of strings"))?,
90+
}
91+
}
92+
wasm_pack_config.features = Some(features);
93+
}
94+
("rustc-opt-level", Value::String(s)) => {
95+
wasm_pack_config.rustc_opt_level = From::from(s)
96+
}
97+
// ("wasm-opt", opt) => match opt {
98+
// WasmOpt::Bool(Value::Boolean(b)) => wasm_pack_config.wasm_opt = From::from(b),
99+
// WasmOpt::Config(Value::Table(t)) => {
100+
// let mut opt_config = WasmOptConfig::default();
101+
// for (key, value) in t {
102+
// match (key.as_str(), value.clone()) {
103+
// ("opt-level", Value::String(s)) => {
104+
// opt_config.opt_level = From::from(s)
105+
// }
106+
// }
107+
// }
108+
// wasm_pack_config.wasm_opt = Some(opt_config);
109+
// }
110+
// },
111+
// ("wasm-snip", snip) => match snip {
112+
// WasmSnip::Bool(Value::Boolean(b)) => wasm_pack_config.wasm_snip = From::from(b),
113+
// WasmSnip::Config(Value::Table(t)) => {
114+
// let mut snip_config = WasmSnipConfig::default();
115+
// for (key, value) in t {
116+
// match (key.as_str(), value.clone()) {
117+
// ("snip-rust-fmt-code", Value::Boolean(b)) => {
118+
// snip_config.snip_rust_fmt_code = From::from(b)
119+
// }
120+
// ("snip-rust-panicking-code", Value::Boolean(b)) => {
121+
// snip_config.snip_rust_panicking_code = From::from(b)
122+
// }
123+
// }
124+
// }
125+
// wasm_pack_config.wasm_snip = Some(snip_config);
126+
// }
127+
// },
128+
("wasm-bindgen", Value::Boolean(b)) => {
129+
wasm_pack_config.wasm_bindgen = From::from(b)
130+
}
131+
(key, value) => Err(format_err!(
132+
"unexpected \
133+
`package.metadata.wasm_pack.build.<buildmode>` key `{}` with value `{}`",
134+
key,
135+
value
136+
))?,
137+
}
138+
}
139+
Ok(wasm_pack_config)
140+
}
141+
}
142+
143+
pub(crate) fn parse_config(manifest_path: PathBuf) -> Result<BuildConfig, Error> {
144+
let cargo_toml: Value = {
145+
let mut content = String::new();
146+
File::open(&manifest_path)
147+
.context("Failed to open Cargo.toml")?
148+
.read_to_string(&mut content)
149+
.context("Failed to read Cargo.toml")?;
150+
content
151+
.parse::<Value>()
152+
.context("Failed to parse Cargo.toml")?
153+
};
154+
155+
let metadata = cargo_toml
156+
.get("package")
157+
.and_then(|table| table.get("metadata"))
158+
.and_then(|table| table.get("wasm-pack"));
159+
let metadata = match metadata {
160+
None => {
161+
return Ok(BuildConfig {
162+
manifest_path: Some(manifest_path),
163+
..Default::default()
164+
})
165+
}
166+
Some(metadata) => metadata
167+
.as_table()
168+
.ok_or_else(|| format_err!("wasm-pack configuration invalid: {:?}", metadata))?,
169+
};
170+
171+
let mut config = BuildConfig {
172+
manifest_path: Some(manifest_path),
173+
..Default::default()
174+
};
175+
176+
for (key, value) in metadata {
177+
match (key.as_str(), value.clone()) {
178+
("build", Value::Table(t)) => config.build = Some(BuildMode::from_table(t)?),
179+
// FIXME(csmoe): May be more configs for wasm_pack except `build`
180+
(key, value) => Err(format_err!(
181+
"unexpected \
182+
`package.metadata.wasm_pack` key `{}` with value `{}`",
183+
key,
184+
value
185+
))?,
186+
}
187+
}
188+
189+
Ok(config)
190+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern crate toml;
1919
pub mod bindgen;
2020
pub mod build;
2121
pub mod command;
22+
pub mod config;
2223
pub mod emoji;
2324
pub mod error;
2425
pub mod logger;

0 commit comments

Comments
 (0)