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>
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<()>
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

Interactions with other parts of the system

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.