Skip to content

Commit

Permalink
feat: define macros for const config from env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
ROMemories committed Jan 18, 2024
1 parent e15bfbc commit 18619c8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ riot-wrappers = { version = "^0.8", default-features = false, features = [

static_cell = { version = "2.0.0", features = [ "nightly" ] }
ld-memory = { version = "0.2.9" }
konst = { version = "0.3.8", default_features = false }
const_panic = { version = "0.2.8", default_features = false }

[profile.dev]
incremental = false
Expand Down
2 changes: 2 additions & 0 deletions src/riot-rs-embassy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ embassy-time = { workspace = true, optional = true }
embassy-usb = { workspace = true, optional = true }
embassy-net = { workspace = true, optional = true, features = [ "dhcpv4", "medium-ethernet" ] }
heapless = "0.8.0"
konst = { workspace = true, features = ["parsing"] }
const_panic = { workspace = true }

[target.'cfg(context = "cortex-m")'.dependencies]
embassy-executor = { workspace = true, features = [
Expand Down
27 changes: 27 additions & 0 deletions src/riot-rs-embassy/src/env_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
macro_rules! define_env_with_default_macro {
($macro_name:ident, $parse_fn_name:ident, $output_type_name:literal) => {
#[macro_export]
macro_rules! $macro_name {
($env_var:literal, $default:expr) => {
if let Some(str_value) = option_env!($env_var) {
if let Ok(value) = konst::primitive::$parse_fn_name(str_value) {
value
} else {
const_panic::concat_panic!(
"Could not parse environment variable `",
$env_var,
"=",
str_value,
"` as ",
$output_type_name,
);
}
} else {
$default
}
};
}
};
}

define_env_with_default_macro!(usize_env_or, parse_usize, "a usize");
1 change: 1 addition & 0 deletions src/riot-rs-embassy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#![feature(used_with_arg)]

pub mod define_peripherals;
mod env_utils;

#[cfg_attr(context = "nrf52", path = "arch/nrf52.rs")]
#[cfg_attr(context = "rp2040", path = "arch/rp2040.rs")]
Expand Down

0 comments on commit 18619c8

Please sign in to comment.