Refactor SwaybarArray::get_by_name* to use a map

Also some other minor refactorings.
This commit is contained in:
Stephen Seo 2022-07-10 15:10:26 +09:00
parent eb09c43375
commit 8b0c892f05
2 changed files with 22 additions and 21 deletions

View file

@ -145,10 +145,8 @@ fn main() {
if is_empty { if is_empty {
let meminfo_obj = SwaybarObject::from_string("meminfo".to_owned(), meminfo_string); let meminfo_obj = SwaybarObject::from_string("meminfo".to_owned(), meminfo_string);
array.push_object(meminfo_obj); array.push_object(meminfo_obj);
} else { } else if let Some(meminfo_obj) = array.get_by_name_mut("meminfo") {
if let Some(meminfo_obj) = array.get_by_name_mut("meminfo") { meminfo_obj.update_as_generic(meminfo_string, None);
meminfo_obj.update_as_generic(meminfo_string, None);
}
} }
} }
@ -165,10 +163,8 @@ fn main() {
if is_empty { if is_empty {
let loadavg_obj = SwaybarObject::from_string("loadavg".to_owned(), loadavg_string); let loadavg_obj = SwaybarObject::from_string("loadavg".to_owned(), loadavg_string);
array.push_object(loadavg_obj); array.push_object(loadavg_obj);
} else { } else if let Some(loadavg_obj) = array.get_by_name_mut("loadavg") {
if let Some(loadavg_obj) = array.get_by_name_mut("loadavg") { loadavg_obj.update_as_generic(loadavg_string, None);
loadavg_obj.update_as_generic(loadavg_string, None);
}
} }
} }

View file

@ -1,4 +1,5 @@
use std::{ use std::{
collections::HashMap,
fmt::Display, fmt::Display,
ops::{Index, IndexMut}, ops::{Index, IndexMut},
}; };
@ -54,9 +55,10 @@ pub struct SwaybarObject {
pub markup: Option<String>, pub markup: Option<String>,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Debug)]
pub struct SwaybarArray { pub struct SwaybarArray {
objects: Vec<SwaybarObject>, objects: Vec<SwaybarObject>,
objects_idx_map: HashMap<String, usize>,
} }
impl SwaybarHeader { impl SwaybarHeader {
@ -181,11 +183,13 @@ impl SwaybarArray {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
objects: Vec::new(), objects: Vec::new(),
objects_idx_map: HashMap::new(),
} }
} }
pub fn push_object(&mut self, object: SwaybarObject) { pub fn push_object(&mut self, object: SwaybarObject) {
self.objects.push(object); self.objects.push(object);
self.refresh_map();
} }
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
@ -194,12 +198,8 @@ impl SwaybarArray {
// TODO this is linear, and it probably is possible to improve this // TODO this is linear, and it probably is possible to improve this
pub fn get_by_name(&self, name: &str) -> Option<&SwaybarObject> { pub fn get_by_name(&self, name: &str) -> Option<&SwaybarObject> {
for object in &self.objects { if let Some(idx) = self.objects_idx_map.get(name) {
if let Some(object_name) = object.get_name() { return Some(&self.objects[*idx]);
if object_name == name {
return Some(object);
}
}
} }
None None
@ -207,16 +207,21 @@ impl SwaybarArray {
// TODO this is linear, and it probably is possible to improve this // TODO this is linear, and it probably is possible to improve this
pub fn get_by_name_mut(&mut self, name: &str) -> Option<&mut SwaybarObject> { pub fn get_by_name_mut(&mut self, name: &str) -> Option<&mut SwaybarObject> {
for object in &mut self.objects { if let Some(idx) = self.objects_idx_map.get(name) {
if let Some(object_name) = object.get_name() { return Some(&mut self.objects[*idx]);
if object_name == name {
return Some(object);
}
}
} }
None None
} }
fn refresh_map(&mut self) {
self.objects_idx_map.clear();
for (idx, object) in self.objects.iter().enumerate() {
if let Some(name) = object.get_name() {
self.objects_idx_map.insert(name.to_owned(), idx);
}
}
}
} }
impl Index<usize> for SwaybarArray { impl Index<usize> for SwaybarArray {