linesmith_core/segments/extras.rs
1//! Workspace-level helpers for reading `[segments.<id>]` TOML extras.
2//!
3//! The render-time config bag is `BTreeMap<String, toml::Value>`; each
4//! segment's `from_extras` constructor reads its own keys out of it.
5//! Helpers here cover patterns that any segment can use
6//! (warn-on-wrong-type, silent-on-absent) so segment families don't
7//! reinvent them.
8//!
9//! Family-specific helpers (e.g. the rate-limit family's
10//! `apply_common_extras` and `parse_*_format` parsers) live next to
11//! the family they serve, not here.
12//!
13//! Warn-message format matches the existing convention:
14//! `segments.{id}.{key}: expected <type>; ignoring`.
15
16use std::collections::BTreeMap;
17
18/// Read a boolean knob from `[segments.<id>]`. Wrong-type values warn
19/// and return `None`; absent keys are silent.
20#[must_use]
21pub(crate) fn parse_bool(
22 extras: &BTreeMap<String, toml::Value>,
23 key: &str,
24 id: &str,
25 warn: &mut impl FnMut(&str),
26) -> Option<bool> {
27 let v = extras.get(key)?;
28 match v.as_bool() {
29 Some(b) => Some(b),
30 None => {
31 warn(&format!("segments.{id}.{key}: expected bool; ignoring"));
32 None
33 }
34 }
35}