Skip to main content

next_rust_server/
seo.rs

1//! Generated `sitemap.xml` and `robots.txt` (`app/sitemap.rs`, `app/robots.rs`).
2
3use crate::response::{IntoResponse, Response};
4
5/// `pub async fn sitemap() -> Sitemap`
6#[derive(Debug, Clone, Default)]
7pub struct Sitemap {
8    pub entries: Vec<SitemapEntry>,
9}
10
11#[derive(Debug, Clone, Default)]
12pub struct SitemapEntry {
13    pub url: String,
14    /// W3C datetime, e.g. `2026-09-17`.
15    pub last_modified: Option<String>,
16    pub change_frequency: Option<String>,
17    pub priority: Option<f32>,
18}
19
20impl Sitemap {
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    pub fn url(mut self, url: impl Into<String>) -> Self {
26        self.entries.push(SitemapEntry { url: url.into(), ..Default::default() });
27        self
28    }
29
30    pub fn entry(mut self, entry: SitemapEntry) -> Self {
31        self.entries.push(entry);
32        self
33    }
34
35    pub fn to_xml(&self) -> String {
36        let esc = |s: &str| next_rust_view::escape_attr(s).into_owned();
37        let mut x = String::from(
38            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n",
39        );
40        for e in &self.entries {
41            x.push_str("  <url>\n    <loc>");
42            x.push_str(&esc(&e.url));
43            x.push_str("</loc>\n");
44            if let Some(v) = &e.last_modified {
45                x.push_str(&format!("    <lastmod>{}</lastmod>\n", esc(v)));
46            }
47            if let Some(v) = &e.change_frequency {
48                x.push_str(&format!("    <changefreq>{}</changefreq>\n", esc(v)));
49            }
50            if let Some(v) = e.priority {
51                x.push_str(&format!("    <priority>{:.1}</priority>\n", v.clamp(0.0, 1.0)));
52            }
53            x.push_str("  </url>\n");
54        }
55        x.push_str("</urlset>\n");
56        x
57    }
58}
59
60impl IntoResponse for Sitemap {
61    fn into_response(self) -> Response {
62        Response::new(http::StatusCode::OK, crate::response::Body::Bytes(self.to_xml().into()))
63            .with_content_type("application/xml")
64            .with_cache_control("public, max-age=3600")
65    }
66}
67
68/// `pub async fn robots() -> Robots`
69#[derive(Debug, Clone, Default)]
70pub struct Robots {
71    pub rules: Vec<RobotsRule>,
72    pub sitemap: Option<String>,
73    pub host: Option<String>,
74}
75
76#[derive(Debug, Clone, Default)]
77pub struct RobotsRule {
78    pub user_agent: String,
79    pub allow: Vec<String>,
80    pub disallow: Vec<String>,
81    pub crawl_delay: Option<u32>,
82}
83
84impl Robots {
85    /// Allow everything for all user agents.
86    pub fn allow_all() -> Self {
87        Robots {
88            rules: vec![RobotsRule { user_agent: "*".into(), allow: vec!["/".into()], ..Default::default() }],
89            ..Default::default()
90        }
91    }
92
93    pub fn rule(mut self, rule: RobotsRule) -> Self {
94        self.rules.push(rule);
95        self
96    }
97
98    pub fn sitemap(mut self, url: impl Into<String>) -> Self {
99        self.sitemap = Some(url.into());
100        self
101    }
102
103    pub fn to_text(&self) -> String {
104        let clean = |s: &str| s.replace(['\r', '\n'], "");
105        let mut t = String::new();
106        for r in &self.rules {
107            t.push_str(&format!("User-Agent: {}\n", clean(&r.user_agent)));
108            for a in &r.allow {
109                t.push_str(&format!("Allow: {}\n", clean(a)));
110            }
111            for d in &r.disallow {
112                t.push_str(&format!("Disallow: {}\n", clean(d)));
113            }
114            if let Some(c) = r.crawl_delay {
115                t.push_str(&format!("Crawl-delay: {c}\n"));
116            }
117            t.push('\n');
118        }
119        if let Some(h) = &self.host {
120            t.push_str(&format!("Host: {}\n", clean(h)));
121        }
122        if let Some(s) = &self.sitemap {
123            t.push_str(&format!("Sitemap: {}\n", clean(s)));
124        }
125        t
126    }
127}
128
129impl IntoResponse for Robots {
130    fn into_response(self) -> Response {
131        Response::text(self.to_text()).with_cache_control("public, max-age=3600")
132    }
133}