pub fn write_feed<P: AsRef<Path>>(path: P, feed: Feed) -> Result<Feed>Expand description
Write a protobuf feed to disk atomically (best-effort).
§Behavior
- Ensures the parent directory of
pathexists (creates it if needed). - Encodes
feedto a temporary file with extension".pb.tmp". - Flushes and then renames the temp file over
path.- On Unix/POSIX, the rename is atomic when source and destination are on the same filesystem.
- On Windows,
renamemay fail if the destination exists; this function forwards that error as-is.
The input feed is consumed and returned unchanged on success to make
call sites ergonomic.
§Arguments
path: Destination path of the.pbfile.feed: The feed to persist (consumed).
§Returns
The same Feed value that was written (handy for chaining).
§Errors
- Directory creation errors from
fs::create_dir_all, with context"failed to create directory {dir}". - File creation/write/flush errors for the temporary file, with context
"failed to write {tmp}". - Rename errors when moving the temp file into place, with context
"failed to move temp file into place: {path}". - Protobuf encode errors from
feed.encode(&mut buf). - The error type is
anyhow::Errorvia your crate-wideResult.
§Example
use std::path::PathBuf;
use linkleaf_core::fs::{read_feed, write_feed};
use anyhow::Result;
fn main() -> Result<()> {
let path = PathBuf::from("mylinks.pb");
let mut feed = read_feed(&path)?; // or Feed { .. } if creating anew
feed.title = "My Links".into();
let written = write_feed(&path, feed)?; // atomic write
assert_eq!(written.title, "My Links");
Ok(())
}§Notes
- Atomicity requires the temporary file and the destination to be on the same filesystem.
- If multiple processes may write concurrently, consider adding a file lock around the write section.