use std::borrow::Cow;
use crate::{sitemap_index_writer::Error, Lastmod, Loc};
type Result<T, E = Error> = std::result::Result<T, E>;
#[cfg_attr(all(feature = "chrono", feature = "url"), doc = "```rust")]
#[cfg_attr(not(all(feature = "chrono", feature = "url")), doc = "```rust,ignore")]
#[cfg_attr(all(feature = "time", feature = "url"), doc = "```rust")]
#[cfg_attr(not(all(feature = "time", feature = "url")), doc = "```rust,ignore")]
pub struct Sitemap<'a> {
pub(crate) loc: Cow<'a, str>,
pub(crate) lastmod: Option<Cow<'a, str>>,
}
impl<'a> TryFrom<&'a str> for Sitemap<'a> {
type Error = Error;
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
Self::loc(value)
}
}
impl<'a> Sitemap<'a> {
#[cfg_attr(feature = "url", doc = "```rust")]
#[cfg_attr(not(feature = "url"), doc = "```rust,ignore")]
pub fn loc<S>(loc: S) -> Result<Self>
where
S: TryInto<Loc<'a>>,
{
let loc = loc.try_into().map_err(|_| Error::InvalidLoc)?.into_inner();
Ok(Self { loc, lastmod: None })
}
#[cfg_attr(feature = "chrono", doc = "```rust")]
#[cfg_attr(not(feature = "chrono"), doc = "```rust,ignore")]
#[cfg_attr(feature = "time", doc = "```rust")]
#[cfg_attr(not(feature = "time"), doc = "```rust,ignore")]
pub fn lastmod<S>(mut self, s: S) -> Result<Self>
where
S: TryInto<Lastmod<'a>>,
{
let lastmod = s
.try_into()
.map_err(|_| Error::InvalidLastmod)?
.into_inner();
self.lastmod = Some(lastmod);
Ok(self)
}
}