Skip to content

Commit

Permalink
Use EnumSet for BrailleChars
Browse files Browse the repository at this point in the history
  • Loading branch information
egli committed Jan 23, 2024
1 parent f81fd13 commit 1ffa1a4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
49 changes: 24 additions & 25 deletions src/parser/braille.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::collections::HashSet;
use enumset::{enum_set, EnumSet, EnumSetType};

#[derive(thiserror::Error, Debug, PartialEq)]
pub enum ParseError {
#[error("Invalid braille {character:?}")]
InvalidBraille { character: Option<char> },
}

#[derive(Hash, PartialEq, Eq, Debug)]
#[derive(EnumSetType, Debug)]
pub enum BrailleDot {
Dot0,
Dot1,
Expand All @@ -26,7 +26,7 @@ pub enum BrailleDot {
DotF,
}

pub type BrailleChar = HashSet<BrailleDot>;
pub type BrailleChar = EnumSet<BrailleDot>;
pub type BrailleChars = Vec<BrailleChar>;

pub fn is_braille_dot(c: char) -> bool {
Expand Down Expand Up @@ -91,18 +91,17 @@ fn dot_to_hex(dot: &BrailleDot) -> u32 {
}

fn has_virtual_dots(char: &BrailleChar) -> bool {
let virtual_dots = HashSet::from([
BrailleDot::Dot9,
BrailleDot::DotA,
BrailleDot::DotB,
BrailleDot::DotC,
BrailleDot::DotD,
BrailleDot::DotE,
BrailleDot::DotF,
]);
let virtual_dots = enum_set!(
BrailleDot::Dot9 |
BrailleDot::DotA |
BrailleDot::DotB |
BrailleDot::DotC |
BrailleDot::DotD |
BrailleDot::DotE |
BrailleDot::DotF |
);
!virtual_dots
.intersection(char)
.collect::<HashSet<_>>()
.intersection(*char)
.is_empty()
}

Expand All @@ -119,7 +118,7 @@ fn dot_to_unicode(dot: &BrailleChar) -> char {
};
let unicode = dot
.iter()
.map(dot_to_hex)
.map(|dot| dot_to_hex(&dot))
.fold(unicode_plane, |acc, x| acc | x);
char::from_u32(unicode).unwrap()
}
Expand All @@ -137,17 +136,17 @@ mod tests {
fn chars_to_dots_test() {
assert_eq!(
chars_to_dots("123"),
Ok(HashSet::from([
BrailleDot::Dot1,
BrailleDot::Dot2,
Ok(enum_set!(
BrailleDot::Dot1 |
BrailleDot::Dot2 |
BrailleDot::Dot3
]))
))
);
assert_eq!(
chars_to_dots("1a"),
Ok(HashSet::from([BrailleDot::Dot1, BrailleDot::DotA,]))
Ok(enum_set!(BrailleDot::Dot1 | BrailleDot::DotA))
);
assert_eq!(chars_to_dots("a"), Ok(HashSet::from([BrailleDot::DotA,])));
assert_eq!(chars_to_dots("a"), Ok(enum_set!(BrailleDot::DotA)));
assert_eq!(
chars_to_dots("z"),
Err(ParseError::InvalidBraille {
Expand All @@ -161,8 +160,8 @@ mod tests {
assert_eq!(
braille_chars("1-1"),
Ok(vec![
HashSet::from([BrailleDot::Dot1]),
HashSet::from([BrailleDot::Dot1])
enum_set!(BrailleDot::Dot1),
enum_set!(BrailleDot::Dot1)
])
);
assert_eq!(
Expand All @@ -186,11 +185,11 @@ mod tests {
#[test]
fn dots_to_unicode_test() {
assert_eq!(
dots_to_unicode(&vec![HashSet::from([BrailleDot::Dot1, BrailleDot::Dot8])]),
dots_to_unicode(&vec![enum_set!(BrailleDot::Dot1 | BrailleDot::Dot8)]),
"⢁".to_string()
);
assert_eq!(
dots_to_unicode(&vec![HashSet::from([BrailleDot::Dot1, BrailleDot::Dot9])]),
dots_to_unicode(&vec![enum_set!(BrailleDot::Dot1 | BrailleDot::Dot9)]),
"\u{f0101}".to_string()
);
}
Expand Down
14 changes: 8 additions & 6 deletions src/parser/multipass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ impl<'a> TestParser<'a> {

#[cfg(test)]
mod tests {
use enumset::enum_set;

use super::braille::BrailleDot;
use super::*;

Expand All @@ -397,11 +399,11 @@ mod tests {
assert_eq!(
TestParser::new("@123").dots(),
Ok(TestInstruction::Dots {
dots: vec![HashSet::from([
BrailleDot::Dot1,
BrailleDot::Dot2,
dots: vec![enum_set!(
BrailleDot::Dot1 |
BrailleDot::Dot2 |
BrailleDot::Dot3
])]
)]
})
);
assert_eq!(
Expand All @@ -420,8 +422,8 @@ mod tests {
TestParser::new("@1-2").dots(),
Ok(TestInstruction::Dots {
dots: vec![
HashSet::from([BrailleDot::Dot1]),
HashSet::from([BrailleDot::Dot2])
enum_set!(BrailleDot::Dot1),
enum_set!(BrailleDot::Dot2)
]
})
);
Expand Down

0 comments on commit 1ffa1a4

Please sign in to comment.