Skip to main content

Object

Struct Object 

Source
pub struct Object { /* private fields */ }
Expand description

A parsed object: its header, its body, and the bytes it came from.

Implementations§

Source§

impl Object

Source

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 { .. })));
Source

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");
Source

pub fn protocol(&self) -> &str

The protocol major version.

§Example
assert_eq!(object.protocol(), publet_core::PROTOCOL_VERSION);
Source

pub fn kind(&self) -> &str

The object type.

§Example
assert_eq!(object.kind(), "claim.prose");
Source

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");
Source

pub fn author(&self) -> &Cid

The identifier of the signing key object.

§Example
assert_eq!(object.author().to_string(), AUTHOR);
Source

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())),
);
Source

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);
Source

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);
Source

pub fn ext(&self) -> Option<&BTreeMap<String, Value>>

Extension fields, if present.

§Example
// This object carries no extension fields.
assert_eq!(object.ext(), None);
Source

pub fn bytes(&self) -> &[u8] ⓘ

The canonical bytes this object was parsed from.

§Example
// The bytes are what the identifier is computed from.
assert_eq!(object.bytes(), bytes.as_slice());
assert!(Cid::of(&bytes, HashAlg::Sha2_256).verifies(object.bytes()));

Trait Implementations§

Source§

impl Clone for Object

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Object

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Object

Source§

impl PartialEq for Object

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Object

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.