use macro rule for default implementation of pattern

This commit is contained in:
Tobias Ollive 2022-03-13 10:16:40 +01:00
parent 20b8f6d4db
commit 13326a49d9
12 changed files with 69 additions and 104 deletions

9
Cargo.lock generated
View File

@ -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",

View File

@ -12,6 +12,7 @@ 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"

View File

@ -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};

View File

@ -49,10 +49,36 @@ pub(crate) enum Patterns<const N: usize> {
pub trait Pattern: Iterator {
type Strip;
fn init(&mut self) -> Option<Self::Strip>;
fn init(&mut self) -> Option<Self::Strip> {
None
}
fn is_last_iteration(&self) -> bool ;
}
#[macro_export]
macro_rules! impl_pattern_none {
($name:ty, $generic: tt) => {
type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
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<const N: usize> Iterator for Patterns<N> {
type Item = Strip<N>;

View File

@ -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<const N: usize> {
}
impl<const N: usize> Pattern for Blink<N> {
type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
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<const N: usize> Iterator for Blink<N> {

View File

@ -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<const N: usize> {
}
impl<const N: usize> Pattern for FadeRandom<N> {
type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
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<const N: usize> Pattern for FadeRandom<N> {
// type Strip = Strip<N>;
//
// 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<const N: usize> Iterator for FadeRandom<N> {
type Item = Strip<N>;

View File

@ -18,10 +18,6 @@ pub struct FadeUnstable<const N: usize> {
impl<const N: usize> Pattern for FadeUnstable<N> {
type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
None
}
fn is_last_iteration(&self) -> bool {
match self.max_iteration {
None => false,

View File

@ -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<const N: usize> {
}
impl<const N: usize> Pattern for FillRandom<N> {
type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
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<const N: usize> Iterator for FillRandom<N> {

View File

@ -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<const N: usize> {
}
impl<const N: usize> Pattern for FillUnstable<N> {
type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
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<const N: usize> Iterator for FillUnstable<N> {

View File

@ -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<const N: usize> {
}
impl<const N: usize> Pattern for Rainbow<N> {
type Strip = Strip<N>;
fn init(&mut self) -> Option<Self::Strip> {
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<const N: usize> Iterator for Rainbow<N> {

View File

@ -20,7 +20,7 @@ impl<const N: usize> Pattern for Ring<N> {
fn init(&mut self) -> Option<Self::Strip> {
None
}
} //FIXME
fn is_last_iteration(&self) -> bool {
match self.max_iteration {

View File

@ -20,7 +20,7 @@ impl<const N: usize> Pattern for RingScanner<N> {
fn init(&mut self) -> Option<Self::Strip> {
None
}
} //FIXME
fn is_last_iteration(&self) -> bool {
match self.max_iteration {