From 1f03a8fd1355e55d595572f0b7a5c7ecc8ddc46f Mon Sep 17 00:00:00 2001 From: Rico Riedel Date: Sun, 31 Jul 2022 16:46:09 +0200 Subject: [PATCH] Add converter --- src/{sampler => convert}/char.rs | 1 + src/{sampler => convert}/color.rs | 1 + src/{sampler => convert}/level.rs | 2 + src/convert/mod.rs | 96 +++++++++++++++++++++++++++++++ src/main.rs | 2 +- src/sampler/mod.rs | 3 - 6 files changed, 101 insertions(+), 4 deletions(-) rename src/{sampler => convert}/char.rs (97%) rename src/{sampler => convert}/color.rs (97%) rename src/{sampler => convert}/level.rs (94%) create mode 100644 src/convert/mod.rs delete mode 100644 src/sampler/mod.rs diff --git a/src/sampler/char.rs b/src/convert/char.rs similarity index 97% rename from src/sampler/char.rs rename to src/convert/char.rs index 05de15b..3f2fbfe 100644 --- a/src/sampler/char.rs +++ b/src/convert/char.rs @@ -1,3 +1,4 @@ +#[cfg_attr(test, mockall::automock)] pub trait CharConverter { fn convert(&self, level: f32) -> char; } diff --git a/src/sampler/color.rs b/src/convert/color.rs similarity index 97% rename from src/sampler/color.rs rename to src/convert/color.rs index 30d064e..94c5cbf 100644 --- a/src/sampler/color.rs +++ b/src/convert/color.rs @@ -1,5 +1,6 @@ use crossterm::style::Color; +#[cfg_attr(test, mockall::automock)] pub trait ColorConverter { fn convert(&self, level: f32) -> Color; } diff --git a/src/sampler/level.rs b/src/convert/level.rs similarity index 94% rename from src/sampler/level.rs rename to src/convert/level.rs index d833903..72f5bca 100644 --- a/src/sampler/level.rs +++ b/src/convert/level.rs @@ -1,9 +1,11 @@ +#[derive(Copy, Clone, PartialEq, Debug)] pub enum Level { Keep, Draw(f32), Clear, } +#[cfg_attr(test, mockall::automock)] pub trait LevelConverter { fn convert(&self, level: f32) -> Level; } diff --git a/src/convert/mod.rs b/src/convert/mod.rs new file mode 100644 index 0000000..87d0616 --- /dev/null +++ b/src/convert/mod.rs @@ -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 { + char: T1, + color: T2, + level: T3, +} + +impl ConverterImpl { + pub fn new(char: T1, color: T2, level: T3) -> Self { + Self { char, color, level } + } +} + +impl Converter + for ConverterImpl +{ + 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)); + } +} diff --git a/src/main.rs b/src/main.rs index 10a4973..d1ae43b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ pub mod error; pub mod pattern; pub mod printer; -pub mod sampler; +pub mod convert; pub mod term; mod vec; diff --git a/src/sampler/mod.rs b/src/sampler/mod.rs deleted file mode 100644 index a521828..0000000 --- a/src/sampler/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod color; -mod level; -mod char;