Skip to content

Commit

Permalink
Back to Newtype Pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
samclane committed Oct 3, 2024
1 parent f241e9c commit 3e39c82
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
100 changes: 50 additions & 50 deletions src/listener.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
collections::BTreeSet,
fmt::{Display, Formatter, Result as FmtResult},
ops::{Deref, DerefMut},
str::FromStr,
sync::{Arc, Mutex},
thread::{spawn, JoinHandle},
Expand Down Expand Up @@ -125,87 +126,86 @@ impl Ord for InputItem {
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InputAction {
items: BTreeSet<InputItem>,
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct InputAction(BTreeSet<InputItem>);

impl InputAction {
pub fn new() -> Self {
InputAction {
items: BTreeSet::new(),
}
InputAction(BTreeSet::new())
}
}

pub fn with_items(items: BTreeSet<InputItem>) -> Self {
InputAction { items }
impl Deref for InputAction {
type Target = BTreeSet<InputItem>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

pub fn insert(&mut self, item: InputItem) {
self.items.insert(item);
impl DerefMut for InputAction {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

pub fn remove(&mut self, item: &InputItem) {
self.items.remove(item);
impl FromIterator<InputItem> for InputAction {
fn from_iter<I: IntoIterator<Item = InputItem>>(iter: I) -> Self {
InputAction(iter.into_iter().collect())
}
}

impl IntoIterator for InputAction {
type Item = InputItem;
type IntoIter = std::collections::btree_set::IntoIter<InputItem>;

pub fn contains(&self, item: &InputItem) -> bool {
self.items.contains(item)
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

pub fn is_subset(&self, other: &Self) -> bool {
self.items.is_subset(&other.items)
impl Extend<InputItem> for InputAction {
fn extend<I: IntoIterator<Item = InputItem>>(&mut self, iter: I) {
self.0.extend(iter);
}
}

pub fn iter(&self) -> std::collections::btree_set::Iter<'_, InputItem> {
self.items.iter()
impl From<BTreeSet<InputItem>> for InputAction {
fn from(items: BTreeSet<InputItem>) -> Self {
InputAction(items)
}
}

pub fn extend(&mut self, other: &Self) {
self.items.extend(other.items.iter().cloned());
impl From<&[InputItem]> for InputAction {
fn from(items: &[InputItem]) -> Self {
InputAction(items.iter().cloned().collect())
}
}

impl FromStr for InputAction {
type Err = InputActionParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts = s.split('+');
let mut items = BTreeSet::new();

for part in parts {
let part = part.trim();
match InputItem::from_str(part) {
Ok(item) => {
items.insert(item);
}
Err(_) => {
return Err(InputActionParseError::InvalidItem(part.to_string()));
}
}
}

Ok(InputAction { items })
s.split('+')
.map(str::trim)
.map(InputItem::from_str)
.collect::<Result<BTreeSet<_>, _>>()
.map(InputAction)
.map_err(|e| InputActionParseError::InvalidItem(e.to_string()))
}
}

impl Display for InputAction {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
let mut items: Vec<String> = self.items.iter().map(|item| item.to_string()).collect();
let mut items: Vec<String> = self.iter().map(|item| item.to_string()).collect();
items.sort();
write!(f, "{}", items.join("+"))
}
}

impl Default for InputAction {
fn default() -> Self {
InputAction::new()
}
}

impl From<BTreeSet<InputItem>> for InputAction {
fn from(items: BTreeSet<InputItem>) -> Self {
InputAction { items }
impl PartialEq<BTreeSet<InputItem>> for InputAction {
fn eq(&self, other: &BTreeSet<InputItem>) -> bool {
self.0 == *other
}
}

Expand Down Expand Up @@ -458,7 +458,7 @@ impl InputListener {
.keys_pressed
.lock()
.expect("Failed to lock keys_pressed mutex");
input_action.items.is_subset(&keys)
input_action.is_subset(&keys)
}

pub fn add_callback(&self, callback: BackgroundCallback) {
Expand Down Expand Up @@ -546,7 +546,7 @@ mod tests {
]
.into_iter()
.collect();
assert_eq!(action.items, expected_items);
assert_eq!(action, expected_items);

assert!(InputAction::from_str("ctrl+invalid").is_err());
}
Expand Down Expand Up @@ -578,7 +578,7 @@ mod tests {
]
.into_iter()
.collect();
assert_eq!(input_action.items, expected_items);
assert_eq!(input_action, expected_items);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl TextBuffer for KeyboardShortcut {
}
if let Ok(keys) = InputAction::from_str(&c.to_string()) {
// combine the 2 sets
new_keys.extend(&keys);
new_keys.extend(keys);
}
}
self.keys = new_keys;
Expand Down Expand Up @@ -244,7 +244,7 @@ impl<'a> Widget for ShortcutEdit<'a> {
for key in inputstate.keys_down.iter() {
let modifiers = inputstate.modifiers;
let input_item = from_egui(*key, modifiers);
keys_pressed.extend(&input_item);
keys_pressed.extend(input_item);
}
shortcut.keys = keys_pressed;
shortcut.update_display_string();
Expand Down

0 comments on commit 3e39c82

Please sign in to comment.