make listing name optional (e.g. invalid utf-8 encoding)

This commit is contained in:
yggverse 2025-07-10 14:41:37 +03:00
parent fc27f46cdf
commit 0d38aa5c06
4 changed files with 30 additions and 26 deletions

View file

@ -8,7 +8,7 @@ pub struct Value {
// Isolate by applying internal filter on value set
size: Option<u64>,
name: Option<String>,
list: Option<Vec<(String, u64)>>,
list: Option<Vec<(Option<String>, u64)>>,
}
impl Value {
@ -17,14 +17,14 @@ impl Value {
node: u64,
size: Option<u64>,
name: Option<String>,
list: Option<Vec<(String, u64)>>,
list: Option<Vec<(Option<String>, 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<String>, u64)>> {
self.list.as_ref()
}
/// Get reference to the safely constructed `length` member
@ -41,20 +41,14 @@ impl Value {
}
}
fn filter_name(value: Option<String>) -> Option<String> {
value.map(filter)
}
fn filter_list(value: Option<Vec<(String, u64)>>) -> Option<Vec<(String, u64)>> {
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::<String>());
}
s
fn filter(value: Option<String>) -> Option<String> {
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::<String>());
}
s
})
}