From 8c33a99db3180f0b14b012005ff8e61d2c98c672 Mon Sep 17 00:00:00 2001 From: dmackdev Date: Sun, 8 Oct 2023 18:59:03 +0100 Subject: [PATCH] Encapsulated validation logic in AttributesValidator. --- pubsubman/src/ui/publish_view/attributes.rs | 43 ++++++++++++++++----- pubsubman/src/ui/publish_view/mod.rs | 34 +++------------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/pubsubman/src/ui/publish_view/attributes.rs b/pubsubman/src/ui/publish_view/attributes.rs index 9db215c..5bb2fd8 100644 --- a/pubsubman/src/ui/publish_view/attributes.rs +++ b/pubsubman/src/ui/publish_view/attributes.rs @@ -6,6 +6,16 @@ use crate::ui::validity_frame::ValidityFrame; pub struct Attributes(Vec<(String, String)>); impl Attributes { + fn validator(&self) -> AttributesValidator { + let mut key_count_map = HashMap::new(); + + for (key, _) in self.0.iter() { + *key_count_map.entry(key.clone()).or_insert_with(|| 0) += 1; + } + + AttributesValidator(key_count_map) + } + pub fn is_empty(&self) -> bool { self.0.is_empty() } @@ -55,17 +65,32 @@ impl From<&Attributes> for HashMap { } } -#[derive(Default)] -pub struct AttributesKeyCounter; +#[derive(Default, Clone)] +pub struct AttributesValidator(HashMap); -impl AttributesKeyCounter { - pub fn key_count_map(&self, attributes: &Attributes) -> HashMap { - let mut key_count_map = HashMap::new(); +impl AttributesValidator { + pub fn is_valid(&self) -> bool { + self.0.iter().all(|(_, count)| *count < 2) + } - for (key, _) in attributes.0.iter() { - *key_count_map.entry(key.clone()).or_insert_with(|| 0) += 1; - } + pub fn is_key_valid(&self, key: &str) -> bool { + self.0.get(key).is_some_and(|count| *count < 2) + } +} - key_count_map +pub fn attributes_validator(ctx: &egui::Context, attributes: &Attributes) -> AttributesValidator { + impl egui::util::cache::ComputerMut<&Attributes, AttributesValidator> for AttributesValidator { + fn compute(&mut self, attributes: &Attributes) -> AttributesValidator { + attributes.validator() + } } + + type AttributesKeyCounterCache = + egui::util::cache::FrameCache; + + ctx.memory_mut(|mem| { + mem.caches + .cache::() + .get(attributes) + }) } diff --git a/pubsubman/src/ui/publish_view/mod.rs b/pubsubman/src/ui/publish_view/mod.rs index ba91330..82b4ff4 100644 --- a/pubsubman/src/ui/publish_view/mod.rs +++ b/pubsubman/src/ui/publish_view/mod.rs @@ -1,14 +1,12 @@ -use std::collections::HashMap; - use pubsubman_backend::{ message::FrontendMessage, model::{PubsubMessageToPublish, TopicName}, }; use tokio::sync::mpsc::Sender; -use crate::{actions::publish_message, ui::publish_view::attributes::AttributesKeyCounter}; +use crate::actions::publish_message; -use self::attributes::Attributes; +use self::attributes::{attributes_validator, Attributes}; mod attributes; @@ -40,8 +38,8 @@ impl PublishView { }); let mut header_text = egui::RichText::new("Attributes"); - let attributes_key_count_map = key_count_map(ui.ctx(), &self.attributes); - let all_attributes_valid = attributes_key_count_map.iter().all(|(_, count)| *count < 2); + let attributes_validator = attributes_validator(ui.ctx(), &self.attributes); + let all_attributes_valid = attributes_validator.is_valid(); if !all_attributes_valid { header_text = header_text.color(ui.visuals().error_fg_color); @@ -57,11 +55,8 @@ impl PublishView { .num_columns(3) .spacing((0.0, 4.0)) .show(ui, |ui| { - self.attributes.show(ui, |key| { - attributes_key_count_map - .get(key) - .is_some_and(|count| *count < 2) - }); + self.attributes + .show(ui, |key| attributes_validator.is_key_valid(key)); }); } @@ -90,20 +85,3 @@ impl From<&mut PublishView> for PubsubMessageToPublish { Self::new(val.data.clone(), (&val.attributes).into()) } } - -fn key_count_map(ctx: &egui::Context, attributes: &Attributes) -> HashMap { - impl egui::util::cache::ComputerMut<&Attributes, HashMap> for AttributesKeyCounter { - fn compute(&mut self, attributes: &Attributes) -> HashMap { - self.key_count_map(attributes) - } - } - - type AttributesKeyCounterCache = - egui::util::cache::FrameCache, AttributesKeyCounter>; - - ctx.memory_mut(|mem| { - mem.caches - .cache::() - .get(attributes) - }) -}