parse canonical url once per feed init

This commit is contained in:
yggverse 2025-08-10 01:34:06 +03:00
parent 1d4022f618
commit 04f4387534
2 changed files with 33 additions and 12 deletions

View file

@ -1,10 +1,13 @@
mod link;
use crate::Torrent; use crate::Torrent;
use link::Link;
use url::Url; use url::Url;
/// Export crawl index to the RSS file /// Export crawl index to the RSS file
pub struct Feed { pub struct Feed {
buffer: String, buffer: String,
canonical: Option<Url>, canonical: Link,
} }
impl Feed { impl Feed {
@ -43,7 +46,11 @@ impl Feed {
buffer.push_str(c.as_str()); buffer.push_str(c.as_str());
buffer.push_str("</link>") buffer.push_str("</link>")
} }
Self { buffer, canonical }
Self {
buffer,
canonical: Link::from_url(canonical),
}
} }
/// Append `item` to the feed `channel` /// Append `item` to the feed `channel`
@ -58,16 +65,7 @@ impl Feed {
.map(|b| b.to_string()) .map(|b| b.to_string())
.unwrap_or("?".into()) // @TODO .unwrap_or("?".into()) // @TODO
), ),
self.canonical self.canonical.link(&torrent.info_hash)
.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.buffer.push_str("<description>"); self.buffer.push_str("<description>");

23
src/feed/link.rs Normal file
View file

@ -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<Url>) -> 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)
}
}