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
#![forbid(unsafe_code, future_incompatible)]
#![forbid(rust_2018_idioms, rust_2018_compatibility)]
#![deny(missing_debug_implementations, bad_style)]
#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]
//! ## Example
//! ```rust
//! use twitter_card::{Summary, TwitterCard};
//!
//! let card = Summary::builder()
//! .site("@flickr")
//! .title("Small Island Developing States Photo Submission")
//! .desc("View the album on Flickr.")
//! .image("https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg")
//! .build();
//! ```
//!
//! ```html
//! <meta name="twitter:card" content="summary" />
//! <meta name="twitter:site" content="@flickr" />
//! <meta name="twitter:title" content="Small Island Developing States Photo Submission" />
//! <meta name="twitter:description" content="View the album on Flickr." />
//! <meta name="twitter:image" content="https://farm6.staticflickr.com/5510/14338202952_93595258ff_z.jpg" />
//! ```
mod summary;
mod summary_large_image;
pub use crate::summary::Summary;
pub use crate::summary_large_image::SummaryLargeImage;
/// Twitter Card.
pub trait TwitterCard {
/// Convert the Twitter Card to a string.
fn build(self) -> String;
}
/// Create a Twitter card tag.
///
/// The card type.
///
/// _Used with all cards._
#[inline]
pub fn create_card(content: &str) -> String {
create("twitter:card", content)
}
/// Create a Twitter site tag.
///
/// @username of website. Either twitter:site or twitter:site:id is required.
///
/// _Used with summary, summary_large_image, app, player cards._
#[inline]
pub fn create_site(content: &str) -> String {
create("twitter:site", content)
}
/// Create a Twitter site id tag.
///
/// Same as twitter:site, but the user’s Twitter ID. Either twitter:site or
/// twitter:site:id is required.
///
/// _Used with summary, summary_large_image, player cards_
#[inline]
pub fn create_site_id(content: &str) -> String {
create("twitter:site:id", content)
}
/// Create a Twitter description tag.
///
/// Description of content.
///
/// _Used with summary, summary_large_image, player cards._
///
/// ## Panics.
/// Panics if the description is more than 200 characters.
#[inline]
pub fn create_desc(content: &str) -> String {
debug_assert!(
content.len() <= 200,
"Description has a maximum of 200 characters"
);
create("twitter:description", content)
}
/// Create a Twitter title tag.
///
/// Title of content.
///
/// _Used with summary, summary_large_image, player cards._
///
/// ## Panics.
/// Panics if the description is more than 70 characters.
#[inline]
pub fn create_title(content: &str) -> String {
debug_assert!(content.len() <= 70, "Title has a maximum of 70 characters");
create("twitter:title", content)
}
/// Create a Twitter image tag.
///
/// URL of image to use in the card. Images must be less than 5MB in size. JPG,
/// PNG, WEBP and GIF formats are supported. Only the first frame of an animated
/// GIF will be used. SVG is not supported.
///
/// _Used with summary, summary_large_image, player cards._
///
#[inline]
pub fn create_image(content: &str) -> String {
create("twitter:image", content)
}
/// Create a Twitter image alt tag.
///
/// A text description of the image conveying the essential nature of an image
/// to users who are visually impaired. Maximum 420 characters.
///
/// _Used with summary, summary_large_image, player cards._
///
/// ## Panics.
/// Panics if the description is more than 420 characters.
#[inline]
pub fn create_image_alt(content: &str) -> String {
debug_assert!(
content.len() <= 420,
"Image alt has a maximum of 420 characters"
);
create("twitter:image:alt", content)
}
/// Create a Twitter creator id tag.
///
/// Twitter user ID of content creator.
///
/// _Used with summary, summary_large_image cards_
#[inline]
pub fn create_creator_id(content: &str) -> String {
create("twitter:creator:id", content)
}
/// Create a Twitter creator tag.
///
/// @username of content creator.
///
/// _Used with summary_large_image cards._
#[inline]
pub fn create_creator(content: &str) -> String {
create("twitter:creator", content)
}
/// Create a Twitter player tag.
///
/// HTTPS URL of player iframe
///
/// _Used with player card_
///
#[inline]
pub fn create_player(content: &str) -> String {
create("twitter:player", content)
}
/// Create a Twitter player width tag.
///
/// Width of iframe in pixels.
///
/// _Used with player card_
///
#[inline]
pub fn create_player_width(content: &str) -> String {
create("twitter:player:width", content)
}
/// Create a Twitter player height tag.
///
/// Height of iframe in pixels.
///
/// _Used with player card_
///
#[inline]
pub fn create_player_height(content: &str) -> String {
create("twitter:player:height", content)
}
/// Create a Twitter player stream tag.
///
/// URL to raw video or audio stream.
///
/// _Used with player card_
///
#[inline]
pub fn create_player_stream(content: &str) -> String {
create("twitter:player:stream", content)
}
/// Create a Twitter app name iPhone tag.
///
/// Name of your iPhone app.
///
/// _Used with app card_
///
#[inline]
pub fn create_app_name_iphone(content: &str) -> String {
create("twitter:app:name:iphone", content)
}
/// Create a Twitter app id iPhone tag.
///
/// Your app ID in the iTunes App Store (Note: NOT your bundle ID).
///
/// _Used with app card_
///
#[inline]
pub fn create_app_id_iphone(content: &str) -> String {
create("twitter:app:id:iphone", content)
}
/// Create a Twitter app url iPhone tag.
///
/// Your app’s custom URL scheme (you must include ”://” after your scheme
/// name).
///
/// _Used with app card_
///
#[inline]
pub fn create_app_url_iphone(content: &str) -> String {
create("twitter:app:url:iphone", content)
}
/// Create a Twitter app name iPad tag.
///
/// Name of your iPad app.
///
/// _Used with app card_
///
#[inline]
pub fn create_app_name_ipad(content: &str) -> String {
create("twitter:app:name:ipad", content)
}
/// Create a Twitter app id iPad tag.
///
/// Your app ID in the iTunes App Store (Note: NOT your bundle ID).
///
/// _Used with app card_
///
#[inline]
pub fn create_app_id_ipad(content: &str) -> String {
create("twitter:app:id:ipad", content)
}
/// Create a Twitter app url iPad tag.
///
/// Your app’s custom URL scheme (you must include ”://” after your scheme
/// name).
///
/// _Used with app card_
///
#[inline]
pub fn create_app_url_ipad(content: &str) -> String {
create("twitter:app:url:ipad", content)
}
/// Create a Twitter app name Google Play tag.
///
/// Name of your Android app.
///
/// _Used with app card_
///
#[inline]
pub fn create_app_name_googleplay(content: &str) -> String {
create("twitter:app:name:googleplay", content)
}
/// Create a Twitter app id Google Play tag.
///
/// Your app ID in the Google Play Store.
///
/// _Used with app card_
///
#[inline]
pub fn create_app_id_googleplay(content: &str) -> String {
create("twitter:app:id:googleplay", content)
}
/// Create a Twitter app url Google Play tag.
///
/// Your app’s custom URL scheme.
///
/// _Used with app card_
///
#[inline]
pub fn create_app_url_googleplay(content: &str) -> String {
create("twitter:app:url:googleplay", content)
}
/// Create an HTML tag
#[inline]
fn create(name: &str, content: &str) -> String {
format!(r#"<meta name="{}" content="{}" />"#, name, content)
}