diff --git a/src/index.rs b/src/index.rs index a48eb11..2969ccf 100644 --- a/src/index.rs +++ b/src/index.rs @@ -62,7 +62,7 @@ impl Index { infohash: String, node: u64, size: u64, - list: Option>, + list: Option, u64)>>, name: Option, ) { if self diff --git a/src/index/value.rs b/src/index/value.rs index de50e47..e10c306 100644 --- a/src/index/value.rs +++ b/src/index/value.rs @@ -8,7 +8,7 @@ pub struct Value { // Isolate by applying internal filter on value set size: Option, name: Option, - list: Option>, + list: Option, u64)>>, } impl Value { @@ -17,14 +17,14 @@ impl Value { node: u64, size: Option, name: Option, - list: Option>, + list: Option, u64)>>, ) -> Self { Self { time: Utc::now(), node, size, - list: filter_list(list), - name: filter_name(name), + list: list.map(|f| f.into_iter().map(|(n, l)| (filter(n), l)).collect()), + name: filter(name), } } /// Get reference to the safely constructed `name` member @@ -32,7 +32,7 @@ impl Value { self.name.as_ref() } /// Get reference to the safely constructed files `list` member - pub fn list(&self) -> Option<&Vec<(String, u64)>> { + pub fn list(&self) -> Option<&Vec<(Option, u64)>> { self.list.as_ref() } /// Get reference to the safely constructed `length` member @@ -41,20 +41,14 @@ impl Value { } } -fn filter_name(value: Option) -> Option { - value.map(filter) -} - -fn filter_list(value: Option>) -> Option> { - value.map(|f| f.into_iter().map(|(n, l)| (filter(n), l)).collect()) -} - /// Strip tags and bom chars, crop long strings (prevents memory pool overload) -fn filter(value: String) -> String { - const C: usize = 125; // + 3 for `...` offset, 128 chars max @TODO optional - let s = value._strip_bom()._strip_tags(); - if s.chars().count() > C { - return format!("{}...", s.chars().take(C).collect::()); - } - s +fn filter(value: Option) -> Option { + value.map(|v| { + const C: usize = 125; // + 3 for `...` offset, 128 chars max @TODO optional + let s = v._strip_bom()._strip_tags(); + if s.chars().count() > C { + return format!("{}...", s.chars().take(C).collect::()); + } + s + }) } diff --git a/src/main.rs b/src/main.rs index 84dcaa3..846837e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -363,7 +363,10 @@ fn size(info: &TorrentMetaV1Info) -> u64 { t } -fn list(info: &TorrentMetaV1Info, limit: usize) -> Option> { +fn list( + info: &TorrentMetaV1Info, + limit: usize, +) -> Option, u64)>> { info.files.as_ref().map(|files| { let mut b = Vec::with_capacity(files.len()); let mut i = files.iter(); @@ -387,7 +390,7 @@ fn list(info: &TorrentMetaV1Info, limit: usize) -> Option, limit: usize) -> Option, list: Option<&Vec<(String, u64)>>) -> Option { +pub fn item_description( + size: Option, + list: Option<&Vec<(Option, u64)>>, +) -> Option { use crate::format::Format; if size.is_none() && list.is_none() { return None; @@ -115,7 +118,11 @@ pub fn item_description(size: Option, list: Option<&Vec<(String, u64)>>) -> } if let Some(l) = list { for (path, size) in l { - b.push(format!("{path} ({})", size.bytes())) + b.push(format!( + "{} ({})", + path.as_deref().unwrap_or("?"), // @TODO invalid encoding + size.bytes() + )) } } Some(b.join("\n"))