Skip to content

Commit

Permalink
Encapsulated validation logic in AttributesValidator.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmackdev committed Oct 8, 2023
1 parent 76c0917 commit 8c33a99
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
43 changes: 34 additions & 9 deletions pubsubman/src/ui/publish_view/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -55,17 +65,32 @@ impl From<&Attributes> for HashMap<String, String> {
}
}

#[derive(Default)]
pub struct AttributesKeyCounter;
#[derive(Default, Clone)]
pub struct AttributesValidator(HashMap<String, usize>);

impl AttributesKeyCounter {
pub fn key_count_map(&self, attributes: &Attributes) -> HashMap<String, usize> {
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<AttributesValidator, AttributesValidator>;

ctx.memory_mut(|mem| {
mem.caches
.cache::<AttributesKeyCounterCache>()
.get(attributes)
})
}
34 changes: 6 additions & 28 deletions pubsubman/src/ui/publish_view/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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));
});
}

Expand Down Expand Up @@ -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<String, usize> {
impl egui::util::cache::ComputerMut<&Attributes, HashMap<String, usize>> for AttributesKeyCounter {
fn compute(&mut self, attributes: &Attributes) -> HashMap<String, usize> {
self.key_count_map(attributes)
}
}

type AttributesKeyCounterCache =
egui::util::cache::FrameCache<HashMap<String, usize>, AttributesKeyCounter>;

ctx.memory_mut(|mem| {
mem.caches
.cache::<AttributesKeyCounterCache>()
.get(attributes)
})
}

0 comments on commit 8c33a99

Please sign in to comment.