This repository has been archived on 2026-08-31. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
donglora/build.rs
T
swaits 2633daf437 refactor: LoRaBoard trait for compile-time board completeness verification
Every board's Board struct now implements the LoRaBoard trait, which
enforces that NAME, TX_POWER_RANGE, init(), and mac_address() are all
provided. Missing any of these is a compile error — no "good intentions"
needed.

The trait verifies metadata and initialization. Tasks use the board's
concrete types directly (Embassy tasks must be concrete). Board-specific
type aliases (RadioDriver, UsbDriver, DisplayI2c) and struct definitions
(RadioParts, UsbParts, DisplayParts) remain in each board file.

- New: src/board/traits.rs (LoRaBoard trait definition)
- Modified: all 3 board files (implement trait, remove standalone constants)
- Modified: main.rs (import trait for init)
- Modified: radio/task.rs (use Board::TX_POWER_RANGE via trait)
- Modified: display/task.rs (use Board::NAME via trait)
- Modified: build.rs (exclude traits.rs from board discovery)
- Removed: embedded-hal-async direct dep (not needed for trait approach)
2026-03-30 08:44:39 -06:00

56 lines
1.8 KiB
Rust

use minijinja::{context, Environment};
use owo_colors::OwoColorize;
use std::fs;
fn main() {
let board_dir = "src/board";
let boards: Vec<String> = fs::read_dir(board_dir)
.expect("failed to read src/board directory")
.filter_map(|e| {
let p = e.ok()?.path();
let stem = p.file_stem()?.to_str()?;
if p.is_file() && p.extension()? == "rs" && stem != "mod" && stem != "traits" {
return Some(p.file_stem()?.to_str()?.to_string());
}
None
})
.collect();
let enabled: Vec<_> = std::env::vars()
.filter(|(k, _)| k.starts_with("CARGO_FEATURE_"))
.map(|(k, _)| k.replace("CARGO_FEATURE_", "").to_lowercase())
.filter(|f| boards.contains(f))
.collect();
match enabled.len() {
0 => panic!(
"\n\n{}\n",
format!("No board selected! Found: {:?}", boards)
.red()
.bold()
),
1 => println!("cargo:rustc-cfg=board_selected"),
_ => panic!(
"\n\n{}\n",
format!("Conflict: Multiple boards enabled {:?}", enabled)
.yellow()
.bold()
),
}
let mut env = Environment::new();
let template =
fs::read_to_string("src/board/mod.rs.j2").expect("missing src/board/mod.rs.j2");
env.add_template("mod", &template)
.expect("failed to parse board template");
let rendered = env
.get_template("mod")
.expect("template not found")
.render(context!(boards => boards))
.expect("failed to render board module");
fs::write("src/board/mod.rs", rendered).expect("failed to write src/board/mod.rs");
println!("cargo:rerun-if-changed=src/board/mod.rs.j2");
println!("cargo:rerun-if-changed={}", board_dir);
}