Skip to main content

Crate dynamic_config_embedded

Crate dynamic_config_embedded 

Source
Expand description

Hot-reloadable configuration for no_std targets.

dynamic-config reads files, searches directories and merges layers with figment. A microcontroller has no files, no directories and no allocator, and figment is std — so this is not that crate with a feature switched off. It is the same shape, built from what a device actually has.

use dynamic_config_embedded::{ConfigCell, Format, Validate};
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
struct Settings {
    interval_ms: u32,
    verbose: bool,
}

// Accepting everything is the default; implement it to reject a
// configuration whose fields are individually fine and jointly wrong.
impl Validate for Settings {}

static SETTINGS: ConfigCell<Settings> = ConfigCell::new();

// Compiled-in defaults, so the device is configured before anything arrives.
SETTINGS.store(Settings { interval_ms: 1000, verbose: false });

// A document from wherever this device gets one: a serial link, an MQTT
// message, a page of flash.
SETTINGS.apply(br#"{"interval_ms": 250, "verbose": true}"#, Format::Json)?;

assert_eq!(SETTINGS.get().unwrap().interval_ms, 250);

§What it keeps from the big crate

  • A snapshot in a static, replaced whole. A reader never sees a half-applied configuration.
  • A bad document cannot take the process down. Parsing and validation happen before anything is installed; a failure leaves the previous configuration serving.
  • changes(), a Future that resolves on the next configuration. The same generation-counter-and-wakers design as the std crate, which is why it drives on Embassy, RTIC, or a hand-written executor.
  • Validation, through the same Validate shape.

§What it cannot keep, and why

Files, directory search, profilesthere is no filesystem
Environment variablesthere is no environment
Layered mergingfigment is std, and merging needs a value tree that allocates
Arc snapshotsno allocator; readers get a Copy or a clone instead
Provenance (source_of)there is one source, so the question does not arise

A device gets one document at a time and replaces the whole configuration. That is not a reduced version of layering — it is what configuring a device looks like.

§No allocator, and no lock a reader can block on

Storage is a critical-section around a plain slot: a handful of instructions with interrupts masked, which is the primitive every embedded HAL provides and the only one this crate needs. Readers clone the value out; there is no Arc to hand back because there is no allocator to make one.

That makes T: Clone the price of admission. For a configuration struct of scalars — which is what a device’s configuration is — the clone is a memcpy.

Structs§

Changesasync
A handle that resolves each time the configuration is replaced.
ConfigCell
Process-wide storage for one configuration type.
Error
A configuration failure.

Enums§

ErrorKind
Why a document could not become a configuration.
Format
How a document is written.

Constants§

DEFAULT_WAITERS
How many tasks a ConfigCell can park by default.

Traits§

Validate
A configuration that can reject itself.