use crate::patterns::Patterns; use crate::Strip; use serde_derive::{Deserialize, Serialize}; use std::fs; use std::time::Duration; use tokio::sync::watch::Sender; #[derive(Serialize, Deserialize, Debug)] pub struct Context { pub(crate) pattern: Patterns, pub period: Duration, } impl IntoIterator for Runner { type Item = Context; type IntoIter = > as IntoIterator>::IntoIter; // so that you don't have to write std::vec::IntoIter, which nobody remembers anyway fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } } #[derive(Serialize, Deserialize, Debug)] pub(crate) struct Runner(Vec>); impl Runner { pub fn new() -> Runner { let runner = Vec::>::new(); Runner(runner) } pub fn push(&mut self, value: Context) { self.0.push(value) } pub async fn runner_loop(self, tx: Sender>) { for mut context in self { let mut strip = context.pattern.next().unwrap(); let delay = context.period; println!("{:?}", delay); while tx.send(strip).is_ok() { if let Some(value) = context.pattern.next() { strip = value; } else { break; } tokio::time::sleep(delay).await; } } } pub fn load_from_file(file: &std::path::Path) -> Runner { let file = fs::read_to_string(file).unwrap(); let spectacle: Runner = serde_yaml::from_str(&*file).unwrap(); spectacle } }