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))]
mod object_type;
pub use crate::object_type::ObjectType;
use failure::{ensure, Error, ResultExt};
use url::Url;
pub trait Object {
fn build(self) -> String;
}
#[inline]
pub fn create_url<'s>(content: impl Into<&'s str>) -> String {
create("og:url", content.into())
}
#[inline]
pub fn create_title<'s>(content: impl Into<&'s str>) -> String {
create("og:title", content.into())
}
#[inline]
pub fn create_description<'s>(content: impl Into<&'s str>) -> String {
create("og:description", content.into())
}
#[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()))
}
#[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()))
}
#[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()))
}
#[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()))
}
#[inline]
fn create(name: &str, content: &str) -> String {
format!(r#"<meta property="{}" content="{}" />"#, name, content)
}