diff --git a/src/feed.rs b/src/feed.rs index 71a25e5..79fb586 100644 --- a/src/feed.rs +++ b/src/feed.rs @@ -1,10 +1,13 @@ +mod link; + use crate::Torrent; +use link::Link; use url::Url; /// Export crawl index to the RSS file pub struct Feed { buffer: String, - canonical: Option, + canonical: Link, } impl Feed { @@ -43,7 +46,11 @@ impl Feed { buffer.push_str(c.as_str()); buffer.push_str("") } - Self { buffer, canonical } + + Self { + buffer, + canonical: Link::from_url(canonical), + } } /// Append `item` to the feed `channel` @@ -58,16 +65,7 @@ impl Feed { .map(|b| b.to_string()) .unwrap_or("?".into()) // @TODO ), - self.canonical - .clone() - .map(|mut c| escape({ - c.set_path(&torrent.info_hash); - c.set_fragment(None); - c.set_query(None); - c.as_str() - })) - .unwrap_or(escape(&torrent.info_hash)) // should be non-optional absolute URL - // by the RSS specification @TODO + self.canonical.link(&torrent.info_hash) )); self.buffer.push_str(""); diff --git a/src/feed/link.rs b/src/feed/link.rs new file mode 100644 index 0000000..afe7848 --- /dev/null +++ b/src/feed/link.rs @@ -0,0 +1,23 @@ +use url::Url; + +/// Valid link prefix donor for the RSS channel item +pub struct Link(String); + +impl Link { + pub fn from_url(canonical: Option) -> Self { + Self( + canonical + .map(|mut c| { + c.set_path("/"); + c.set_fragment(None); + c.set_query(None); + super::escape(c.as_str()) // filter once + }) + .unwrap_or_default(), // should be non-optional absolute URL + // by the RSS specification @TODO + ) + } + pub fn link(&self, info_hash: &str) -> String { + format!("{}{info_hash}", self.0) + } +}