pub struct Builder { /* private fields */ }Expand description
Builds an object and emits its canonical bytes.
Implementations§
Source§impl Builder
impl Builder
Sourcepub fn created(self, when: &str) -> Self
pub fn created(self, when: &str) -> Self
Set the creation instant (RFC 3339 UTC, second precision).
§Example
use publet_core::Object;
let bytes = Object::builder("claim.prose", AUTHOR)
.created("2026-10-05T00:00:00Z")
.build()?;
assert_eq!(Object::parse(&bytes)?.peek().created(), "2026-10-05T00:00:00Z");Sourcepub fn field(self, key: &str, value: Value) -> Self
pub fn field(self, key: &str, value: Value) -> Self
Insert a body field.
§Example
use publet_core::{Object, cbor::Value};
// Fields may be added in any order; the encoding sorts them.
let one = Object::builder("note", AUTHOR)
.created("2026-10-05T00:00:00Z")
.field("b", Value::Uint(2))
.field("a", Value::Uint(1))
.build()?;
let other = Object::builder("note", AUTHOR)
.created("2026-10-05T00:00:00Z")
.field("a", Value::Uint(1))
.field("b", Value::Uint(2))
.build()?;
assert_eq!(one, other);Sourcepub fn prev(self, cid: &str) -> Self
pub fn prev(self, cid: &str) -> Self
Name the author’s previous object.
§Example
use publet_core::{Cid, HashAlg, Object};
let first = Object::builder("note", AUTHOR).created("2026-10-05T00:00:00Z").build()?;
let first_cid = Cid::of(&first, HashAlg::Sha2_256);
let second = Object::builder("note", AUTHOR)
.created("2026-10-06T00:00:00Z")
.prev(&first_cid.to_string())
.build()?;
assert_eq!(Object::parse(&second)?.peek().prev(), Some(&first_cid));Sourcepub fn basis(self, cid: &str) -> Self
pub fn basis(self, cid: &str) -> Self
Name the state the author was looking at (Section 4.6).
§Example
use publet_core::{Cid, HashAlg, Object};
// The state the author had read when writing this.
let read = Cid::of(b"the head the author was looking at", HashAlg::Sha2_256);
let bytes = Object::builder("note", AUTHOR)
.created("2026-10-05T00:00:00Z")
.basis(&read.to_string())
.build()?;
assert_eq!(Object::parse(&bytes)?.peek().basis(), Some(&read));Sourcepub fn build(self) -> Result<Vec<u8>, ObjectError>
pub fn build(self) -> Result<Vec<u8>, ObjectError>
Emit canonical bytes.
§Errors
Returns ObjectError if a CID-valued field does not parse or if
no creation instant was set.
§Example
use publet_core::{Object, ObjectError};
let bytes = Object::builder("note", AUTHOR).created("2026-10-05T00:00:00Z").build()?;
assert!(Object::parse(&bytes).is_ok());
// A creation instant is required.
assert_eq!(
Object::builder("note", AUTHOR).build(),
Err(ObjectError::MissingField { field: "created" }),
);
// The author must be an identifier.
assert!(matches!(
Object::builder("note", "not an identifier").created("2026-10-05T00:00:00Z").build(),
Err(ObjectError::BadCid { field: "author", .. }),
));Trait Implementations§
Auto Trait Implementations§
impl Freeze for Builder
impl RefUnwindSafe for Builder
impl Send for Builder
impl Sync for Builder
impl Unpin for Builder
impl UnsafeUnpin for Builder
impl UnwindSafe for Builder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more