Struct wnfs_common::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 libipld::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 libipld::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 libipld::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())));

Trait Implementations§

source§

impl Clone for Metadata

source§

fn clone(&self) -> Metadata

Returns a copy 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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method 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> 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>

§

impl<T> References<RawCodec> for T

§

fn references<R, E>(_c: RawCodec, _r: &mut R, _set: &mut E) -> Result<(), Error>
where R: Read, E: Extend<Cid<64>>,

Scrape the references from an impl Read. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

source§

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

§

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>,

§

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>,

§

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>,