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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#![cfg_attr(feature = "nightly", deny(missing_docs))]
#![cfg_attr(feature = "nightly", feature(external_doc))]
#![cfg_attr(feature = "nightly", doc(include = "../README.md"))]
#![cfg_attr(test, deny(warnings))]
#![forbid(unsafe_code, missing_debug_implementations)]

extern crate failure;
extern crate mime_guess;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;

use failure::Error;
use std::ffi::OsStr;
use std::path::Path;

/// Defines the developers’ preferred display mode for the website.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DisplayMode {
  /// All of the available display area is used and no user agent chrome is
  /// shown.
  #[serde(rename = "full-screen")]
  FullScreen,
  /// The application will look and feel like a standalone application. This can
  /// include the application having a different window, its own icon in the
  /// application launcher, etc. In this mode, the user agent will exclude UI
  /// elements for controlling navigation, but can include other UI elements
  /// such as a status bar.
  #[serde(rename = "standalone")]
  Standalone,
  /// The application will look and feel like a standalone application, but will
  /// have a minimal set of UI elements for controlling navigation. The elements
  /// will vary by browser.
  #[serde(rename = "minimal-ui")]
  MinimalUi,
  /// The application opens in a conventional browser tab or new window,
  /// depending on the browser and platform. This is the default.
  #[serde(rename = "browser")]
  Browser,
}

/// Add an icon to the web manifest.
///
/// ## Example Output
/// ```json
/// "icons": [{
///   "src": "images/touch/homescreen48.png",
///   "sizes": "48x48",
///   "type": "image/png"
/// }, {
///   "src": "images/touch/homescreen72.png",
///   "sizes": "72x72",
///   "type": "image/png"
/// }, {
///   "src": "images/touch/homescreen96.png",
///   "sizes": "96x96",
///   "type": "image/png"
/// }, {
///   "src": "images/touch/homescreen144.png",
///   "sizes": "144x144",
///   "type": "image/png"
/// }, {
///   "src": "images/touch/homescreen168.png",
///   "sizes": "168x168",
///   "type": "image/png"
/// }, {
///   "src": "images/touch/homescreen192.png",
///   "sizes": "192x192",
///   "type": "image/png"
/// }],
/// ```
#[derive(Debug, Clone, Serialize)]
pub struct Icon<'s> {
  src: &'s str,
  sizes: &'s str,
  #[serde(rename = "type")]
  icon_type: String,
}

impl<'s> Icon<'s> {
  /// Create a new `Icon` instance.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # use webmanifest::Icon;
  /// let src = "images/touch/homescreen48.png";
  /// let icon = Icon::new(&src, "48x48");
  /// ```
  #[must_use]
  #[inline]
  pub fn new(src: &'s str, sizes: &'s str) -> Self {
    let ext = Path::new(src)
      .extension()
      .and_then(OsStr::to_str)
      .unwrap_or("");
    let mime_type = mime_guess::get_mime_type(&ext);
    let icon_type = format!("{}/{}", mime_type.type_(), mime_type.subtype());
    Self {
      src,
      sizes,
      icon_type,
    }
  }
}

/// Create a new manifest builder.
#[derive(Debug, Clone, Serialize)]
pub struct Manifest<'s, 'i, 'r> {
  name: &'s str,
  short_name: Option<&'s str>,
  start_url: Option<&'s str>,
  #[serde(rename = "display")]
  display_mode: Option<DisplayMode>,
  background_color: Option<&'s str>,
  description: Option<&'s str>,
  icons: Vec<&'i Icon<'i>>,
  related_applications: Vec<&'r Related<'r>>,
}

impl<'s, 'i, 'r> Manifest<'s, 'i, 'r> {
  /// Create a new instance.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # use webmanifest::Manifest;
  /// let name = "My Cool Application";
  /// let builder = Manifest::builder(name);
  /// ```
  #[must_use]
  #[inline]
  pub fn builder(name: &'s str) -> Self {
    Self {
      name,
      short_name: None,
      description: None,
      start_url: None,
      display_mode: None,
      background_color: None,
      icons: vec![],
      related_applications: vec![],
    }
  }

  /// Finalize the builder and create the manifest.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # extern crate failure;
  /// # use webmanifest::Manifest;
  /// # fn main() -> Result<(), failure::Error> {
  /// let name = "My Cool Application";
  /// let manifest = Manifest::builder(name).build()?;
  /// # Ok(())}
  /// ```
  #[must_use]
  #[inline]
  pub fn build(self) -> Result<String, Error> {
    let manifest = serde_json::to_string(&self)?;
    Ok(manifest)
  }

  /// Finalize the builder and create a pretty representation of the manifest.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # extern crate failure;
  /// # use webmanifest::Manifest;
  /// # fn main() -> Result<(), failure::Error> {
  /// let name = "My Cool Application";
  /// let manifest = Manifest::builder(name).pretty()?;
  /// # Ok(())}
  /// ```
  #[must_use]
  #[inline]
  pub fn pretty(self) -> Result<String, Error> {
    let manifest = serde_json::to_string_pretty(&self)?;
    Ok(manifest)
  }

  /// Set the `short_name` value.
  ///
  /// ## Panics
  /// This will panic if the short name exceeds 12 characters.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # extern crate failure;
  /// # use webmanifest::Manifest;
  /// # fn main() -> Result<(), failure::Error> {
  /// let name = "My Cool Application";
  /// let manifest = Manifest::builder(name)
  ///   .short_name("Cool App")
  ///   .build()?;
  /// # Ok(())}
  /// ```
  #[must_use]
  #[inline]
  pub fn short_name(mut self, val: &'s str) -> Self {
    debug_assert!(val.len() < 12);
    self.short_name = Some(val);
    self
  }

  /// Set the `start_url` value.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # extern crate failure;
  /// # use webmanifest::Manifest;
  /// # fn main() -> Result<(), failure::Error> {
  /// let name = "My Cool Application";
  /// let manifest = Manifest::builder(name)
  ///   .start_url(".")
  ///   .build()?;
  /// # Ok(())}
  /// ```
  #[must_use]
  #[inline]
  pub fn start_url(mut self, val: &'s str) -> Self {
    self.start_url = Some(val);
    self
  }

  /// Set the `display` value.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # extern crate failure;
  /// # use webmanifest::{Manifest, DisplayMode};
  /// # fn main() -> Result<(), failure::Error> {
  /// let name = "My Cool Application";
  /// let manifest = Manifest::builder(name)
  ///   .display_mode(DisplayMode::Standalone)
  ///   .build()?;
  /// # Ok(())}
  /// ```
  #[must_use]
  #[inline]
  pub fn display_mode(mut self, val: DisplayMode) -> Self {
    self.display_mode = Some(val);
    self
  }

  /// Set the `background_color` value.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # extern crate failure;
  /// # use webmanifest::Manifest;
  /// # fn main() -> Result<(), failure::Error> {
  /// let name = "My Cool Application";
  /// let manifest = Manifest::builder(name)
  ///   .background_color("#000")
  ///   .build()?;
  /// # Ok(())}
  /// ```
  #[must_use]
  #[inline]
  pub fn background_color(mut self, val: &'s str) -> Self {
    self.background_color = Some(val);
    self
  }

  /// Set the `description` value.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # extern crate failure;
  /// # use webmanifest::Manifest;
  /// # fn main() -> Result<(), failure::Error> {
  /// let name = "My Cool Application";
  /// let desc = "It does many things.";
  /// let manifest = Manifest::builder(name)
  ///   .description(desc)
  ///   .build()?;
  /// # Ok(())}
  /// ```
  #[must_use]
  #[inline]
  pub fn description(mut self, val: &'s str) -> Self {
    self.description = Some(val);
    self
  }

  /// Add an `Icon` to the icons vector.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # extern crate failure;
  /// # use webmanifest::{Manifest, Icon};
  /// # fn main() -> Result<(), failure::Error> {
  /// let name = "My Cool Application";
  /// let src = "images/touch/homescreen48.png";
  /// let manifest = Manifest::builder(name)
  ///   .icon(&Icon::new(&src, "48x48"))
  ///   .build()?;
  /// # Ok(())}
  /// ```
  #[must_use]
  #[inline]
  pub fn icon(mut self, val: &'i Icon) -> Self {
    self.icons.push(val);
    self
  }

  /// Add an `Related` application to the `related_applications` vector.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # extern crate failure;
  /// # use webmanifest::{Manifest, Related};
  /// # fn main() -> Result<(), failure::Error> {
  /// let name = "My Cool Application";
  ///
  /// let url = "https://play.google.com/store/apps/details?id=cheeaun.hackerweb";
  /// let manifest = Manifest::builder(name)
  ///   .related(&Related::new("play", url))
  ///   .build()?;
  /// # Ok(())}
  /// ```
  #[must_use]
  #[inline]
  pub fn related(mut self, val: &'r Related) -> Self {
    self.related_applications.push(val);
    self
  }
}

/// An entry in an array of native applications that are installable by, or
/// accessible to, the underlying platform.
///
/// For example, a native Android application obtainable through the Google Play
/// Store. Such applications are intended to be alternatives to the website that
/// provides similar/equivalent functionality — like the native app version of
/// the website.
///
/// ## Example Output
/// ```json
/// "related_applications": [{
///   "platform": "play",
///   "url": "https://play.google.com/store/apps/details?id=cheeaun.hackerweb"
/// }]
/// ```
#[derive(Debug, Clone, Serialize)]
pub struct Related<'s> {
  platform: &'s str,
  url: &'s str,
}

impl<'s> Related<'s> {
  /// Create a new `Related` instance.
  ///
  /// ## Example
  /// ```rust
  /// # extern crate webmanifest;
  /// # use webmanifest::Related;
  /// let platform = "play";
  /// let url = "https://play.google.com/store/apps/details?id=cheeaun.hackerweb";
  /// let related = Related::new(platform, url);
  /// ```
  #[inline]
  #[must_use]
  pub fn new(platform: &'s str, url: &'s str) -> Self {
    Self { platform, url }
  }
}