use std::path::Path;
use crate::atomic_write::atomic_write;
use crate::errors::LockfileError;
use super::parse::parse_lockfile;
use super::schema::Lockfile;
use super::write::write_lockfile;
pub fn read_from(path: &Path) -> Result<Option<Lockfile>, LockfileError> {
match std::fs::read_to_string(path) {
Ok(source) => parse_lockfile(&source, Some(path)).map(Some),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(source) => Err(LockfileError::Io {
source,
path: Some(path.to_path_buf()),
}),
}
}
pub fn write_to(path: &Path, lockfile: &Lockfile) -> Result<(), LockfileError> {
let body = write_lockfile(lockfile);
atomic_write(path, body.as_bytes()).map_err(|source| LockfileError::Io {
source,
path: Some(path.to_path_buf()),
})
}