2021-12-26 18:14:03 +00:00
|
|
|
use rs_ws281x::RawColor;
|
|
|
|
use std::{
|
|
|
|
vec::Vec,
|
|
|
|
string::String
|
|
|
|
};
|
|
|
|
|
|
|
|
type ParseError = Box<dyn std::error::Error>;
|
|
|
|
|
2021-12-27 01:26:29 +00:00
|
|
|
trait Pattern<'a> {
|
2021-12-27 02:13:11 +00:00
|
|
|
pub fn execute(&self, values: &'a mut [RawColor]);
|
2021-12-02 23:46:52 +00:00
|
|
|
pub fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized;
|
2021-12-26 18:14:03 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 01:26:29 +00:00
|
|
|
impl<'a> TryFrom<Vec<String>> for Box<dyn Pattern<'a>> {
|
2021-12-02 23:46:52 +00:00
|
|
|
type Error = ParseError;
|
|
|
|
fn try_from(s: Vec<String>) -> Result<Self, Error> {
|
2021-12-26 18:14:03 +00:00
|
|
|
match s[0].as_str() {
|
2021-12-27 01:26:29 +00:00
|
|
|
"unit" => Unit::parse(s),
|
|
|
|
"val" => Value::parse(s),
|
2021-12-26 18:14:03 +00:00
|
|
|
_ => Err("No Match".into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Unit;
|
2021-12-27 01:26:29 +00:00
|
|
|
impl<'a> Pattern<'a> for Unit {
|
2021-12-26 18:14:03 +00:00
|
|
|
fn execute(&self, values: &'a mut [RawColor]) {}
|
|
|
|
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
|
|
|
|
Ok(Unit {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Value {
|
|
|
|
r: Option<u8>,
|
|
|
|
g: Option<u8>,
|
|
|
|
b: Option<u8>
|
2021-12-27 01:26:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Pattern<'a> for Value {
|
2021-12-26 18:14:03 +00:00
|
|
|
fn execute(&self, values: &'a mut [RawColor]) {
|
|
|
|
for i in 0..crate::LED_SIZE {
|
|
|
|
let color: RawColor = values[i];
|
|
|
|
if self.r.is_some() {
|
|
|
|
color[0] = self.r.unwrap();
|
|
|
|
}
|
|
|
|
if self.g.is_some() {
|
|
|
|
color[0] = self.g.unwrap();
|
|
|
|
}
|
|
|
|
if self.b.is_some() {
|
|
|
|
color[0] = self.b.unwrap();
|
|
|
|
}
|
2021-12-27 02:13:11 +00:00
|
|
|
values[i] = color;
|
2021-12-26 18:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
|
2021-12-27 01:26:29 +00:00
|
|
|
let param1 = args[1].parse::<u8>();
|
|
|
|
let param2 = args[2].parse::<u8>();
|
|
|
|
let param3 = args[2].parse::<u8>();
|
2021-12-26 18:14:03 +00:00
|
|
|
if param1.is_ok() && param2.is_ok() && param3.is_ok() {
|
|
|
|
Ok(Value{
|
2021-12-27 02:13:11 +00:00
|
|
|
r: Some(param1.unwrap()),
|
|
|
|
g: Some(param2.unwrap()),
|
|
|
|
b: Some(param3.unwrap())
|
2021-12-26 18:14:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
match param2 {
|
2021-12-27 01:26:29 +00:00
|
|
|
Ok(i) => match args[1].as_str() {
|
2021-12-26 18:14:03 +00:00
|
|
|
"r" => {
|
2021-12-27 01:26:29 +00:00
|
|
|
Ok(Value {
|
2021-12-26 18:14:03 +00:00
|
|
|
r: Some(i),
|
|
|
|
g: None,
|
|
|
|
b: None
|
|
|
|
})
|
|
|
|
},
|
|
|
|
"g" => {
|
|
|
|
Ok(Value {
|
|
|
|
r: None,
|
|
|
|
g: Some(i),
|
|
|
|
b: None
|
|
|
|
})
|
|
|
|
},
|
|
|
|
"b" => {
|
2021-12-27 01:26:29 +00:00
|
|
|
Ok(Value {
|
2021-12-26 18:14:03 +00:00
|
|
|
r: None,
|
|
|
|
g: None,
|
|
|
|
b: Some(i)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
},
|
2021-12-27 02:13:11 +00:00
|
|
|
Err(e) => Err(e)
|
2021-12-26 18:14:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 01:26:29 +00:00
|
|
|
pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern<'static>>, ParseError> {
|
|
|
|
let res: Result<Box<dyn Pattern>, ParseError> = v.try_into();
|
|
|
|
res
|
2021-12-02 23:46:52 +00:00
|
|
|
}
|