Metadata

Struct Metadata 

Source
pub struct Metadata(pub BTreeMap<String, Ipld>);
Expand description

The metadata of a node in the WNFS file system.

§Examples

use wnfs_common::Metadata;
use chrono::Utc;

let metadata = Metadata::new(Utc::now());

println!("{:?}", metadata);

Tuple Fields§

§0: BTreeMap<String, Ipld>

Implementations§

Source§

impl Metadata

Source

pub fn new(time: DateTime<Utc>) -> Self

Creates a new metadata.

§Examples
use wnfs_common::Metadata;
use chrono::Utc;

let metadata = Metadata::new(Utc::now());

println!("{:?}", metadata);
Source

pub fn upsert_mtime(&mut self, time: DateTime<Utc>)

Updates modified time.

§Examples
use wnfs_common::Metadata;
use chrono::{Utc, TimeZone, Duration};

let mut metadata = Metadata::new(Utc::now());
let time = Utc::now() + Duration::days(1);

metadata.upsert_mtime(time);

let imprecise_time = Utc.timestamp_opt(time.timestamp(), 0).single();
assert_eq!(metadata.get_modified(), imprecise_time);
Source

pub fn get_created(&self) -> Option<DateTime<Utc>>

Returns the created time.

§Examples
use wnfs_common::Metadata;
use chrono::{Utc, TimeZone};

let time = Utc::now();
let metadata = Metadata::new(time);

let imprecise_time = Utc.timestamp_opt(time.timestamp(), 0).single();
assert_eq!(metadata.get_created(), imprecise_time);

Will return None if there’s no created metadata on the node or if it’s not a second-based POSIX timestamp integer.

Source

pub fn get_modified(&self) -> Option<DateTime<Utc>>

Returns the modified time.

§Examples
use wnfs_common::Metadata;
use chrono::{Utc, TimeZone};

let time = Utc::now();
let metadata = Metadata::new(time);

let imprecise_time = Utc.timestamp_opt(time.timestamp(), 0).single();
assert_eq!(metadata.get_modified(), imprecise_time);

Will return None if there’s no created metadata on the node or if it’s not a second-based POSIX timestamp integer.

Source

pub fn put(&mut self, key: &str, value: Ipld) -> Option<Ipld>

Inserts a key-value pair into the metadata. If the key already existed, the value is updated, and the old value is returned.

§Examples
use wnfs_common::Metadata;
use chrono::Utc;
use ipld_core::ipld::Ipld;

let mut metadata = Metadata::new(Utc::now());
metadata.put("foo", Ipld::String("bar".into()));
assert_eq!(metadata.0.get("foo"), Some(&Ipld::String("bar".into())));
metadata.put("foo", Ipld::String("baz".into()));
assert_eq!(metadata.0.get("foo"), Some(&Ipld::String("baz".into())));

Returns (self, old_value), where old_value is None if the key did not exist prior to this call.

Source

pub fn get(&self, key: &str) -> Option<&Ipld>

Returns metadata value behind given key.

Source

pub fn put_serializable( &mut self, key: &str, value: impl Serialize, ) -> Result<Option<Ipld>>

Serializes and inserts given value at given key in metadata.

Source

pub fn get_deserializable<D: DeserializeOwned>( &self, key: &str, ) -> Option<Result<D>>

Returns deserialized metadata value behind given key.

Source

pub fn delete(&mut self, key: &str) -> Option<Ipld>

Deletes a key from the metadata.

§Examples
use wnfs_common::Metadata;
use chrono::Utc;
use ipld_core::ipld::Ipld;

let mut metadata = Metadata::new(Utc::now());
metadata.put("foo", Ipld::String("bar".into()));
assert_eq!(metadata.0.get("foo"), Some(&Ipld::String("bar".into())));
metadata.delete("foo");
assert_eq!(metadata.0.get("foo"), None);

Returns Some<Ipld> if the key existed prior to this call, otherwise None.

Source

pub fn update(&mut self, other: &Self)

Updates this metadata with the contents of another metadata. merge strategy is to take theirs.

§Examples
use wnfs_common::Metadata;
use chrono::Utc;
use ipld_core::ipld::Ipld;

let mut metadata1 = Metadata::new(Utc::now());
metadata1.put("foo", Ipld::String("bar".into()));
let mut metadata2 = Metadata::new(Utc::now());
metadata2.put("foo", Ipld::String("baz".into()));
metadata1.update(&metadata2);
assert_eq!(metadata1.0.get("foo"), Some(&Ipld::String("baz".into())));
Source

pub fn tie_break_with(&mut self, other: &Self) -> Result<()>

Tie break this node with another one. Used for conflict reconciliation. We don’t merge the two metadata maps together (yet), instead we compare their hashes. The one with the lower hash survives.

Trait Implementations§

Source§

impl Clone for Metadata

Source§

fn clone(&self) -> Metadata

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Metadata

Source§

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

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

impl<'de> Deserialize<'de> for Metadata

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Metadata

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Metadata

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Metadata

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> LoadIpld for T

Source§

fn decode_ipld(cid: &Cid<64>, bytes: Bytes) -> Result<T, Error>

Source§

impl<T> StoreIpld for T
where T: Serialize,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<S> CondSend for S
where S: Send,

Source§

impl<S> CondSync for S
where S: Send + Sync,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,