Skip to content

Commit d9d7717

Browse files
authored
esp-config (#2156)
* Initial esp-config poc, replacing the place-spi-driver-in-ram feature * Allow documentation generation for configuration options * add `Value::Number` and a macro to parse * Add Value::String and replace esp-wifi's config * repo maint * make bool parsing stricter and number parsing more flexible * use hand rolled const str to int * Collect unknown config options * friendly errors * also batch invalid values * dump msrv to 1.79 * Mention perf boost from disabling logging * review suggestions * output selected config * changelogs and migration guides * review feedback * avoid multiple case conversions where possible * refactor generate, fix bug, add full test * run host tests in CI * add more esp-config tests * review comments * add cargo env workaround
1 parent 46be3b1 commit d9d7717

File tree

21 files changed

+731
-179
lines changed

21 files changed

+731
-179
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ on:
2323
env:
2424
CARGO_TERM_COLOR: always
2525
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26-
MSRV: "1.77.0"
26+
MSRV: "1.79.0"
2727
RUSTDOCFLAGS: -Dwarnings
2828
DEFMT_LOG: trace
2929

@@ -179,3 +179,19 @@ jobs:
179179

180180
# Check the formatting of all packages:
181181
- run: cargo xtask fmt-packages --check
182+
183+
# --------------------------------------------------------------------------
184+
# host tests
185+
186+
host-tests:
187+
runs-on: ubuntu-latest
188+
189+
steps:
190+
- uses: actions/checkout@v4
191+
- uses: dtolnay/rust-toolchain@v1
192+
with:
193+
toolchain: stable
194+
- uses: Swatinem/rust-cache@v2
195+
196+
# Check the formatting of all packages:
197+
- run: cd esp-config && cargo test --features build

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ exclude = [
55
"esp-alloc",
66
"esp-backtrace",
77
"esp-build",
8+
"esp-config",
89
"esp-hal",
910
"esp-hal-embassy",
1011
"esp-hal-procmacros",

esp-config/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "esp-config"
3+
version = "0.1.0"
4+
edition = "2021"
5+
rust-version = "1.79.0"
6+
7+
[dependencies]
8+
document-features = "0.2.10"
9+
10+
[dev-dependencies]
11+
temp-env = "0.3.6"
12+
13+
[features]
14+
## Enable the generation and parsing of a config
15+
build = []

esp-config/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# esp-config
2+
3+
[![Crates.io](https://img.shields.io/crates/v/esp-config?labelColor=1C2C2E&color=C96329&logo=Rust&style=flat-square)](https://crates.io/crates/esp-config)
4+
[![docs.rs](https://img.shields.io/docsrs/esp-config?labelColor=1C2C2E&color=C96329&logo=rust&style=flat-square)](https://docs.rs/esp-config)
5+
![MSRV](https://img.shields.io/badge/MSRV-1.79-blue?labelColor=1C2C2E&style=flat-square)
6+
![Crates.io](https://img.shields.io/crates/l/esp-config?labelColor=1C2C2E&style=flat-square)
7+
[![Matrix](https://img.shields.io/matrix/esp-rs:matrix.org?label=join%20matrix&labelColor=1C2C2E&color=BEC5C9&logo=matrix&style=flat-square)](https://matrix.to/#/#esp-rs:matrix.org)
8+
9+
## [Documentation](https://docs.rs/crate/esp-config)
10+
11+
## Usage
12+
13+
`esp-config` takes a prefix (usually the crate name) and a set of configuration keys and default values to produce a configuration system that supports:
14+
15+
- Emitting rustc cfg's for boolean keys
16+
- Emitting environment variables for numbers
17+
- Along with decimal parsing, it supports Hex, Octal and Binary with the respective `0x`, `0o` and `0b` prefixes.
18+
- Emitting environment variables string values
19+
20+
### Viewing the configuration
21+
22+
The possible configuration values are output as a markdown table in the crates `OUT_DIR` with the format `{prefix}_config_table.md`, this can then be included into the crates top level documentation. Here is an example of the output:
23+
24+
25+
| Name | Description | Default value |
26+
|------|-------------|---------------|
27+
|**ESP_HAL_PLACE_SPI_DRIVER_IN_RAM**|Places the SPI driver in RAM for better performance|false|
28+
29+
### Setting configuration options
30+
31+
For any available configuration option, the environment variable or cfg is _always_ set based on the default value specified in the table. Users can override this by setting environment variables locally in their shell _or_ the preferred option is to utilize cargo's [`env` section](https://doc.rust-lang.org/cargo/reference/config.html#env).
32+
33+
It's important to note that due to a [bug in cargo](https://github.com/rust-lang/cargo/issues/10358), any modifications to the environment, local or otherwise will only get picked up on a full clean build of the project.
34+
35+
To see the final selected configuration another table is output to the `OUT_DIR` with the format `{prefix}_selected_config.md`.
36+
37+
### Capturing configuration values in the downstream crate
38+
39+
For all supported data types, there are helper macros that emit `const` code for parsing the configuration values.
40+
41+
- Numbers - `esp_config_int!(integer_type, "ENV")`
42+
- Strings - `esp_config_str!("ENV")`
43+
- Bool - `esp_config_bool!("ENV")`
44+
45+
In addition to environment variables, for boolean types rust `cfg`'s are emitted in snake case _without_ the prefix.
46+
47+
## Minimum Supported Rust Version (MSRV)
48+
49+
This crate is guaranteed to compile on stable Rust 1.79 and up. It _might_
50+
compile with older versions but that may change in any new patch release.
51+
52+
## License
53+
54+
Licensed under either of:
55+
56+
- Apache License, Version 2.0 ([LICENSE-APACHE](../LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
57+
- MIT license ([LICENSE-MIT](../LICENSE-MIT) or http://opensource.org/licenses/MIT)
58+
59+
at your option.
60+
61+
### Contribution
62+
63+
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in
64+
the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without
65+
any additional terms or conditions.

0 commit comments

Comments
 (0)