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 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.
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 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.
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 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())));Sourcepub fn tie_break_with(&mut self, other: &Self) -> Result<()>
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.