openlatch-provider 0.2.1

Self-service onboarding CLI + runtime daemon for OpenLatch Editors and Providers
//! YAML round-trip helpers. Comments are NOT preserved in v0.1 — the
//! `editor update` / `register` flows regenerate the manifest cleanly.
//! Comment-preserving round-tripping is deferred to v0.2 when we adopt
//! `yaml-rust2`.

use std::path::Path;

use crate::error::{OlError, OL_4274_MANIFEST_WRITE_FAILED};
use crate::manifest::Manifest;

/// Atomically write a [`Manifest`] back to disk.
pub fn save(path: &Path, manifest: &Manifest) -> Result<(), OlError> {
    let serialized = serde_yaml::to_string(manifest).map_err(|e| {
        OlError::new(
            OL_4274_MANIFEST_WRITE_FAILED,
            format!("YAML serialize: {e}"),
        )
    })?;
    let tmp = path.with_extension("yaml.tmp");
    std::fs::write(&tmp, serialized.as_bytes()).map_err(|e| {
        OlError::new(
            OL_4274_MANIFEST_WRITE_FAILED,
            format!("write {}: {e}", tmp.display()),
        )
    })?;
    std::fs::rename(&tmp, path).map_err(|e| {
        OlError::new(
            OL_4274_MANIFEST_WRITE_FAILED,
            format!("rename {}: {e}", path.display()),
        )
    })?;
    Ok(())
}