serde_config.rs
Overview
This file provides utility functions for serializing and deserializing the application's configuration data to and from YAML files. It primarily acts as an interface between the configuration data model (Config) and the file system, enabling persistent storage and retrieval of configuration settings in a human-readable YAML format. The file encapsulates error handling to ensure that issues in reading, writing, or parsing configuration files are reported clearly.
Functions
load_config_from_file
pub fn load_config_from_file(path: &PathBuf) -> anyhow::Result<Config>
Purpose: Reads a YAML configuration file from the specified file path and deserializes its contents into a
Configstruct.Parameters:
path: &PathBuf- A reference to aPathBufspecifying the location of the configuration file to load.
Returns:
anyhow::Result<Config>- On success, returns the deserializedConfigobject. On failure, returns an error wrapped inanyhow::Resultdetailing the nature of the failure (file access or deserialization).
Error Handling:
If the file cannot be opened or read, it returns an error with the message
"Failed to open config file: <error>".If deserialization from YAML fails, it returns an error with the message
"Failed to deserialize config: <error>".
Usage Example:
use std::path::PathBuf;
let config_path = PathBuf::from("config.yaml");
match load_config_from_file(&config_path) {
Ok(config) => {
// Use the loaded configuration
}
Err(e) => {
eprintln!("Error loading config: {}", e);
}
}
save_config_to_file
pub fn save_config_to_file(config: &Config, path: &PathBuf) -> anyhow::Result<()>
Purpose: Serializes a given
Configstruct into a YAML string and writes it to the specified file path.Parameters:
config: &Config- A reference to theConfigobject that needs to be saved.path: &PathBuf- A reference to aPathBufspecifying the location where the configuration file will be written.
Returns:
anyhow::Result<()>- ReturnsOk(())on success; otherwise, returns an error wrapped inanyhow::Resultindicating serialization or file write failure.
Error Handling:
If serialization to YAML fails, returns an error with the message
"Failed to serialize config: <error>".If writing to the file fails, the underlying I/O error is propagated.
Usage Example:
use std::path::PathBuf;
let config = Config { /* fields initialized */ };
let save_path = PathBuf::from("config.yaml");
if let Err(e) = save_config_to_file(&config, &save_path) {
eprintln!("Error saving config: {}", e);
}
Implementation Details
The file uses the
serde_yamlcrate for YAML serialization and deserialization, leveragingserdetraits implemented on theConfigstruct.Error handling is based on the
anyhowcrate, which provides flexible error contexts and propagation.File I/O operations utilize the standard library functions
std::fs::read_to_stringandstd::fs::write.The design ensures that error messages provide clear diagnostics about whether the failure occurred during file access or during YAML serialization/deserialization.
Interactions with other parts of the system
The file depends on the
Configstruct from thecrate::configmodule, which defines the schema for the configuration data.It serves as a bridge between the persistent storage layer (files on disk) and the in-memory representation (
Config) used throughout the application.Other components that require loading or saving configuration settings invoke these functions to maintain consistent and centralized management of configuration persistence.
Integration with the error handling strategy across the system is achieved by using
anyhow::Result, which simplifies error propagation and reporting.
Diagram
flowchart TD
A["load_config_from_file(path)"] --> B[Read file contents]
B --> C{Success?}
C -- Yes --> D[Deserialize YAML to Config]
D --> E{Success?}
E -- Yes --> F[Return Config]
E -- No --> G[Return Deserialization Error]
C -- No --> H[Return File Read Error]
I["save_config_to_file(config, path)"] --> J[Serialize Config to YAML]
J --> K{Success?}
K -- Yes --> L[Write YAML string to file]
L --> M{Success?}
M -- Yes --> N[Return Ok]
M -- No --> O[Return File Write Error]
K -- No --> P[Return Serialization Error]
This flowchart illustrates the sequential steps and error handling paths in the two main functions for loading and saving configuration data.