From 4c180810a5089a6d438a94c17cd5180cc5edf55c Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 6 Jun 2023 16:54:08 +0800 Subject: [PATCH] fixup sorting so that it only goes by lowercase values --- apps/vault/src/actions.rs | 80 +++++++++---------- apps/vault/src/itemcache.rs | 140 ++++++++++++++++++++------------- apps/vault/src/ux/framework.rs | 8 +- 3 files changed, 128 insertions(+), 100 deletions(-) diff --git a/apps/vault/src/actions.rs b/apps/vault/src/actions.rs index 2555d38b7..d1c256362 100644 --- a/apps/vault/src/actions.rs +++ b/apps/vault/src/actions.rs @@ -512,14 +512,14 @@ impl<'a> ActionManager<'a> { extra.push_str("; "); extra.push_str(t!("vault.u2f.appinfo.authcount", xous::LANG)); extra.push_str(&pw.count.to_string()); - let li = ListItem { - name: desc.to_string(), // these allocs will be slow, but we do it only once on boot - extra: extra.to_string(), - dirty: true, - guid: guid.to_string(), - atime: pw.atime, - count: pw.count, - }; + let li = ListItem::new( + desc.to_string(), // these allocs will be slow, but we do it only once on boot + extra.to_string(), + true, + guid.to_string(), + pw.atime, + pw.count, + ); log::debug!("updating {} to list item {}", li.extra, li.key()); assert!( self.item_lists.lock().unwrap().insert_unique(entry.mode, li).is_some(), @@ -923,9 +923,9 @@ impl<'a> ActionManager<'a> { } #[cfg(feature="vaultperf")] self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!()); - if prev_entry.name != desc { // this check should be redundant, but, leave it in to be safe - prev_entry.name.clear(); - prev_entry.name.push_str(&desc); + if prev_entry.name() != &desc { // this check should be redundant, but, leave it in to be safe + prev_entry.name_clear(); + prev_entry.name_push_str(&desc); } #[cfg(feature="vaultperf")] self.perfentry(&self.pm, PERFMETA_NONE, 3, std::line!()); @@ -946,14 +946,14 @@ impl<'a> ActionManager<'a> { extra.push_str(t!("vault.u2f.appinfo.authcount", xous::LANG)); extra.push_str(&pw_rec.count.to_string()); - let li = ListItem { - name: desc.to_string(), // these allocs will be slow, but we do it only once on boot - extra: extra.to_string(), - dirty: true, - guid: key.name, - atime: pw_rec.atime, - count: pw_rec.count, - }; + let li = ListItem::new( + desc.to_string(), // these allocs will be slow, but we do it only once on boot + extra.to_string(), + true, + key.name, + pw_rec.atime, + pw_rec.count, + ); il.push(self.mode_cache, li); #[cfg(feature="vaultperf")] self.perfentry(&self.pm, PERFMETA_ENDBLOCK, 4, std::line!()); @@ -1022,14 +1022,14 @@ impl<'a> ActionManager<'a> { ai.count, ); let desc = format!("{} (U2F)", ai.name); - let li = ListItem { - name: desc, + let li = ListItem::new( + desc, extra, - dirty: true, - guid: key.name, - count: ai.count, - atime: ai.atime, - }; + true, + key.name, + ai.count, + ai.atime, + ); self.item_lists.lock().unwrap().insert_unique(self.mode_cache, li); } else { let err = format!("{}:{}:{}: ({})[moved data]...", @@ -1080,14 +1080,14 @@ impl<'a> ActionManager<'a> { }; let desc = format!("{} / {} (FIDO2)", result.rp_id, String::from_utf8(result.credential_id).unwrap_or("---".to_string())); let extra = format!("{}", name); - let li = ListItem { - name: desc, + let li = ListItem::new( + desc, extra, - dirty: true, - guid: key.name, - count: 0, - atime: 0, - }; + true, + key.name, + 0, + 0, + ); self.item_lists.lock().unwrap().insert_unique(self.mode_cache, li); } None => { @@ -1139,14 +1139,14 @@ impl<'a> ActionManager<'a> { if totp.is_hotp {"HOTP"} else {"TOTP"} ); let desc = format!("{}", totp.name); - let li = ListItem { - name: desc, + let li = ListItem::new( + desc, extra, - dirty: true, - guid: key.name, - count: 0, - atime: 0, - }; + true, + key.name, + 0, + 0, + ); self.item_lists.lock().unwrap().insert_unique(self.mode_cache, li); } else { let err = format!("{}:{}:{}: ({})[moved data]...", diff --git a/apps/vault/src/itemcache.rs b/apps/vault/src/itemcache.rs index 47d1bc5f4..bb7a8a70b 100644 --- a/apps/vault/src/itemcache.rs +++ b/apps/vault/src/itemcache.rs @@ -11,7 +11,7 @@ pub struct ListKey { impl ListKey { pub fn key_from_parts(name: &str, guid: &str) -> Self { ListKey { - name: name.to_owned(), + name: name.to_owned().to_lowercase(), guid: guid.to_owned(), } } @@ -24,52 +24,60 @@ impl ListKey { // re-uses the existing storage to avoid allocations pub fn reset_from_parts(&mut self, name: &str, guid: &str) { self.name.clear(); - self.name.push_str(name); + self.name.push_str(&name.to_lowercase()); self.guid.clear(); self.guid.push_str(guid); } } + +impl Ord for ListKey { + fn cmp(&self, other: &Self) -> Ordering { + match self.name.cmp(&other.name) { + Ordering::Equal => { + self.guid.cmp(&other.guid) + }, + Ordering::Greater => Ordering::Greater, + Ordering::Less => Ordering::Less, + } + } +} +impl PartialOrd:: for ListKey { + fn partial_cmp(&self, other: &ListItem) -> Option { + Some( + match self.name.cmp(&other.sortable_name) { + Ordering::Equal => { + self.guid.cmp(&other.guid) + }, + Ordering::Greater => Ordering::Greater, + Ordering::Less => Ordering::Less, + } + ) + } +} +impl PartialOrd for ListKey { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} impl PartialEq for ListKey { fn eq(&self, other: &Self) -> bool { self.name == other.name && self.guid == other.guid } } -impl PartialOrd for ListKey { - fn partial_cmp(&self, other: &Self) -> Option { - Some( - match self.name.to_lowercase().cmp(&other.name.to_lowercase()) { - Ordering::Equal => { - self.guid.cmp(&other.guid) - }, - Ordering::Greater => Ordering::Greater, - Ordering::Less => Ordering::Less, - } - ) - } -} +impl Eq for ListKey {} impl PartialEq:: for ListKey { fn eq(&self, other: &ListItem) -> bool { - self.name == other.name && self.guid == other.guid - } -} -impl PartialOrd:: for ListKey { - fn partial_cmp(&self, other: &ListItem) -> Option { - Some( - match self.name.to_lowercase().cmp(&other.name.to_lowercase()) { - Ordering::Equal => { - self.guid.cmp(&other.guid) - }, - Ordering::Greater => Ordering::Greater, - Ordering::Less => Ordering::Less, - } - ) + self.name == other.sortable_name && self.guid == other.guid } } + + /// Display list for items. "name" is the key by which the list is sorted. /// "extra" is more information about the item, which should not be part of the sort. #[derive(Debug)] pub struct ListItem { - pub name: String, + name: String, + sortable_name: String, pub extra: String, /// used by drawing routines to optimize refresh time pub dirty: bool, @@ -79,30 +87,9 @@ pub struct ListItem { pub atime: u64, pub count: u64, } -impl ListItem { - // good riddance. - /* pub fn clone(&self) -> ListItem { - ListItem { - name: self.name.to_string(), - extra: self.extra.to_string(), - dirty: self.dirty, - guid: self.guid.to_string(), - atime: self.atime, - count: self.count, - } - } */ - /// This is made available for edit/delete routines to generate the key without having to - /// make a whole ListItem record (which is somewhat expensive). - pub fn key_from_parts(name: &str, guid: &str) -> String { - name.to_lowercase() + &guid.to_string() - } - pub fn key(&self) -> String { - Self::key_from_parts(&self.name, &self.guid) - } -} impl Ord for ListItem { fn cmp(&self, other: &Self) -> Ordering { - match self.name.to_lowercase().cmp(&other.name.to_lowercase()) { + match self.sortable_name.cmp(&other.sortable_name) { Ordering::Equal => { self.guid.cmp(&other.guid) }, @@ -120,7 +107,7 @@ impl PartialOrd:: for ListItem { fn partial_cmp(&self, other: &ListKey) -> Option { // log::info!("self {}:{}\nother: {}:{}", self.name, self.guid, other.name, other.guid); Some( - match self.name.to_lowercase().cmp(&other.name.to_lowercase()) { + match self.sortable_name.cmp(&other.name) { Ordering::Equal => { self.guid.cmp(&other.guid) }, @@ -132,16 +119,57 @@ impl PartialOrd:: for ListItem { } impl PartialEq:: for ListItem { fn eq(&self, other: &ListKey) -> bool { - self.name == other.name && self.guid == other.guid + self.sortable_name == other.name && self.guid == other.guid } } impl PartialEq for ListItem { fn eq(&self, other: &Self) -> bool { - self.name == other.name && self.guid == other.guid + self.sortable_name == other.sortable_name && self.guid == other.guid } } impl Eq for ListItem {} +impl ListItem { + pub fn new( + name: String, + extra: String, + dirty: bool, + guid: String, + atime: u64, + count: u64, + ) -> Self { + let sortable_name = name.to_lowercase(); + Self { + name, + sortable_name, + extra, + dirty, + guid, + atime, + count, + } + } + /// This is made available for edit/delete routines to generate the key without having to + /// make a whole ListItem record (which is somewhat expensive). + pub fn key_from_parts(name: &str, guid: &str) -> String { + name.to_lowercase() + &guid.to_string() + } + pub fn key(&self) -> String { + Self::key_from_parts(&self.name, &self.guid) + } + pub fn name(&self) -> &String { + &self.name + } + pub fn name_clear(&mut self) { + self.name.clear(); + self.sortable_name.clear(); + } + pub fn name_push_str(&mut self, name: &String) { + self.name.push_str(name); + self.sortable_name.push_str(&name.to_lowercase()); + } +} + pub struct FilteredListView { list: Vec, sorted: bool, @@ -240,7 +268,7 @@ impl FilteredListView { return; } // step 1. binary search to find if the criteria is even anywhere in the list. - match self.list.binary_search_by(|probe| probe.name.partial_cmp(criteria).unwrap()) { + match self.list.binary_search_by(|probe| probe.sortable_name.partial_cmp(criteria).unwrap()) { Ok(mut index) | Err(mut index) => { if !self.list[index].name.to_lowercase().starts_with(criteria) { self.filter_range = None diff --git a/apps/vault/src/ux/framework.rs b/apps/vault/src/ux/framework.rs index 122dddebd..ea27b3c08 100644 --- a/apps/vault/src/ux/framework.rs +++ b/apps/vault/src/ux/framework.rs @@ -467,7 +467,7 @@ impl VaultUx { break; } if item.dirty { - log::debug!("drawing {}", item.name); + log::debug!("drawing {}", item.name()); let mut box_text = TextView::new(self.content, graphics_server::TextBounds::BoundingBox( Rectangle::new( @@ -485,7 +485,7 @@ impl VaultUx { box_text.border_width = 4; } match mode_at_entry { - VaultMode::Fido | VaultMode::Password => {write!(box_text, "{}\n{}", item.name, item.extra).ok();}, + VaultMode::Fido | VaultMode::Password => {write!(box_text, "{}\n{}", item.name(), item.extra).ok();}, VaultMode::Totp => { let fields = item.extra.split(':').collect::>(); if fields.len() == 5 { @@ -509,7 +509,7 @@ impl VaultUx { ).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string()); // why code on top? because the item.name can be very long, and it can wrap which would cause // the code to become hidden. - write!(box_text, "{}\n{}", code, item.name).ok(); + write!(box_text, "{}\n{}", code, item.name()).ok(); } else { let code = generate_totp_code( step_seconds, @@ -517,7 +517,7 @@ impl VaultUx { ).unwrap_or(t!("vault.error.record_error", xous::LANG).to_string()); // why code on top? because the item.name can be very long, and it can wrap which would cause // the code to become hidden. - write!(box_text, "HOTP {}\n{}", code, item.name).ok(); + write!(box_text, "HOTP {}\n{}", code, item.name()).ok(); } } else { write!(box_text, "{}", t!("vault.error.record_error", xous::LANG)).ok();