Skip to content

Commit 87b28b9

Browse files
committed
Complain if a non-task crate uses task config
1 parent 52b6fca commit 87b28b9

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

build/util/src/lib.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn target_os() -> String {
4545

4646
/// Reads the `HUBRIS_TASK_NAME` env var.
4747
pub fn task_name() -> String {
48-
crate::env_var("HUBRIS_TASK_NAME").expect("missing HUBRIS_TASK_NAME")
48+
env_var("HUBRIS_TASK_NAME").expect("missing HUBRIS_TASK_NAME")
4949
}
5050

5151
/// Checks to see whether the given feature is enabled
@@ -84,7 +84,7 @@ pub fn expose_m_profile() -> Result<()> {
8484

8585
/// Returns the `HUBRIS_BOARD` envvar, if set.
8686
pub fn target_board() -> Option<String> {
87-
crate::env_var("HUBRIS_BOARD").ok()
87+
env_var("HUBRIS_BOARD").ok()
8888
}
8989

9090
/// Exposes the board type from the `HUBRIS_BOARD` envvar into
@@ -167,20 +167,32 @@ pub fn task_config<T: DeserializeOwned>() -> Result<T> {
167167
}
168168

169169
/// Pulls the task configuration, or `None` if the configuration is not
170-
/// provided.
170+
/// provided. Returns an error if the task full config is not present.
171171
pub fn task_maybe_config<T: DeserializeOwned>() -> Result<Option<T>> {
172-
let t = toml_from_env::<toml_task::Task<T>>("HUBRIS_TASK_CONFIG")?;
173-
Ok(t.and_then(|t| t.config))
172+
let t = task_full_config()?;
173+
Ok(t.config)
174174
}
175175

176176
/// Pulls the full task configuration block for the current task
177177
///
178178
/// (compare with `task_maybe_config`, which returns just the `config`
179179
/// subsection)
180180
pub fn task_full_config<T: DeserializeOwned>() -> Result<toml_task::Task<T>> {
181-
let t = toml_from_env::<toml_task::Task<T>>("HUBRIS_TASK_CONFIG")?
182-
.ok_or_else(|| anyhow!("HUBRIS_TASK_CONFIG is not defined"))?;
183-
Ok(t)
181+
try_task_full_config()?
182+
.ok_or_else(|| anyhow!("HUBRIS_TASK_CONFIG is not defined"))
183+
}
184+
185+
pub fn try_task_full_config<T: DeserializeOwned>(
186+
) -> Result<Option<toml_task::Task<T>>> {
187+
let out = toml_from_env::<toml_task::Task<T>>("HUBRIS_TASK_CONFIG")?;
188+
if let Some(out) = &out {
189+
let crate_name = env_var("CARGO_PKG_NAME").unwrap();
190+
assert_eq!(
191+
out.name, crate_name,
192+
"can't read task config from non-task crate {crate_name}"
193+
);
194+
}
195+
Ok(out)
184196
}
185197

186198
/// Pulls the full task configuration block, with the `config` subsection
@@ -280,7 +292,7 @@ impl TaskIds {
280292
/// - `Err(e)` if deserialization failed or the environment variable did not
281293
/// contain UTF-8.
282294
fn toml_from_env<T: DeserializeOwned>(var: &str) -> Result<Option<T>> {
283-
let config = match crate::env_var(var) {
295+
let config = match env_var(var) {
284296
Err(e) => {
285297
use std::env::VarError;
286298

drv/spartan7-loader/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ mod gen {
238238
include!(concat!(env!("OUT_DIR"), "/spartan7_fpga.rs"));
239239
}
240240

241+
#[cfg(feature = "use-spi-core")]
241242
mod spi_config {
242243
use drv_stm32h7_spi_server_core::__reexport::*;
243244
include!(concat!(env!("OUT_DIR"), "/spi_config.rs"));

0 commit comments

Comments
 (0)