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
302
303
304
305
use std::io::Write;

use crate::url::Url;

use self::private::SealedTryIntoUrl;

use super::sitemap_xml_writer::SitemapXmlWriter;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("invalid changefreq")]
    InvalidChangefreq,
    #[error("invalid lastmod")]
    InvalidLastmod,
    #[error("invalid loc")]
    InvalidLoc,
    #[error("invalid priority")]
    InvalidPriority,
    #[error("io")]
    Io(#[from] std::io::Error),
    #[error("max byte length is 50 MiB (52,428,800 bytes)")]
    MaxByteLength,
    #[error("max number of urls is 50,000")]
    MaxNumberOfUrls,
}

impl From<crate::sitemap_xml_writer::Error> for Error {
    fn from(value: crate::sitemap_xml_writer::Error) -> Self {
        match value {
            super::sitemap_xml_writer::Error::Io(e) => Error::Io(e),
            super::sitemap_xml_writer::Error::MaxByteLength => Error::MaxByteLength,
        }
    }
}

type Result<T, E = Error> = std::result::Result<T, E>;

/// A writer for sitemap file.
///
/// # Examples
///
/// The following example is a sitemap containing only one URL specified by `&str`.
///
/// ```rust
/// use sitemap_xml_writer::{Changefreq, Lastmod, Loc, Priority, SitemapWriter, Url};
/// use std::io::Cursor;
///
/// # fn main() -> anyhow::Result<()> {
/// let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
/// writer.write("http://www.example.com/")?;
/// writer.end()?;
///
/// assert_eq!(
///     String::from_utf8(writer.into_inner().into_inner())?,
///     concat!(
///         r#"<?xml version="1.0" encoding="UTF-8"?>"#,
///         r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
///         r#"<url>"#,
///         r#"<loc>http://www.example.com/</loc>"#,
///         r#"</url>"#,
///         r#"</urlset>"#
///     )
/// );
/// #    Ok(())
/// # }
/// ```
///
/// The following example is a sitemap that uses all the optional tags. It also includes an example using non-string types.
///
/// ```rust
/// use sitemap_xml_writer::{Changefreq, SitemapWriter, Url};
/// use std::io::Cursor;
///
/// # fn main() -> anyhow::Result<()> {
/// let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
/// writer.write(
///     Url::loc("http://www.example.com/")?
///         .lastmod("2005-01-01")?
///         .changefreq("monthly")?
///         .priority("0.8")?,
/// )?;
/// writer.end()?;
///
/// assert_eq!(
///     String::from_utf8(writer.into_inner().into_inner())?,
///     concat!(
///         r#"<?xml version="1.0" encoding="UTF-8"?>"#,
///         r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
///         r#"<url>"#,
///         r#"<loc>http://www.example.com/</loc>"#,
///         r#"<lastmod>2005-01-01</lastmod>"#,
///         r#"<changefreq>monthly</changefreq>"#,
///         r#"<priority>0.8</priority>"#,
///         r#"</url>"#,
///         r#"</urlset>"#
///     )
/// );
/// #     Ok(())
/// # }
/// ```
///
/// The following an example using `chrono` crate types.
///
#[cfg_attr(feature = "chrono", doc = "```rust")]
#[cfg_attr(not(feature = "chrono"), doc = "```rust,ignore")]
/// use sitemap_xml_writer::{Changefreq, SitemapWriter, Url};
/// use std::io::Cursor;
///
/// # fn main() -> anyhow::Result<()> {
/// let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
/// writer.write(
///     Url::loc("http://www.example.com/")?
///         // `::chrono::NaiveDate` and `::chrono::DateTime` are supported.
///         .lastmod(::chrono::NaiveDate::parse_from_str("2005-01-01", "%Y-%m-%d")?)?
///         .changefreq(Changefreq::Monthly)?
///         .priority(0.8)?
/// )?;
/// writer.end()?;
///
/// assert_eq!(
///     String::from_utf8(writer.into_inner().into_inner())?,
///     concat!(
///         r#"<?xml version="1.0" encoding="UTF-8"?>"#,
///         r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
///         r#"<url>"#,
///         r#"<loc>http://www.example.com/</loc>"#,
///         r#"<lastmod>2005-01-01</lastmod>"#,
///         r#"<changefreq>monthly</changefreq>"#,
///         r#"<priority>0.8</priority>"#,
///         r#"</url>"#,
///         r#"</urlset>"#
///     )
/// );
/// #     Ok(())
/// # }
/// ```
///
/// The following an example using `time` crate types.
///
#[cfg_attr(feature = "time", doc = "```rust")]
#[cfg_attr(not(feature = "time"), doc = "```rust,ignore")]
/// use sitemap_xml_writer::{Changefreq, SitemapWriter, Url};
/// use std::io::Cursor;
///
/// # fn main() -> anyhow::Result<()> {
/// let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
/// writer.write(
///     Url::loc("http://www.example.com/")?
///         // `::time::Date` and `::time::OffsetDateTime` are supported.
///         .lastmod(::time::macros::date!(2005-01-01))?
///         .changefreq(Changefreq::Monthly)?
///         .priority(0.8)?
/// )?;
/// writer.end()?;
///
/// assert_eq!(
///     String::from_utf8(writer.into_inner().into_inner())?,
///     concat!(
///         r#"<?xml version="1.0" encoding="UTF-8"?>"#,
///         r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
///         r#"<url>"#,
///         r#"<loc>http://www.example.com/</loc>"#,
///         r#"<lastmod>2005-01-01</lastmod>"#,
///         r#"<changefreq>monthly</changefreq>"#,
///         r#"<priority>0.8</priority>"#,
///         r#"</url>"#,
///         r#"</urlset>"#
///     )
/// );
/// #     Ok(())
/// # }
/// ```
///
/// The following an example using `url` crate types.
///
#[cfg_attr(feature = "url", doc = "```rust")]
#[cfg_attr(not(feature = "url"), doc = "```rust,ignore")]
/// use sitemap_xml_writer::{Changefreq, SitemapWriter, Url};
/// use std::io::Cursor;
///
/// # fn main() -> anyhow::Result<()> {
/// let mut writer = SitemapWriter::start(Cursor::new(Vec::new()))?;
/// writer.write(
///     // <https://crates.io/crates/url> support
///     // You can specify `::url::Url`.
///     // If you want to ensure that the URL is valid, use `::url::Url`.
///     // If you use &str, the URL is assumed to be valid and only the length
///     // check and XML entity escaping are performed.
///     Url::loc(::url::Url::parse("http://www.example.com/")?)?
///         .lastmod("2005-01-01")?
///         .changefreq(Changefreq::Monthly)?
///         .priority(0.8)?
/// )?;
/// writer.end()?;
///
/// assert_eq!(
///     String::from_utf8(writer.into_inner().into_inner())?,
///     concat!(
///         r#"<?xml version="1.0" encoding="UTF-8"?>"#,
///         r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#,
///         r#"<url>"#,
///         r#"<loc>http://www.example.com/</loc>"#,
///         r#"<lastmod>2005-01-01</lastmod>"#,
///         r#"<changefreq>monthly</changefreq>"#,
///         r#"<priority>0.8</priority>"#,
///         r#"</url>"#,
///         r#"</urlset>"#
///     )
/// );
/// #     Ok(())
/// # }
/// ```
pub struct SitemapWriter<W: Write> {
    writer: SitemapXmlWriter<W>,
    number_of_urls: usize,
}

impl<W: Write> SitemapWriter<W> {
    const MAX_NUMBER_OF_URLS: usize = 50_000;

    /// Creates a new `SitemapWriter<W>`. At the same time, write the XML declaration and an opening `<urlset>` tag.
    pub fn start(inner: W) -> Result<Self> {
        Self::start_inner(inner, false)
    }

    /// Creates a new `SitemapWriter<W>` with indentation enabled. At the same time, write the XML declaration and an opening `<urlset>` tag.
    pub fn start_with_indent(inner: W) -> Result<Self> {
        Self::start_inner(inner, true)
    }

    /// Writes a `url` element.
    pub fn write<'a, U>(&mut self, url: U) -> Result<()>
    where
        U: SealedTryIntoUrl<'a>,
    {
        if self.number_of_urls + 1 > Self::MAX_NUMBER_OF_URLS {
            return Err(Error::MaxNumberOfUrls);
        }
        self.number_of_urls += 1;

        let url: Url<'a> = url.try_into_url()?;
        self.writer.start_tag(b"url")?;

        let content = url.loc;
        self.writer.element(b"loc", content.as_ref())?;

        if let Some(content) = url.lastmod {
            self.writer.element(b"lastmod", content.as_ref())?;
        }

        if let Some(content) = url.changefreq {
            self.writer.element(b"changefreq", content.as_ref())?;
        }

        if let Some(content) = url.priority {
            self.writer.element(b"priority", content.as_ref())?;
        }

        self.writer.end_tag(b"url")?;
        Ok(())
    }

    /// Writes a closing `</urlset>` tag.
    pub fn end(&mut self) -> Result<()> {
        self.writer.end_tag(b"urlset")?;
        Ok(())
    }

    /// Unwraps this `SitemapWrite<W>`, returning the underlying writer.
    pub fn into_inner(self) -> W {
        self.writer.into_inner()
    }

    fn start_inner(inner: W, pretty: bool) -> Result<Self> {
        let mut s = Self {
            writer: SitemapXmlWriter::new(inner, pretty),
            number_of_urls: 0_usize,
        };
        s.writer.declaration()?;
        s.writer.start_tag_with_default_ns(b"urlset")?;
        Ok(s)
    }
}

mod private {
    use crate::Url;

    use super::Error;

    pub trait SealedTryIntoUrl<'a> {
        fn try_into_url(self) -> Result<Url<'a>, Error>;
    }

    impl<'a> SealedTryIntoUrl<'a> for Url<'a> {
        fn try_into_url(self) -> Result<Url<'a>, Error> {
            Ok(self)
        }
    }

    impl<'a> SealedTryIntoUrl<'a> for &'a str {
        fn try_into_url(self) -> Result<Url<'a>, Error> {
            Url::loc(self)
        }
    }
}