Add converter

This commit is contained in:
Rico Riedel 2022-07-31 16:46:09 +02:00
parent a42d141e6a
commit 1f03a8fd13
No known key found for this signature in database
GPG Key ID: 75AC868575DE7B18
6 changed files with 101 additions and 4 deletions

View File

@ -1,3 +1,4 @@
#[cfg_attr(test, mockall::automock)]
pub trait CharConverter { pub trait CharConverter {
fn convert(&self, level: f32) -> char; fn convert(&self, level: f32) -> char;
} }

View File

@ -1,5 +1,6 @@
use crossterm::style::Color; use crossterm::style::Color;
#[cfg_attr(test, mockall::automock)]
pub trait ColorConverter { pub trait ColorConverter {
fn convert(&self, level: f32) -> Color; fn convert(&self, level: f32) -> Color;
} }

View File

@ -1,9 +1,11 @@
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Level { pub enum Level {
Keep, Keep,
Draw(f32), Draw(f32),
Clear, Clear,
} }
#[cfg_attr(test, mockall::automock)]
pub trait LevelConverter { pub trait LevelConverter {
fn convert(&self, level: f32) -> Level; fn convert(&self, level: f32) -> Level;
} }

96
src/convert/mod.rs Normal file
View File

@ -0,0 +1,96 @@
use crate::convert::char::CharConverter;
use crate::convert::color::ColorConverter;
use crate::convert::level::{Level, LevelConverter};
use crossterm::style::Color;
mod char;
mod color;
mod level;
pub trait Converter {
fn char(&self, level: f32) -> char;
fn color(&self, level: f32) -> Color;
fn level(&self, level: f32) -> Level;
}
pub struct ConverterImpl<T1, T2, T3> {
char: T1,
color: T2,
level: T3,
}
impl<T1, T2, T3> ConverterImpl<T1, T2, T3> {
pub fn new(char: T1, color: T2, level: T3) -> Self {
Self { char, color, level }
}
}
impl<T1: CharConverter, T2: ColorConverter, T3: LevelConverter> Converter
for ConverterImpl<T1, T2, T3>
{
fn char(&self, level: f32) -> char {
self.char.convert(level)
}
fn color(&self, level: f32) -> Color {
self.color.convert(level)
}
fn level(&self, level: f32) -> Level {
self.level.convert(level)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::convert::char::MockCharConverter;
use crate::convert::color::MockColorConverter;
use crate::convert::level::MockLevelConverter;
use mockall::predicate::eq;
#[test]
fn char() {
let mut char = MockCharConverter::new();
let color = MockColorConverter::new();
let level = MockLevelConverter::new();
char.expect_convert().with(eq(4.0)).return_const('M');
let converter = ConverterImpl::new(char, color, level);
assert_eq!('M', converter.char(4.0));
}
#[test]
fn color() {
let char = MockCharConverter::new();
let mut color = MockColorConverter::new();
let level = MockLevelConverter::new();
color
.expect_convert()
.with(eq(2.0))
.return_const(Color::Yellow);
let converter = ConverterImpl::new(char, color, level);
assert_eq!(Color::Yellow, converter.color(2.0));
}
#[test]
fn level() {
let char = MockCharConverter::new();
let color = MockColorConverter::new();
let mut level = MockLevelConverter::new();
level
.expect_convert()
.with(eq(3.0))
.return_const(Level::Draw(2.0));
let converter = ConverterImpl::new(char, color, level);
assert_eq!(Level::Draw(2.0), converter.level(3.0));
}
}

View File

@ -1,7 +1,7 @@
pub mod error; pub mod error;
pub mod pattern; pub mod pattern;
pub mod printer; pub mod printer;
pub mod sampler; pub mod convert;
pub mod term; pub mod term;
mod vec; mod vec;

View File

@ -1,3 +0,0 @@
mod color;
mod level;
mod char;