pub fn parse_config<T>(path: &Path) -> Result<T>where
T: DeserializeOwned,Expand description
Parse a TOML configuration file into the specified type.
Generic function that reads a TOML file and deserializes it into any type
that implements serde::de::DeserializeOwned. Provides enhanced error
messages that include the file path context.
§Type Parameters
T: The target type that implementsDeserializeOwned
§Parameters
path: Path to the TOML configuration file to parse
§Returns
The parsed configuration object of type T.
§Examples
§Basic Usage
use agpm_cli::config::parse_config;
use serde::Deserialize;
use std::path::Path;
#[derive(Deserialize)]
struct Config {
name: String,
port: u16,
}
let config: Config = parse_config(Path::new("server.toml"))?;
println!("Starting {} on port {}", config.name, config.port);§Error Handling
use agpm_cli::config::parse_config;
use serde::Deserialize;
use std::path::Path;
#[derive(Deserialize)]
struct Config { name: String }
match parse_config::<Config>(Path::new("missing.toml")) {
Ok(config) => println!("Config loaded: {}", config.name),
Err(e) => eprintln!("Failed to load config: {}", e),
}§Error Conditions
This function returns an error if:
§File System Errors
- File does not exist
- Insufficient permissions to read the file
- I/O errors during file reading
- Path is a directory, not a file
§Parsing Errors
- File contains invalid TOML syntax
- TOML structure doesn’t match the target type
T - Required fields are missing
- Field types don’t match expectations
- TOML contains unsupported features for the target type
§Error Messages
The function provides two levels of error context:
- File Operation Context: “Failed to read config file: /path/to/file.toml”
- Parsing Context: “Failed to parse config file: /path/to/file.toml”
The underlying error (file system or TOML parsing) is preserved as the cause.
§Performance
- Reads the entire file into memory before parsing
- TOML parsing is generally fast for typical configuration file sizes
- No caching - each call performs a fresh read and parse
§Thread Safety
This function is thread-safe and can be called concurrently from multiple threads. Each call operates independently on the file system.