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
#![forbid(unsafe_code, future_incompatible)]
#![deny(missing_debug_implementations, bad_style)]
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]

//! ## Example
//!
//! ```rust
//! ```

mod object_type;
pub use crate::object_type::ObjectType;

use failure::{ensure, Error, ResultExt};
use url::Url;

/// OpenGraph Object
pub trait Object {
  /// Convert the Object to a string.
  fn build(self) -> String;
}

/// The canonical URL for your page.
///
/// This should be the undecorated URL, without session variables, user
/// identifying parameters, or counters. Likes and Shares for this URL will
/// aggregate at this URL. For example, mobile domain URLs should point to the
/// desktop version of the URL as the canonical URL to aggregate Likes and
/// Shares across different versions of the page.
#[inline]
pub fn create_url<'s>(content: impl Into<&'s str>) -> String {
  create("og:url", content.into())
}

/// The title of your article without any branding such as your site name.
#[inline]
pub fn create_title<'s>(content: impl Into<&'s str>) -> String {
  create("og:title", content.into())
}

/// A brief description of the content, usually between 2 and 4 sentences.
#[inline]
pub fn create_description<'s>(content: impl Into<&'s str>) -> String {
  create("og:description", content.into())
}

/// The URL of the image that appears when someone shares the content.
#[inline]
pub fn create_image<'s>(content: impl Into<&'s str>) -> Result<String, Error> {
  let url = Url::parse(content.into()).context("String must be a valid URL")?;
  Ok(create("og:image", url.as_str()))
}

/// Equivalent to `og:image`.
#[inline]
pub fn create_image_url<'s>(
  content: impl Into<&'s str>,
) -> Result<String, Error> {
  let url = Url::parse(content.into()).context("String must be a valid URL")?;
  Ok(create("og:image:url", url.as_str()))
}

/// https:// URL for the image.
#[inline]
pub fn create_secure_image_url<'s>(
  content: impl Into<&'s str>,
) -> Result<String, Error> {
  let url = Url::parse(content.into()).context("String must be a valid URL")?;
  ensure!(
    url.scheme() == "https",
    "URL must start with https://. Use `.image_url()` instead."
  );
  Ok(create("og:image:secure_url", url.as_str()))
}

/// MIME type of the image. One of image/jpeg, image/gif or image/png
#[inline]
pub fn create_image_type<'s>(
  content: impl Into<&'s str>,
) -> Result<String, Error> {
  let url = Url::parse(content.into()).context("String must be a valid URL")?;
  Ok(create("og:image:url", url.as_str()))
}

/// Create an HTML Open Graph tag
#[inline]
fn create(name: &str, content: &str) -> String {
  format!(r#"<meta property="{}" content="{}" />"#, name, content)
}