pub struct Object { /* private fields */ }Expand description
A parsed object: its header, its body, and the bytes it came from.
Implementations§
Source§impl Object
impl Object
Sourcepub fn parse(bytes: &[u8]) -> Result<Unverified<Self>, ObjectError>
pub fn parse(bytes: &[u8]) -> Result<Unverified<Self>, ObjectError>
Parse canonical bytes into an object whose identity is not yet checked.
§Errors
Returns ObjectError if the bytes are not canonical, if a required
header field is absent or ill-typed, or if an unrecognized field
appears at the top level.
§Example
use publet_core::{Cid, HashAlg, Object};
let bytes = Object::builder("claim.prose", AUTHOR)
.created("2026-10-05T00:00:00Z")
.build()?;
// Parsing checks the form; verifying checks the identity.
let unverified = Object::parse(&bytes)?;
let verified = unverified.verify(&Cid::of(&bytes, HashAlg::Sha2_256))?;
assert_eq!(verified.object().kind(), "claim.prose");§Example: what is refused
use publet_core::{Object, ObjectError};
// The integer 1 written in two bytes instead of one: not canonical.
assert!(matches!(Object::parse(&[0x18, 0x01]), Err(ObjectError::Canonical(_))));
// Canonical CBOR, but not a map.
assert!(matches!(Object::parse(&[0x01]), Err(ObjectError::NotAMap { .. })));Sourcepub fn builder(kind: &str, author: &str) -> Builder
pub fn builder(kind: &str, author: &str) -> Builder
Start building an object of kind authored by author.
§Example
use publet_core::{Object, cbor::Value};
let bytes = Object::builder("claim.prose", AUTHOR)
.created("2026-10-05T00:00:00Z")
.field("content", Value::Text("Water boils at 100 °C at sea level.".into()))
.build()?;
assert_eq!(Object::parse(&bytes)?.peek().kind(), "claim.prose");Sourcepub fn created(&self) -> &str
pub fn created(&self) -> &str
The author’s claimed creation instant.
Section 4.3: this is an unverified claim. Use a timestamp attestation where correctness depends on time.
§Example
assert_eq!(object.created(), "2026-10-05T00:00:00Z");Sourcepub fn body(&self) -> &BTreeMap<String, Value>
pub fn body(&self) -> &BTreeMap<String, Value>
The type-specific payload.
§Example
assert_eq!(
object.body().get("content"),
Some(&Value::Text("Water boils at 100 °C at sea level.".into())),
);Sourcepub fn prev(&self) -> Option<&Cid>
pub fn prev(&self) -> Option<&Cid>
The author’s previous object, if this one names it.
§Example
// This object names no previous object.
assert_eq!(object.prev(), None);Sourcepub fn basis(&self) -> Option<&Cid>
pub fn basis(&self) -> Option<&Cid>
The state the author was looking at, if this one names it.
Section 4.6. Advisory and never gating: nothing is overwritten (R1), so a stale basis cannot clobber anything. What it buys is that the interval between what an author read and what was true when their work landed is recorded rather than reconstructed.
§Example
// This object names no basis.
assert_eq!(object.basis(), None);