1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
use crate::{Band, DataType, Extensions, Fields, Statistics};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::HashMap;
/// An Asset is an object that contains a URI to data associated with the [Item](crate::Item) that can be downloaded or streamed.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct Asset {
/// URI to the asset object.
///
/// Relative and absolute URIs are both allowed.
pub href: String,
/// The displayed title for clients and users.
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
/// A description of the Asset providing additional details, such as how it was processed or created.
///
/// [CommonMark 0.29](http://commonmark.org/) syntax MAY be used for rich text representation.
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// [Media type](crate::media_type) of the asset.
///
/// See the [common media types](https://github.com/radiantearth/stac-spec/blob/master/best-practices.md#common-media-types-in-stac) in the best practice doc for commonly used asset types.
#[serde(skip_serializing_if = "Option::is_none")]
pub r#type: Option<String>,
/// The semantic roles of the asset, similar to the use of rel in [Links](crate::Link).
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(default)]
pub roles: Vec<String>,
/// Creation date and time of the corresponding data, in UTC.
///
/// This identifies the creation time of the data.
///
/// This is a [common
/// metadata](https://github.com/radiantearth/stac-spec/blob/master/item-spec/common-metadata.md)
/// field.
#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<String>,
/// Date and time the metadata was updated last, in UTC.
///
/// This identifies the updated time of the data.
///
/// This is a [common
/// metadata](https://github.com/radiantearth/stac-spec/blob/master/item-spec/common-metadata.md)
/// field.
#[serde(skip_serializing_if = "Option::is_none")]
pub updated: Option<String>,
/// An array of available bands where each object is a [Band].
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub bands: Vec<Band>,
/// Value used to identify no-data.
///
/// The extension specifies that this can be a number or a string, but we
/// just use a f64 with a custom (de)serializer.
///
/// TODO write custom (de)serializer.
#[serde(skip_serializing_if = "Option::is_none")]
pub nodata: Option<f64>,
/// The data type of the values.
#[serde(skip_serializing_if = "Option::is_none")]
pub data_type: Option<DataType>,
/// Statistics of all the values.
#[serde(skip_serializing_if = "Option::is_none")]
pub statistics: Option<Statistics>,
/// Unit of measurement of the value.
#[serde(skip_serializing_if = "Option::is_none")]
pub unit: Option<String>,
/// Additional fields on the asset.
#[serde(flatten)]
pub additional_fields: Map<String, Value>,
#[serde(skip)]
extensions: Vec<String>,
}
/// Trait implemented by anything that has assets.
///
/// As of STAC v1.0.0, this is [Collection](crate::Collection) and [Item](crate::Item).
pub trait Assets {
/// Returns a reference to this object's assets.
///
/// # Examples
///
/// [Item](crate::Item) has assets:
///
/// ```
/// use stac::{Item, Assets};
/// let item: Item = stac::read("data/simple-item.json").unwrap();
/// assert!(!item.assets().is_empty());
/// ```
fn assets(&self) -> &HashMap<String, Asset>;
/// Returns a mut reference to this object's assets.
///
/// # Examples
///
/// [Item](crate::Item) has assets:
///
/// ```
/// use stac::{Item, Asset, Assets};
/// let mut item: Item = stac::read("data/simple-item.json").unwrap();
/// item.assets_mut().insert("foo".to_string(), Asset::new("./asset.tif"));
/// ```
fn assets_mut(&mut self) -> &mut HashMap<String, Asset>;
}
impl Asset {
/// Creates a new asset with the provided href.
///
/// # Examples
///
/// ```
/// # use stac::Asset;
/// let asset = Asset::new("an-href");
/// assert_eq!(asset.href, "an-href");
/// ```
pub fn new(href: impl ToString) -> Asset {
Asset {
href: href.to_string(),
title: None,
description: None,
r#type: None,
roles: Vec::new(),
created: None,
updated: None,
bands: Vec::new(),
data_type: None,
nodata: None,
statistics: None,
unit: None,
additional_fields: Map::new(),
extensions: Vec::new(),
}
}
/// Adds a role to this asset, returning the modified asset.
///
/// Useful for builder patterns.
///
/// # Examples
///
/// ```
/// use stac::Asset;
/// let asset = Asset::new("asset/dataset.tif").role("data");
/// assert_eq!(asset.roles, vec!["data"]);
/// ```
pub fn role(mut self, role: impl ToString) -> Asset {
self.roles.push(role.to_string());
self.roles.dedup();
self
}
}
impl Fields for Asset {
fn fields(&self) -> &Map<String, Value> {
&self.additional_fields
}
fn fields_mut(&mut self) -> &mut Map<String, Value> {
&mut self.additional_fields
}
}
impl Extensions for Asset {
fn extensions(&self) -> &Vec<String> {
&self.extensions
}
fn extensions_mut(&mut self) -> &mut Vec<String> {
&mut self.extensions
}
}
impl From<String> for Asset {
fn from(value: String) -> Self {
Asset::new(value)
}
}
impl<'a> From<&'a str> for Asset {
fn from(value: &'a str) -> Self {
Asset::new(value)
}
}
#[cfg(test)]
mod tests {
use super::Asset;
#[test]
fn new() {
let asset = Asset::new("an-href");
assert_eq!(asset.href, "an-href");
assert!(asset.title.is_none());
assert!(asset.description.is_none());
assert!(asset.r#type.is_none());
assert!(asset.roles.is_empty());
}
#[test]
fn skip_serializing() {
let asset = Asset::new("an-href");
let value = serde_json::to_value(asset).unwrap();
assert!(value.get("title").is_none());
assert!(value.get("description").is_none());
assert!(value.get("type").is_none());
assert!(value.get("roles").is_none());
}
}