pub fn add<P, S, T>(
file: P,
title: S,
url: S,
summary: Option<Summary>,
tags: T,
via: Option<Via>,
id: Option<Uuid>,
) -> Result<Link>Expand description
Add or update a link in a protobuf feed file, then persist the feed.
§Behavior
- Reads the feed at
file. If it doesn’t exist, a new feed is initialized (version = 1). - If an
idis provided:- Updates the existing link with that
idif found (title, url, summary, tags, via), sets itsdateto today (local datetime,YYYY-MM-DD HH:MM:SS), and moves it to the front (newest-first). - Otherwise inserts a new link at the front with that explicit
id.
- Updates the existing link with that
- If no
idis provided:- Updates the first link whose
urlmatches; setsdateto today and moves it to the front. - Otherwise inserts a new link at the front with a freshly generated UUID v4
id.
- Updates the first link whose
Persists the entire feed by calling write_feed, which writes atomically
via a temporary file and rename.
§Arguments
file: Path to the.pbfeed file to update/create.title: Human-readable title for the link.url: Target URL for the link.summary: Optional blurb/notes (None-> empty string).tags: Zero or more tags as an iterator of strings (e.g.,["rust", "async", "tokio"]).via: Optional source/attribution (None-> empty string).id: Optional stable identifier. If present, performs an upsert byid.
§Returns
The newly created or updated Link.
§Ordering
Links are kept newest-first; both inserts and updates end up at index 0.
§Errors
- Propagates any error from
read_feed(except “not found”, which initializes a new feed). - Propagates any error from
write_feed. - No inter-process locking is performed; concurrent writers may race.
§Example
use std::path::PathBuf;
use linkleaf_core::*;
use linkleaf_core::linkleaf_proto::Summary;
use uuid::Uuid;
let file = PathBuf::from("mylinks.pb");
// Create a new link
let a = add(
file.clone(),
"Tokio - Asynchronous Rust",
"https://tokio.rs/",
None,
["rust", "async", "tokio"],
None,
None, // no id -> create (may update if URL already exists)
)?;
// Update the same link by id (upsert)
let _id = Uuid::parse_str(&a.id)?;
let a2 = add(
file.clone(),
"Tokio • Async Rust",
"https://tokio.rs/",
Some(Summary::new("A runtime for reliable async apps")),
[], // no tags change
None,
Some(_id), // provide id -> update or insert with that id
)?;
assert_eq!(a2.id, a.id);
Ok::<(), anyhow::Error>(())
// After update, the item is at the front (index 0).§Notes
- Providing an
idgives the item a stable identity; updates byidwill also update the storedurlto the new value you pass. dateis always set to “today” in local time on both create and update.