twitter_card/
lib.rs

1#![forbid(unsafe_code, future_incompatible)]
2#![forbid(rust_2018_idioms, rust_2018_compatibility)]
3#![deny(missing_debug_implementations, bad_style)]
4#![deny(missing_docs)]
5#![cfg_attr(test, deny(warnings))]
6
7//! Generate HTML for Twitter Card integration.
8//!
9//! ## Example
10//! ```rust
11//! use twitter_card::{Summary, TwitterCard};
12//!
13//! let card = Summary::builder()
14//!   .site("@flickr")
15//!   .title("Small Island Developing States Photo Submission")
16//!   .desc("View the album on Flickr.")
17//!   .image("https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg")
18//!   .build();
19//! ```
20//!
21//! ```html
22//! <meta name="twitter:card" content="summary" />
23//! <meta name="twitter:site" content="@flickr" />
24//! <meta name="twitter:title" content="Small Island Developing States Photo Submission" />
25//! <meta name="twitter:description" content="View the album on Flickr." />
26//! <meta name="twitter:image" content="https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg" />
27//! ```
28
29mod app;
30mod player;
31mod summary;
32mod summary_large_image;
33
34pub use crate::app::App;
35pub use crate::player::Player;
36pub use crate::summary::Summary;
37pub use crate::summary_large_image::SummaryLargeImage;
38
39/// Twitter Card.
40pub trait TwitterCard {
41  /// Convert the Twitter Card to a string.
42  fn build(self) -> String;
43}
44
45/// Create a Twitter card tag.
46///
47/// The card type.
48///
49/// _Used with all cards._
50#[inline]
51pub fn create_card(content: &str) -> String {
52  create("twitter:card", content)
53}
54
55/// Create a Twitter site tag.
56///
57/// @username of website. Either twitter:site or twitter:site:id is required.
58///
59/// _Used with summary, summary_large_image, app, player cards._
60#[inline]
61pub fn create_site(content: &str) -> String {
62  create("twitter:site", content)
63}
64
65/// Create a Twitter site id tag.
66///
67/// Same as twitter:site, but the user’s Twitter ID. Either twitter:site or
68/// twitter:site:id is required.
69///
70/// _Used with summary, summary_large_image, player cards_
71#[inline]
72pub fn create_site_id(content: &str) -> String {
73  create("twitter:site:id", content)
74}
75
76/// Create a Twitter description tag.
77///
78/// Description of content.
79///
80/// _Used with summary, summary_large_image, player cards._
81///
82/// ## Panics.
83/// Panics if the description is more than 200 characters.
84#[inline]
85pub fn create_desc(content: &str) -> String {
86  debug_assert!(
87    content.len() <= 200,
88    "Description has a maximum of 200 characters"
89  );
90  create("twitter:description", content)
91}
92
93/// Create a Twitter title tag.
94///
95/// Title of content.
96///
97/// _Used with summary, summary_large_image, player cards._
98///
99/// ## Panics.
100/// Panics if the description is more than 70 characters.
101#[inline]
102pub fn create_title(content: &str) -> String {
103  debug_assert!(content.len() <= 70, "Title has a maximum of 70 characters");
104  create("twitter:title", content)
105}
106
107/// Create a Twitter image tag.
108///
109/// URL of image to use in the card. Images must be less than 5MB in size. JPG,
110/// PNG, WEBP and GIF formats are supported. Only the first frame of an animated
111/// GIF will be used. SVG is not supported.
112///
113/// _Used with summary, summary_large_image, player cards._
114///
115#[inline]
116pub fn create_image(content: &str) -> String {
117  create("twitter:image", content)
118}
119
120/// Create a Twitter image alt tag.
121///
122/// A text description of the image conveying the essential nature of an image
123/// to users who are visually impaired. Maximum 420 characters.
124///
125/// _Used with summary, summary_large_image, player cards._
126///
127/// ## Panics.
128/// Panics if the description is more than 420 characters.
129#[inline]
130pub fn create_image_alt(content: &str) -> String {
131  debug_assert!(
132    content.len() <= 420,
133    "Image alt has a maximum of 420 characters"
134  );
135  create("twitter:image:alt", content)
136}
137
138/// Create a Twitter creator id tag.
139///
140/// Twitter user ID of content creator.
141///
142/// _Used with summary, summary_large_image cards_
143#[inline]
144pub fn create_creator_id(content: &str) -> String {
145  create("twitter:creator:id", content)
146}
147
148/// Create a Twitter creator tag.
149///
150/// @username of content creator.
151///
152/// _Used with summary_large_image cards._
153#[inline]
154pub fn create_creator(content: &str) -> String {
155  create("twitter:creator", content)
156}
157
158/// Create a Twitter player tag.
159///
160/// HTTPS URL of player iframe
161///
162/// _Used with player card_
163///
164#[inline]
165pub fn create_player(content: &str) -> String {
166  create("twitter:player", content)
167}
168
169/// Create a Twitter player width tag.
170///
171/// Width of iframe in pixels.
172///
173/// _Used with player card_
174///
175#[inline]
176pub fn create_player_width(content: &str) -> String {
177  create("twitter:player:width", content)
178}
179
180/// Create a Twitter player height tag.
181///
182/// Height of iframe in pixels.
183///
184/// _Used with player card_
185///
186#[inline]
187pub fn create_player_height(content: &str) -> String {
188  create("twitter:player:height", content)
189}
190
191/// Create a Twitter player stream tag.
192///
193/// URL to raw video or audio stream.
194///
195/// _Used with player card_
196///
197#[inline]
198pub fn create_player_stream(content: &str) -> String {
199  create("twitter:player:stream", content)
200}
201
202/// Create a Twitter app name iPhone tag.
203///
204/// Name of your iPhone app.
205///
206/// _Used with app card_
207///
208#[inline]
209pub fn create_app_name_iphone(content: &str) -> String {
210  create("twitter:app:name:iphone", content)
211}
212
213/// Create a Twitter app id iPhone tag.
214///
215/// Your app ID in the iTunes App Store (Note: NOT your bundle ID).
216///
217/// _Used with app card_
218///
219#[inline]
220pub fn create_app_id_iphone(content: &str) -> String {
221  create("twitter:app:id:iphone", content)
222}
223
224/// Create a Twitter app url iPhone tag.
225///
226/// Your app’s custom URL scheme (you must include ”://” after your scheme
227/// name).
228///
229/// _Used with app card_
230///
231#[inline]
232pub fn create_app_url_iphone(content: &str) -> String {
233  create("twitter:app:url:iphone", content)
234}
235
236/// Create a Twitter app name iPad tag.
237///
238/// Name of your iPad app.
239///
240/// _Used with app card_
241///
242#[inline]
243pub fn create_app_name_ipad(content: &str) -> String {
244  create("twitter:app:name:ipad", content)
245}
246
247/// Create a Twitter app id iPad tag.
248///
249/// Your app ID in the iTunes App Store (Note: NOT your bundle ID).
250///
251/// _Used with app card_
252///
253#[inline]
254pub fn create_app_id_ipad(content: &str) -> String {
255  create("twitter:app:id:ipad", content)
256}
257
258/// Create a Twitter app url iPad tag.
259///
260/// Your app’s custom URL scheme (you must include ”://” after your scheme
261/// name).
262///
263/// _Used with app card_
264///
265#[inline]
266pub fn create_app_url_ipad(content: &str) -> String {
267  create("twitter:app:url:ipad", content)
268}
269
270/// Create a Twitter app name Google Play tag.
271///
272/// Name of your Android app.
273///
274/// _Used with app card_
275///
276#[inline]
277pub fn create_app_name_googleplay(content: &str) -> String {
278  create("twitter:app:name:googleplay", content)
279}
280
281/// Create a Twitter app id Google Play tag.
282///
283/// Your app ID in the Google Play Store.
284///
285/// _Used with app card_
286///
287#[inline]
288pub fn create_app_id_googleplay(content: &str) -> String {
289  create("twitter:app:id:googleplay", content)
290}
291
292/// Create a Twitter app url Google Play tag.
293///
294/// Your app’s custom URL scheme.
295///
296/// _Used with app card_
297///
298#[inline]
299pub fn create_app_url_googleplay(content: &str) -> String {
300  create("twitter:app:url:googleplay", content)
301}
302
303/// Create an HTML tag
304#[inline]
305fn create(name: &str, content: &str) -> String {
306  format!(r#"<meta name="{}" content="{}" />"#, name, content)
307}