omni_dev/data/
yaml.rs

1//! YAML processing utilities
2
3use anyhow::{Context, Result};
4use serde::{Deserialize, Serialize};
5use std::fs;
6use std::path::Path;
7
8/// Serialize data structure to YAML string
9pub fn to_yaml<T: Serialize>(data: &T) -> Result<String> {
10    serde_yaml::to_string(data).context("Failed to serialize data to YAML")
11}
12
13/// Deserialize YAML string to data structure
14pub fn from_yaml<T: for<'de> Deserialize<'de>>(yaml: &str) -> Result<T> {
15    serde_yaml::from_str(yaml).context("Failed to deserialize YAML")
16}
17
18/// Read and parse YAML file
19pub fn read_yaml_file<T: for<'de> Deserialize<'de>, P: AsRef<Path>>(path: P) -> Result<T> {
20    let content = fs::read_to_string(&path)
21        .with_context(|| format!("Failed to read file: {}", path.as_ref().display()))?;
22
23    from_yaml(&content)
24}
25
26/// Write data structure to YAML file
27pub fn write_yaml_file<T: Serialize, P: AsRef<Path>>(data: &T, path: P) -> Result<()> {
28    let yaml_content = to_yaml(data)?;
29
30    fs::write(&path, yaml_content)
31        .with_context(|| format!("Failed to write file: {}", path.as_ref().display()))?;
32
33    Ok(())
34}