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
impl Metadata
sourcepub fn new(time: DateTime<Utc>) -> Self
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);
sourcepub fn upsert_mtime(&mut self, time: DateTime<Utc>)
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);
sourcepub fn get_created(&self) -> Option<DateTime<Utc>>
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.
sourcepub fn get_modified(&self) -> Option<DateTime<Utc>>
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.
sourcepub fn put(&mut self, key: &str, value: Ipld) -> Option<Ipld>
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.
sourcepub fn put_serializable(
&mut self,
key: &str,
value: impl Serialize
) -> Result<Option<Ipld>>
pub fn put_serializable( &mut self, key: &str, value: impl Serialize ) -> Result<Option<Ipld>>
Serializes and inserts given value at given key in metadata.
sourcepub fn get_deserializable<D: DeserializeOwned>(
&self,
key: &str
) -> Option<Result<D>>
pub fn get_deserializable<D: DeserializeOwned>( &self, key: &str ) -> Option<Result<D>>
Returns deserialized metadata value behind given key.
sourcepub fn delete(&mut self, key: &str) -> Option<Ipld>
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.
sourcepub fn update(&mut self, other: &Self)
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())));