#![forbid(unsafe_code, missing_debug_implementations, missing_docs)]
#![cfg_attr(test, deny(warnings))]
extern crate failure;
extern crate mime_guess;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use failure::Error;
mod direction;
mod display_mode;
mod icon;
mod orientation;
mod related;
pub use direction::Direction;
pub use display_mode::DisplayMode;
pub use icon::Icon;
pub use orientation::Orientation;
pub use related::Related;
pub const MIME_TYPE_STR: &str = "application/manifest+json";
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Manifest<'s, 'i, 'r> {
name: &'s str,
#[serde(skip_serializing_if = "Option::is_none")]
short_name: Option<&'s str>,
#[serde(skip_serializing_if = "Option::is_none")]
start_url: Option<&'s str>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "display")]
display_mode: Option<DisplayMode>,
#[serde(skip_serializing_if = "Option::is_none")]
background_color: Option<&'s str>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<&'s str>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "dir")]
direction: Option<Direction>,
#[serde(skip_serializing_if = "Option::is_none")]
orientation: Option<Orientation>,
#[serde(skip_serializing_if = "Option::is_none")]
lang: Option<&'s str>,
#[serde(skip_serializing_if = "Option::is_none")]
scope: Option<&'s str>,
#[serde(skip_serializing_if = "Option::is_none")]
theme_color: Option<&'s str>,
#[serde(skip_serializing_if = "Option::is_none")]
prefer_related_applications: Option<bool>,
#[serde(borrow)]
icons: Vec<Icon<'i>>,
#[serde(borrow)]
related_applications: Vec<Related<'r>>,
}
impl<'s, 'i, 'r> Manifest<'s, 'i, 'r> {
#[must_use]
#[inline]
pub fn builder(name: &'s str) -> Self {
Self {
name,
short_name: None,
description: None,
start_url: None,
display_mode: None,
orientation: None,
direction: None,
lang: None,
background_color: None,
theme_color: None,
scope: None,
prefer_related_applications: None,
icons: vec![],
related_applications: vec![],
}
}
#[must_use]
#[inline]
pub fn build(self) -> Result<String, Error> {
let manifest = serde_json::to_string(&self)?;
Ok(manifest)
}
#[must_use]
#[inline]
pub fn pretty(self) -> Result<String, Error> {
let manifest = serde_json::to_string_pretty(&self)?;
Ok(manifest)
}
#[must_use]
#[inline]
pub fn short_name(mut self, name: &'s str) -> Self {
debug_assert!(name.len() <= 12);
self.short_name = Some(name);
self
}
#[must_use]
#[inline]
pub fn start_url(mut self, url: &'s str) -> Self {
self.start_url = Some(url);
self
}
#[must_use]
#[inline]
pub fn display_mode(mut self, mode: DisplayMode) -> Self {
self.display_mode = Some(mode);
self
}
#[must_use]
#[inline]
pub fn bg_color(mut self, color: &'s str) -> Self {
self.background_color = Some(color);
self
}
#[must_use]
#[inline]
pub fn theme_color(mut self, color: &'s str) -> Self {
self.theme_color = Some(color);
self
}
#[must_use]
#[inline]
pub fn description(mut self, desc: &'s str) -> Self {
self.description = Some(desc);
self
}
#[must_use]
#[inline]
pub fn lang(mut self, lang: &'s str) -> Self {
self.lang = Some(lang);
self
}
#[must_use]
#[inline]
pub fn orientation(mut self, orientation: Orientation) -> Self {
self.orientation = Some(orientation);
self
}
#[must_use]
#[inline]
pub fn direction(mut self, dir: Direction) -> Self {
self.direction = Some(dir);
self
}
#[must_use]
#[inline]
pub fn prefer_related_applications(mut self, prefer: bool) -> Self {
self.prefer_related_applications = Some(prefer);
self
}
#[must_use]
#[inline]
pub fn scope(mut self, scope: &'s str) -> Self {
self.scope = Some(scope);
self
}
#[must_use]
#[inline]
pub fn icon(mut self, icon: &'i Icon) -> Self {
self.icons.push(icon.clone());
self
}
#[must_use]
#[inline]
pub fn related(mut self, related: &'r Related) -> Self {
self.related_applications.push(related.clone());
self
}
}