pub struct ConfigFile { /* private fields */ }Expand description
A loaded per-application config file.
cli-engine reads a single TOML file at <config-base>/<app_id>/config.toml
(see config_file_path). Engine-reserved settings live in documented
top-level tables (today just [credentials], see EngineConfig); consumer
CLIs own every other top-level table and read them with section or
deserialize. The file is also surfaced to command handlers via
CommandContext::config and to
module registration via
ModuleContext::config.
Edits made with set preserve existing comments and formatting (backed by
toml_edit) and are persisted with save.
Implementations§
Source§impl ConfigFile
impl ConfigFile
Sourcepub fn load(app_id: &str) -> Self
pub fn load(app_id: &str) -> Self
Loads the config file for app_id.
Best-effort: a missing file, unresolvable config directory, or malformed
TOML yields an empty document (a warning is logged for the malformed
case). The resolved path is retained for save even
when the file does not yet exist.
Blocking: this function performs synchronous filesystem I/O. The
engine calls it once at Cli::new time (outside of command execution),
which is acceptable for a one-shot CLI. Avoid calling it from hot paths
or from within an async executor without spawn_blocking.
Sourcepub fn path(&self) -> Option<&Path>
pub fn path(&self) -> Option<&Path>
Returns the resolved config file path, if a config directory was
available. None means neither XDG_CONFIG_HOME/HOME nor APPDATA
resolved to an absolute path (so nothing can be loaded or saved).
Sourcepub fn engine(&self) -> EngineConfig
pub fn engine(&self) -> EngineConfig
Deserializes the engine-reserved sections into an EngineConfig.
Lenient: any deserialization error (for example an invalid
[credentials].store) yields EngineConfig::default.
Sourcepub fn section<T: DeserializeOwned>(&self, name: &str) -> Result<Option<T>>
pub fn section<T: DeserializeOwned>(&self, name: &str) -> Result<Option<T>>
Deserializes a single top-level table name into T, or Ok(None) when
the key is absent.
Use this to read a consumer-owned section such as [deploy]:
cfg.section::<DeployConfig>("deploy")?.
§Errors
Returns an error when the table is present but does not deserialize into
T.
Sourcepub fn deserialize<T: DeserializeOwned>(&self) -> Result<T>
pub fn deserialize<T: DeserializeOwned>(&self) -> Result<T>
Deserializes the entire config file into a consumer root type T.
The root type may include the engine-reserved sections alongside its own;
unknown keys are tolerated when T allows them.
§Errors
Returns an error when the document does not deserialize into T.
Sourcepub fn get(&self, dotted_key: &str) -> Option<String>
pub fn get(&self, dotted_key: &str) -> Option<String>
Returns the string form of the value at a dotted key (for example
credentials.store or deploy.region), or None when absent.
Scalars render without quotes; a table renders as its TOML fragment.
Sourcepub fn set(&mut self, dotted_key: &str, value: &str) -> Result<()>
pub fn set(&mut self, dotted_key: &str, value: &str) -> Result<()>
Sets the value at a dotted key, creating intermediate tables as needed.
value is coerced to a TOML scalar type using these rules (in order):
"true"/"false"(case-sensitive) → TOML boolean.- Any string parseable as an
i64→ TOML integer. - Any string parseable as an
f64(including"1e5","inf","nan") → TOML float. - Everything else → TOML string.
To force a value to be stored as a string when it looks numeric (e.g.
a version like "1.0"), this API does not currently support quoting —
wrap the value in the config file by hand.
The engine-reserved [credentials] table is validated: only the known
key credentials.store is accepted; unknown keys in that table are
rejected. Existing comments and formatting elsewhere in the file are
preserved. Call save to persist.
§Errors
Returns an error for an empty/invalid key, an unknown engine-reserved key, an invalid engine value, or a key whose parent path is not a table.
Sourcepub fn to_toml_string(&self) -> String
pub fn to_toml_string(&self) -> String
Renders the whole config document back to a TOML string (preserving comments and formatting).
Trait Implementations§
Source§impl Clone for ConfigFile
impl Clone for ConfigFile
Source§fn clone(&self) -> ConfigFile
fn clone(&self) -> ConfigFile
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more