use crate::{
Calendar, CalendarKind, Context, CryptoKey, Directory, DirectoryKind, Link, LinkKind, Media,
MediaKind,
};
#[cfg(feature = "typed")]
use crate::{CalendarType, CryptoKeyType, DirectoryType, LinkType, MediaType};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct Resource {
#[cfg(feature = "typed")]
#[serde(rename = "@type")]
resource_type: Option<ResourceType>,
pub kind: Option<String>,
pub uri: String,
pub media_type: Option<String>,
pub contexts: Option<HashMap<Context, bool>>,
pub pref: Option<u64>,
pub label: Option<String>,
}
impl Resource {
pub fn new(uri: String) -> Self {
Self {
#[cfg(feature = "typed")]
resource_type: Some(ResourceType::Resource),
kind: None,
uri,
media_type: None,
contexts: None,
pref: None,
label: None,
}
}
}
#[cfg(feature = "typed")]
#[derive(Serialize, Deserialize, Debug)]
enum ResourceType {
Resource,
}
impl From<Resource> for Calendar {
fn from(resource: Resource) -> Self {
let kind: Option<CalendarKind> = resource.kind.as_deref().map(|s| s.to_string().into());
Self {
#[cfg(feature = "typed")]
calendar_type: Some(CalendarType::Calendar),
kind,
uri: resource.uri,
media_type: resource.media_type,
contexts: resource.contexts,
pref: resource.pref,
label: resource.label,
}
}
}
impl From<Resource> for CryptoKey {
fn from(resource: Resource) -> Self {
Self {
#[cfg(feature = "typed")]
crypto_key_type: Some(CryptoKeyType::CryptoKey),
kind: resource.kind,
uri: resource.uri,
media_type: resource.media_type,
contexts: resource.contexts,
pref: resource.pref,
label: resource.label,
}
}
}
impl From<Resource> for Directory {
fn from(resource: Resource) -> Self {
let kind: Option<DirectoryKind> = resource.kind.as_deref().map(|s| s.to_string().into());
Self {
#[cfg(feature = "typed")]
directory_type: Some(DirectoryType::Directory),
kind,
uri: resource.uri,
media_type: resource.media_type,
contexts: resource.contexts,
pref: resource.pref,
label: resource.label,
list_as: None,
}
}
}
impl From<Resource> for Media {
fn from(resource: Resource) -> Self {
let kind: MediaKind = match resource.kind {
Some(kind) => kind.into(),
None => MediaKind::default(),
};
Self {
#[cfg(feature = "typed")]
media_hidden_type: Some(MediaType::Media),
kind,
uri: resource.uri,
media_type: resource.media_type,
contexts: resource.contexts,
pref: resource.pref,
label: resource.label,
}
}
}
impl From<Resource> for Link {
fn from(resource: Resource) -> Self {
let kind: Option<LinkKind> = resource.kind.as_deref().map(|s| s.to_string().into());
Self {
#[cfg(feature = "typed")]
link_type: Some(LinkType::Link),
kind,
uri: resource.uri,
media_type: resource.media_type,
contexts: resource.contexts,
pref: resource.pref,
label: resource.label,
}
}
}