use crate::simple_impl::simple_state::KeyProvidingData;
pub fn char_matcher(c: char) -> impl Fn(&char) -> bool {
move |k: &char| *k == c
}
pub fn enumerated_char_matcher(c: char) -> impl Fn(&(usize, char)) -> bool {
move |k: &(usize, char)| k.1 == c
}
pub fn char_matcher_reversed(c: char) -> impl Fn(&char) -> bool {
move |k: &char| *k != c
}
macro_rules! unwrap_result {
($expression:expr) => {
if let Ok(x) = $expression {
x
} else {
panic!("Result was expected to be Ok(x) but was Err(x)");
}
};
}
pub(crate) use unwrap_result;
pub struct SingleValueTestData<T: Copy> {
value: T,
}
impl <T: Copy> SingleValueTestData<T> {
pub fn new(value: T) -> Self {
Self { value }
}
}
impl <T: Copy> KeyProvidingData<T> for SingleValueTestData<T> {
fn next_key(&mut self) -> Option<T> {
Option::Some(self.value)
}
}
pub struct CopiedTestData<T> {
iter: usize,
values: Vec<T>,
}
impl <T: Copy> CopiedTestData<T> {
pub fn new(values: Vec<T>) -> Self {
Self { iter: 0, values }
}
}
impl <T: Copy> KeyProvidingData<T> for CopiedTestData<T> {
fn next_key(&mut self) -> Option<T> {
let ret = self.values.get(self.iter);
self.iter += 1;
if let Some(c) = ret {
return Option::Some(*c)
}
Option::None
}
}
pub struct MatchListCopiedTestData<T> {
iter: usize,
values: Vec<T>,
matches: Vec<usize>,
}
impl <T: Copy> MatchListCopiedTestData<T> {
pub fn new(values: Vec<T>) -> Self {
Self { iter: 0, values, matches: Vec::new() }
}
pub fn set_match(&mut self, match_end: usize) -> () {
self.matches.push(match_end);
}
pub fn matches(&self) -> &Vec<usize> {
&self.matches
}
}
impl <T: Copy> KeyProvidingData<(usize, T)> for MatchListCopiedTestData<T> {
fn next_key(&mut self) -> Option<(usize,T)> {
let current_iter = self.iter;
let ret = self.values.get(self.iter);
self.iter += 1;
if let Some(c) = ret {
return Option::Some((current_iter,*c))
}
Option::None
}
}