use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Document {
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<PrimaryData>,
#[serde(skip_serializing_if = "Option::is_none")]
pub errors: Option<Vec<ErrorObject>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub included: Option<Vec<ResourceObject>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<Links>,
#[serde(skip_serializing_if = "Option::is_none")]
pub meta: Option<BTreeMap<String, serde_json::Value>>,
}
impl Document {
pub fn single(resource: ResourceObject) -> Self {
Self {
data: Some(PrimaryData::Single(Box::new(resource))),
..Self::empty()
}
}
pub fn collection(resources: Vec<ResourceObject>) -> Self {
Self {
data: Some(PrimaryData::Collection(resources)),
..Self::empty()
}
}
pub fn errors(errors: Vec<ErrorObject>) -> Self {
Self {
errors: Some(errors),
..Self::empty()
}
}
pub fn with_links(mut self, links: Links) -> Self {
self.links = Some(links);
self
}
pub fn with_included(mut self, included: Vec<ResourceObject>) -> Self {
self.included = Some(included);
self
}
#[must_use]
pub fn with_meta(
mut self,
key: impl Into<String>,
value: impl Into<serde_json::Value>,
) -> Self {
self.meta
.get_or_insert_with(BTreeMap::new)
.insert(key.into(), value.into());
self
}
fn empty() -> Self {
Self {
data: None,
errors: None,
included: None,
links: None,
meta: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PrimaryData {
Single(Box<ResourceObject>),
Collection(Vec<ResourceObject>),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ResourceObject {
#[serde(rename = "type")]
pub kind: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "BTreeMap::is_empty", default)]
pub attributes: BTreeMap<String, serde_json::Value>,
#[serde(skip_serializing_if = "BTreeMap::is_empty", default)]
pub relationships: BTreeMap<String, Relationship>,
#[serde(skip_serializing_if = "Option::is_none")]
pub meta: Option<BTreeMap<String, serde_json::Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<Links>,
}
impl ResourceObject {
pub fn new(kind: impl Into<String>, id: impl Into<String>) -> Self {
Self {
kind: kind.into(),
id: Some(id.into()),
attributes: BTreeMap::new(),
relationships: BTreeMap::new(),
meta: None,
links: None,
}
}
pub fn attr(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
self.attributes.insert(key.into(), value.into());
self
}
pub fn relationship(mut self, name: impl Into<String>, rel: Relationship) -> Self {
self.relationships.insert(name.into(), rel);
self
}
pub fn with_links(mut self, links: Links) -> Self {
self.links = Some(links);
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Relationship {
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Linkage>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<Links>,
}
impl Relationship {
pub fn to_one(identifier: ResourceIdentifier) -> Self {
Self {
data: Some(Linkage::One(Some(identifier))),
links: None,
}
}
pub fn to_many(identifiers: Vec<ResourceIdentifier>) -> Self {
Self {
data: Some(Linkage::Many(identifiers)),
links: None,
}
}
pub fn empty_to_one() -> Self {
Self {
data: Some(Linkage::One(None)),
links: None,
}
}
pub fn with_links(mut self, links: Links) -> Self {
self.links = Some(links);
self
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Linkage {
One(Option<ResourceIdentifier>),
Many(Vec<ResourceIdentifier>),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ResourceIdentifier {
#[serde(rename = "type")]
pub kind: String,
pub id: String,
}
impl ResourceIdentifier {
pub fn new(kind: impl Into<String>, id: impl Into<String>) -> Self {
Self {
kind: kind.into(),
id: id.into(),
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Links {
#[serde(rename = "self", skip_serializing_if = "Option::is_none")]
pub this: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub related: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub first: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub prev: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub next: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last: Option<String>,
}
impl Links {
pub fn this(url: impl Into<String>) -> Self {
Self {
this: Some(url.into()),
..Self::default()
}
}
pub fn related(mut self, url: impl Into<String>) -> Self {
self.related = Some(url.into());
self
}
pub fn first(mut self, url: impl Into<String>) -> Self {
self.first = Some(url.into());
self
}
pub fn next(mut self, url: impl Into<String>) -> Self {
self.next = Some(url.into());
self
}
pub fn prev(mut self, url: impl Into<String>) -> Self {
self.prev = Some(url.into());
self
}
pub fn last(mut self, url: impl Into<String>) -> Self {
self.last = Some(url.into());
self
}
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct ErrorObject {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
pub status: String,
pub code: String,
pub title: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<ErrorSource>,
#[serde(skip_serializing_if = "Option::is_none")]
pub meta: Option<BTreeMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct ErrorSource {
#[serde(skip_serializing_if = "Option::is_none")]
pub pointer: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub parameter: Option<String>,
}
impl ErrorSource {
pub fn pointer(p: impl Into<String>) -> Self {
Self {
pointer: Some(p.into()),
parameter: None,
}
}
pub fn parameter(p: impl Into<String>) -> Self {
Self {
pointer: None,
parameter: Some(p.into()),
}
}
}
pub const MEDIA_TYPE: &str = "application/vnd.api+json";