Meta Tags Generator — Rust source
Build the SEO head tags for any page — title, description, canonical, robots, Open Graph with smart fallbacks, Twitter card and theme-color — with live length warnings.
This is the Rust implementation — the same logic the interactive tool runs, in a shareable, citable form.
//! meta-tags-generator — build the ordered SEO head-tag set: title,
//! description, canonical, robots, Open Graph (og_title/og_description/og_url
//! falling back to the base fields, og:type website whenever the block is
//! present), Twitter card, theme-color. Rust port (canonical TS:
//! src/lib/meta-tags.ts; Go twin:
//! cli/meta-tags-generator/meta-tags-generator.go). Same fields, same
//! fallbacks, same emission order, same warnings (title > 60,
//! description > 160) and the same hard errors (missing title/description,
//! non-absolute og:image, non-hex theme-color suppress emission entirely).
/// The input field set; `MetaTagsInput::default()` is the all-optional shape.
#[derive(Debug, Clone, Default)]
pub struct MetaTagsInput {
pub title: String,
pub description: String,
pub canonical_url: String,
pub robots: String,
pub og_title: String,
pub og_description: String,
pub og_image: String,
pub og_url: String,
pub twitter_card: String,
pub twitter_site: String,
pub theme_color: String,
}
/// The ordered tag list plus diagnostics.
#[derive(Debug, Clone, Default)]
pub struct MetaTagsResult {
pub ok: bool,
pub errors: Vec<String>,
pub warnings: Vec<String>,
pub tags: Vec<String>,
}
fn is_hex_color(s: &str) -> bool {
s.len() == 7
&& s.starts_with('#')
&& s[1..].bytes().all(|b| b.is_ascii_hexdigit())
}
fn is_absolute_http(s: &str) -> bool {
s.starts_with("http://") || s.starts_with("https://")
}
/// Build the ordered head-tag set. Blank optional fields are skipped; hard
/// errors suppress emission entirely; length warnings never block emission.
pub fn build_meta_tags(input: &MetaTagsInput) -> MetaTagsResult {
let mut errors: Vec<String> = Vec::new();
let mut warnings: Vec<String> = Vec::new();
let title = input.title.trim();
let description = input.description.trim();
let canonical = input.canonical_url.trim();
let og_title_in = input.og_title.trim();
let og_title = if og_title_in.is_empty() { title } else { og_title_in };
let og_description_in = input.og_description.trim();
let og_description = if og_description_in.is_empty() { description } else { og_description_in };
let og_image = input.og_image.trim();
let og_url_in = input.og_url.trim();
let og_url = if og_url_in.is_empty() { canonical } else { og_url_in };
let theme_color = input.theme_color.trim();
if title.is_empty() {
errors.push("Title is required.".to_string());
}
if description.is_empty() {
errors.push("Description is required.".to_string());
}
if !og_image.is_empty() && !is_absolute_http(og_image) {
errors.push("og:image must be an absolute http(s) URL.".to_string());
}
if !theme_color.is_empty() && !is_hex_color(theme_color) {
errors.push("theme-color must be a #rrggbb hex value.".to_string());
}
if !errors.is_empty() {
return MetaTagsResult { ok: false, errors, warnings: Vec::new(), tags: Vec::new() };
}
if title.chars().count() > 60 {
warnings.push(format!(
"Title is {} characters — search results truncate around 60.",
title.chars().count()
));
}
if description.chars().count() > 160 {
warnings.push(format!(
"Description is {} characters — search results truncate around 160.",
description.chars().count()
));
}
let mut tags: Vec<String> = Vec::new();
tags.push(format!("<title>{title}</title>"));
tags.push(format!("<meta name=\"description\" content=\"{description}\" />"));
if !canonical.is_empty() {
tags.push(format!("<link rel=\"canonical\" href=\"{canonical}\" />"));
}
let robots = input.robots.trim();
if !robots.is_empty() {
tags.push(format!("<meta name=\"robots\" content=\"{robots}\" />"));
}
let has_og = !og_title_in.is_empty() || !og_description_in.is_empty() || !og_image.is_empty() || !og_url.is_empty();
if has_og {
tags.push(format!("<meta property=\"og:title\" content=\"{og_title}\" />"));
tags.push(format!("<meta property=\"og:description\" content=\"{og_description}\" />"));
if !og_image.is_empty() {
tags.push(format!("<meta property=\"og:image\" content=\"{og_image}\" />"));
}
if !og_url.is_empty() {
tags.push(format!("<meta property=\"og:url\" content=\"{og_url}\" />"));
}
tags.push("<meta property=\"og:type\" content=\"website\" />".to_string());
}
if !input.twitter_card.is_empty() {
tags.push(format!("<meta name=\"twitter:card\" content=\"{}\" />", input.twitter_card));
}
let twitter_site = input.twitter_site.trim();
if !twitter_site.is_empty() {
tags.push(format!("<meta name=\"twitter:site\" content=\"{twitter_site}\" />"));
}
if !theme_color.is_empty() {
tags.push(format!("<meta name=\"theme-color\" content=\"{theme_color}\" />"));
}
MetaTagsResult { ok: true, errors: Vec::new(), warnings, tags }
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →