From 13326a49d91f56a100d10ca65631bf1ebb76af8a Mon Sep 17 00:00:00 2001 From: Tobias Ollive Date: Sun, 13 Mar 2022 10:16:40 +0100 Subject: [PATCH] use macro rule for default implementation of pattern --- Cargo.lock | 9 +++++++++ Cargo.toml | 3 ++- src/main.rs | 1 + src/patterns.rs | 30 +++++++++++++++++++++++++-- src/patterns/blink.rs | 21 ++----------------- src/patterns/fade_random.rs | 38 +++++++++++++++++------------------ src/patterns/fade_unstable.rs | 4 ---- src/patterns/fill_random.rs | 21 ++----------------- src/patterns/fill_unstable.rs | 21 ++----------------- src/patterns/rainbow.rs | 21 ++----------------- src/patterns/ring.rs | 2 +- src/patterns/ring_scanner.rs | 2 +- 12 files changed, 69 insertions(+), 104 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8631403..c14c7ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -920,6 +920,14 @@ dependencies = [ "winapi", ] +[[package]] +name = "pattern_derive" +version = "0.1.0" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "peeking_take_while" version = "0.1.2" @@ -1096,6 +1104,7 @@ dependencies = [ "bluer", "clap", "futures", + "pattern_derive", "rand", "rodio", "serde", diff --git a/Cargo.toml b/Cargo.toml index cd51058..ad9bb5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,8 @@ futures = "0.3.19" rand = "0.8.5" rodio = "0.15.0" clap = { version = "3.1.6", features = ["derive"] } +pattern_derive = { path = "pattern_derive" } serde_derive = "1.0.136" serde = "1.0.136" -serde_yaml = "0.8" \ No newline at end of file +serde_yaml = "0.8" diff --git a/src/main.rs b/src/main.rs index 29dbfea..2d67df6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod patterns; mod runner; mod spectacle; +use pattern_derive; use crate::args::Cli; use crate::audio::play_sound; use crate::config::{load_from_file, Config}; diff --git a/src/patterns.rs b/src/patterns.rs index 022f667..f44f0e8 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -49,8 +49,34 @@ pub(crate) enum Patterns { pub trait Pattern: Iterator { type Strip; - fn init(&mut self) -> Option; - fn is_last_iteration(&self) -> bool; + fn init(&mut self) -> Option { + None + } + fn is_last_iteration(&self) -> bool ; +} + +#[macro_export] +macro_rules! impl_pattern_none { + ($name:ty, $generic: tt) => { + type Strip = Strip; + + fn init(&mut self) -> Option { + None + } + + fn is_last_iteration(&self) -> bool { + match self.max_iteration { + None => false, + Some(max_iter) => { + if self.current_iteration >= max_iter { + true + } else { + false + } + } + } + } + } } impl Iterator for Patterns { diff --git a/src/patterns/blink.rs b/src/patterns/blink.rs index fdf732b..06332bb 100644 --- a/src/patterns/blink.rs +++ b/src/patterns/blink.rs @@ -1,6 +1,6 @@ use crate::patterns::fade::Fade; use crate::patterns::Pattern; -use crate::Strip; +use crate::{impl_pattern_none, Strip}; use serde::{Deserialize, Serialize}; /// # Blink @@ -15,24 +15,7 @@ pub struct Blink { } impl Pattern for Blink { - type Strip = Strip; - - fn init(&mut self) -> Option { - None - } - - fn is_last_iteration(&self) -> bool { - match self.max_iteration { - None => false, - Some(max_iter) => { - if self.current_iteration >= max_iter { - true - } else { - false - } - } - } - } + impl_pattern_none!(FadeRandom, N); } impl Iterator for Blink { diff --git a/src/patterns/fade_random.rs b/src/patterns/fade_random.rs index bd2f363..a896e3b 100644 --- a/src/patterns/fade_random.rs +++ b/src/patterns/fade_random.rs @@ -1,6 +1,6 @@ use crate::patterns::fade::Fade; use crate::patterns::Pattern; -use crate::Strip; +use crate::{impl_pattern_none, Strip}; use serde::{Deserialize, Serialize}; /// # FadeRandom @@ -16,25 +16,25 @@ pub struct FadeRandom { } impl Pattern for FadeRandom { - type Strip = Strip; - - fn init(&mut self) -> Option { - None - } - - fn is_last_iteration(&self) -> bool { - match self.max_iteration { - None => false, - Some(max_iter) => { - if self.current_iteration >= max_iter { - true - } else { - false - } - } - } - } + impl_pattern_none!(FadeRandom, N); } +// +// impl Pattern for FadeRandom { +// type Strip = Strip; +// +// fn is_last_iteration(&self) -> bool { +// match self.max_iteration { +// None => false, +// Some(max_iter) => { +// if self.current_iteration >= max_iter { +// true +// } else { +// false +// } +// } +// } +// } +// } impl Iterator for FadeRandom { type Item = Strip; diff --git a/src/patterns/fade_unstable.rs b/src/patterns/fade_unstable.rs index 5455972..fbf2166 100644 --- a/src/patterns/fade_unstable.rs +++ b/src/patterns/fade_unstable.rs @@ -18,10 +18,6 @@ pub struct FadeUnstable { impl Pattern for FadeUnstable { type Strip = Strip; - fn init(&mut self) -> Option { - None - } - fn is_last_iteration(&self) -> bool { match self.max_iteration { None => false, diff --git a/src/patterns/fill_random.rs b/src/patterns/fill_random.rs index e12ee13..a294428 100644 --- a/src/patterns/fill_random.rs +++ b/src/patterns/fill_random.rs @@ -1,5 +1,5 @@ use crate::patterns::{Pattern, PixelColor}; -use crate::Strip; +use crate::{impl_pattern_none, Strip}; use serde::{Deserialize, Serialize}; /// # FillRandom @@ -15,24 +15,7 @@ pub struct FillRandom { } impl Pattern for FillRandom { - type Strip = Strip; - - fn init(&mut self) -> Option { - None - } - - fn is_last_iteration(&self) -> bool { - match self.max_iteration { - None => false, - Some(max_iter) => { - if self.current_iteration >= max_iter { - true - } else { - false - } - } - } - } + impl_pattern_none!(FadeRandom, N); } impl Iterator for FillRandom { diff --git a/src/patterns/fill_unstable.rs b/src/patterns/fill_unstable.rs index 209b6e1..6271628 100644 --- a/src/patterns/fill_unstable.rs +++ b/src/patterns/fill_unstable.rs @@ -1,5 +1,5 @@ use crate::patterns::{Pattern, PixelColor}; -use crate::Strip; +use crate::{impl_pattern_none, Strip}; use serde::{Deserialize, Serialize}; /// # FillUnstable @@ -15,24 +15,7 @@ pub struct FillUnstable { } impl Pattern for FillUnstable { - type Strip = Strip; - - fn init(&mut self) -> Option { - None - } - - fn is_last_iteration(&self) -> bool { - match self.max_iteration { - None => false, - Some(max_iter) => { - if self.current_iteration >= max_iter { - true - } else { - false - } - } - } - } + impl_pattern_none!(FadeRandom, N); } impl Iterator for FillUnstable { diff --git a/src/patterns/rainbow.rs b/src/patterns/rainbow.rs index fd89b4c..8e29973 100644 --- a/src/patterns/rainbow.rs +++ b/src/patterns/rainbow.rs @@ -1,5 +1,5 @@ use crate::patterns::{Pattern, PixelColor}; -use crate::Strip; +use crate::{impl_pattern_none, Strip}; use serde::{Deserialize, Serialize}; /// # Rainbow pattern @@ -21,24 +21,7 @@ pub struct Rainbow { } impl Pattern for Rainbow { - type Strip = Strip; - - fn init(&mut self) -> Option { - None - } - - fn is_last_iteration(&self) -> bool { - match self.max_iteration { - None => false, - Some(max_iter) => { - if self.current_iteration >= max_iter { - true - } else { - false - } - } - } - } + impl_pattern_none!(FadeRandom, N); } impl Iterator for Rainbow { diff --git a/src/patterns/ring.rs b/src/patterns/ring.rs index 90fa820..b07a943 100644 --- a/src/patterns/ring.rs +++ b/src/patterns/ring.rs @@ -20,7 +20,7 @@ impl Pattern for Ring { fn init(&mut self) -> Option { None - } + } //FIXME fn is_last_iteration(&self) -> bool { match self.max_iteration { diff --git a/src/patterns/ring_scanner.rs b/src/patterns/ring_scanner.rs index d2128a5..110f34c 100644 --- a/src/patterns/ring_scanner.rs +++ b/src/patterns/ring_scanner.rs @@ -20,7 +20,7 @@ impl Pattern for RingScanner { fn init(&mut self) -> Option { None - } + } //FIXME fn is_last_iteration(&self) -> bool { match self.max_iteration {