implement item description format helper

This commit is contained in:
yggverse 2025-07-09 03:28:04 +03:00
parent 51ffa4d93d
commit 82d2a89aa2
2 changed files with 28 additions and 12 deletions

View file

@ -12,7 +12,6 @@ mod trackers;
use anyhow::Result; use anyhow::Result;
use config::Config; use config::Config;
use debug::Debug; use debug::Debug;
use format::Format;
use index::Index; use index::Index;
use librqbit::{ use librqbit::{
AddTorrent, AddTorrentOptions, AddTorrentResponse, ByteBufOwned, ConnectionOptions, AddTorrent, AddTorrentOptions, AddTorrentResponse, ByteBufOwned, ConnectionOptions,
@ -250,7 +249,7 @@ async fn main() -> Result<()> {
rss.push( rss.push(
k, k,
v.name().unwrap_or(k), v.name().unwrap_or(k),
v.size().map(|l| l.bytes()), rss::item_description(v.size(), v.list()),
Some(&v.time.to_rfc2822()), Some(&v.time.to_rfc2822()),
)? )?
} }
@ -317,24 +316,24 @@ fn list(info: &TorrentMetaV1Info<ByteBufOwned>, limit: usize) -> Option<Vec<(Str
let mut b = Vec::with_capacity(files.len()); let mut b = Vec::with_capacity(files.len());
let mut i = files.iter(); let mut i = files.iter();
let mut t = 0; let mut t = 0;
while let Some(f) = i.next() { for f in i.by_ref() {
t += 1; t += 1;
if t < limit { if t < limit {
b.push(( b.push((
String::from_utf8(f.path.iter().flat_map(|b| b.to_vec()).collect()) String::from_utf8(f.path.iter().flat_map(|b| b.to_vec()).collect())
.unwrap_or_default(), .unwrap_or_default(),
f.length, f.length,
)) ));
} else { continue;
}
// limit reached: count sizes left and use placeholder as the last item name // limit reached: count sizes left and use placeholder as the last item name
let mut l = 0; let mut l = 0;
while let Some(f) = i.next() { for f in i.by_ref() {
l += f.length l += f.length
} }
b.push(("...".to_string(), l)); b.push(("...".to_string(), l));
break; break;
} }
}
b b
}) })
} }

View file

@ -92,6 +92,23 @@ impl Rss {
} }
} }
pub fn item_description(size: Option<u64>, list: Option<&Vec<(String, u64)>>) -> Option<String> {
if size.is_none() && list.is_none() {
return None;
}
let mut b = Vec::with_capacity(list.map(|l| l.len()).unwrap_or_default() + 1);
if let Some(s) = size {
use crate::format::Format;
b.push(s.bytes())
}
if let Some(l) = list {
for (path, size) in l {
b.push(format!("* {path}: {size}"))
}
}
Some(b.join("<br>\n"))
}
fn escape(subject: &str) -> String { fn escape(subject: &str) -> String {
subject subject
.replace('&', "&amp;") .replace('&', "&amp;")