jsonfg 0.1.0

Types for OGC Features and Geometries JSON (JSON-FG), a GeoJSON superset with support for arbitrary coordinate reference systems, solids, and curved geometries.
Documentation
//! The temporal `time` member.

use serde::{Deserialize, Serialize};

/// The marker used for an unbounded end of a time [`Time::interval`].
pub const UNBOUNDED: &str = "..";

/// The value of a feature's `time` member: a temporal instant (as a `date` and/or a
/// `timestamp`) and/or an `interval`. At least one member must be present.
///
/// Instant and interval ends are kept as strings in their JSON-FG lexical form: a `date`
/// is `YYYY-MM-DD`, a `timestamp` is an RFC 3339 UTC date-time (`…Z`), and an interval
/// end is a date, a timestamp, or [`UNBOUNDED`] (`".."`).
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct Time {
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub date: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub timestamp: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub interval: Option<[String; 2]>,
    /// Any additional members, preserved verbatim (e.g. a producer-specific temporal
    /// member not defined by JSON-FG 1.0).
    #[serde(flatten)]
    pub foreign_members: crate::JsonObject,
}

impl Time {
    /// A temporal instant given as a calendar date (`YYYY-MM-DD`).
    pub fn date(date: impl Into<String>) -> Self {
        Time {
            date: Some(date.into()),
            ..Default::default()
        }
    }

    /// A temporal instant given as an RFC 3339 UTC timestamp.
    pub fn timestamp(timestamp: impl Into<String>) -> Self {
        Time {
            timestamp: Some(timestamp.into()),
            ..Default::default()
        }
    }

    /// A time interval. Use [`UNBOUNDED`] for an open start or end.
    pub fn interval(start: impl Into<String>, end: impl Into<String>) -> Self {
        Time {
            interval: Some([start.into(), end.into()]),
            ..Default::default()
        }
    }
}