Fields

Trait Fields 

Source
pub trait Fields {
    // Required methods
    fn fields(&self) -> &Map<String, Value>;
    fn fields_mut(&mut self) -> &mut Map<String, Value>;

    // Provided methods
    fn field(&self, key: &str) -> Option<&Value> { ... }
    fn set_field<S: Serialize>(
        &mut self,
        key: impl ToString,
        value: S,
    ) -> Result<Option<Value>> { ... }
    fn fields_with_prefix<D: DeserializeOwned>(&self, prefix: &str) -> Result<D> { ... }
    fn set_fields_with_prefix<S: Serialize>(
        &mut self,
        prefix: &str,
        value: S,
    ) -> Result<()> { ... }
    fn remove_fields_with_prefix(&mut self, prefix: &str) { ... }
}
Expand description

Trait for structures that have gettable and settable fields.

§Examples

use stac::{Catalog, Item, Fields};

let mut catalog = Catalog::new("an-id", "a description");
catalog.set_field("foo", "bar");
assert_eq!(catalog.additional_fields.get("foo").unwrap(), "bar");

let mut item = Item::new("an-id");
item.set_field("foo", "bar");
assert_eq!(item.properties.additional_fields.get("foo").unwrap(), "bar");
assert!(item.additional_fields.is_empty());

Required Methods§

Source

fn fields(&self) -> &Map<String, Value>

Gets the fields value.

§Examples
use stac::{Item, Fields};
let item = Item::new("an-id");
assert!(item.fields().is_empty());
Source

fn fields_mut(&mut self) -> &mut Map<String, Value>

Gets a mutable reference to the fields value.

§Examples
use stac::{Item, Fields};
let mut item = Item::new("an-id");
item.fields_mut().insert("foo".to_string(), "bar".into());

Provided Methods§

Source

fn field(&self, key: &str) -> Option<&Value>

Gets the value of a field.

§Examples
use stac::{Item, Fields};
let mut item = Item::new("an-id");
item.set_field("foo", "bar").unwrap();
assert_eq!(item.properties.additional_fields.get("foo"), item.field("foo"));
Source

fn set_field<S: Serialize>( &mut self, key: impl ToString, value: S, ) -> Result<Option<Value>>

Sets the value of a field.

§Examples
use stac::{Item, Fields};
let mut item = Item::new("an-id");
item.set_field("foo", "bar").unwrap();
assert_eq!(item.properties.additional_fields["foo"], "bar");
Source

fn fields_with_prefix<D: DeserializeOwned>(&self, prefix: &str) -> Result<D>

Gets values with a prefix.

§Examples
use stac::{Fields, Item};
use serde_json::Value;

let item: Item = stac::read("examples/extensions-collection/proj-example/proj-example.json").unwrap();
let projection: Value = item.fields_with_prefix("proj").unwrap();
Source

fn set_fields_with_prefix<S: Serialize>( &mut self, prefix: &str, value: S, ) -> Result<()>

Sets values with a prefix.

§Examples
use stac::{Fields, Item};
use serde_json::json;

let projection = json!({ "code": "EPSG:4326" });
let mut item = Item::new("an-id");
item.set_fields_with_prefix("proj", projection);
Source

fn remove_fields_with_prefix(&mut self, prefix: &str)

Removes values with a prefix.

§Examples
use stac::{Fields, Item};

let mut item: Item = stac::read("examples/extensions-collection/proj-example/proj-example.json").unwrap();
item.remove_fields_with_prefix("proj");

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§