Skip to content

Commit d63eb21

Browse files
committed
feature: treat boolean values in toml files as flags
1 parent dbf193e commit d63eb21

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

crates/cargo-gpu/src/spirv_cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl SpirvCli {
166166
if self.is_toolchain_install_consent {
167167
return Ok(());
168168
}
169+
log::debug!("asking for consent to install the required toolchain");
169170
crossterm::terminal::enable_raw_mode()?;
170171
crate::user_output!("{prompt} [y/n]: ");
171172
let input = crossterm::event::read()?;

crates/cargo-gpu/src/toml.rs

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,43 +75,20 @@ impl Toml {
7575
} else {
7676
anyhow::bail!("toml file '{}' must describe a workspace containing [workspace.metadata.rust-gpu.build] or a describe a crate with [package.metadata.rust-gpu.build]", path.display());
7777
};
78+
7879
log::info!(
7980
"building with [{toml_type}.metadata.rust-gpu.build] section of the toml file at '{}'",
8081
path.display()
8182
);
8283
log::debug!("table: {table:#?}");
8384

84-
let mut parameters: Vec<String> = table
85-
.get("build")
86-
.with_context(|| "toml is missing the 'build' table")?
87-
.as_table()
88-
.with_context(|| {
89-
format!("toml file's '{toml_type}.metadata.rust-gpu.build' property is not a table")
90-
})?
91-
.into_iter()
92-
.map(|(key, val)| -> anyhow::Result<Vec<String>> {
93-
Ok(if let toml::Value::String(string) = val {
94-
[format!("--{key}"), string.clone()].into()
95-
} else {
96-
let mut value = String::new();
97-
let ser = toml::ser::ValueSerializer::new(&mut value);
98-
serde::Serialize::serialize(val, ser)?;
99-
[format!("--{key}"), value].into()
100-
})
101-
})
102-
.collect::<anyhow::Result<Vec<Vec<String>>>>()?
103-
.into_iter()
104-
.flatten()
105-
.collect();
106-
parameters.insert(0, "cargo-gpu".to_owned());
107-
parameters.insert(1, "build".to_owned());
108-
10985
log::info!(
11086
"issuing cargo commands from the working directory '{}'",
11187
working_directory.display()
11288
);
11389
std::env::set_current_dir(working_directory)?;
11490

91+
let parameters = construct_build_parameters_from_toml_table(toml_type, table)?;
11592
log::debug!("build parameters: {parameters:#?}");
11693
if let Cli {
11794
command: Command::Build(mut build),
@@ -162,3 +139,60 @@ impl Toml {
162139
metadata.get("rust-gpu")?.as_table()
163140
}
164141
}
142+
143+
/// Construct the cli parameters to run a `cargo gpu build` command from a TOML table.
144+
fn construct_build_parameters_from_toml_table(
145+
toml_type: &str,
146+
mut table: toml::map::Map<String, toml::Value>,
147+
) -> Result<Vec<String>, anyhow::Error> {
148+
let build_table = table
149+
.get_mut("build")
150+
.with_context(|| "toml is missing the 'build' table")?
151+
.as_table_mut()
152+
.with_context(|| {
153+
format!("toml file's '{toml_type}.metadata.rust-gpu.build' property is not a table")
154+
})?;
155+
let auto_install_rust_toolchain = if build_table.contains_key("auto_install_rust_toolchain") {
156+
build_table
157+
.remove("auto_install_rust_toolchain")
158+
.context("unreachable")?
159+
.as_bool()
160+
.context("auto_install_rust_toolchain must be bool")?
161+
} else {
162+
false
163+
};
164+
let mut parameters: Vec<String> = build_table
165+
.into_iter()
166+
.map(|(key, val)| -> anyhow::Result<Vec<String>> {
167+
Ok(match val {
168+
toml::Value::String(string) => vec![format!("--{key}"), string.clone()],
169+
toml::Value::Boolean(truthy) => {
170+
if *truthy {
171+
vec![format!("--{key}")]
172+
} else {
173+
vec![]
174+
}
175+
}
176+
toml::Value::Integer(_)
177+
| toml::Value::Float(_)
178+
| toml::Value::Datetime(_)
179+
| toml::Value::Array(_)
180+
| toml::Value::Table(_) => {
181+
let mut value = String::new();
182+
let ser = toml::ser::ValueSerializer::new(&mut value);
183+
serde::Serialize::serialize(val, ser)?;
184+
vec![format!("--{key}"), value]
185+
}
186+
})
187+
})
188+
.collect::<anyhow::Result<Vec<Vec<String>>>>()?
189+
.into_iter()
190+
.flatten()
191+
.collect();
192+
parameters.insert(0, "cargo-gpu".to_owned());
193+
parameters.insert(1, "build".to_owned());
194+
if auto_install_rust_toolchain {
195+
parameters.push("--auto_install_rust_toolchain".to_owned());
196+
}
197+
Ok(parameters)
198+
}

0 commit comments

Comments
 (0)