Compare commits

...

2 commits

Author SHA1 Message Date
3a00c4bb09 fix all errors in pattern in pattern 2021-12-02 19:01:16 -05:00
1f155bd231 fixes 2021-12-02 18:48:05 -05:00

View file

@ -6,16 +6,17 @@ use std::{
type ParseError = Box<dyn std::error::Error>;
trait Pattern<'a> {
pub fn execute(&self, values: &'a mut [RawColor]);
pub fn parse(args: Vec<String>) -> Result<Self, ParseError>;
pub trait Pattern<'a> {
fn execute(&self, values: &'a mut [RawColor]);
fn parse(args: Vec<String>) -> Result<Self, ParseError> where Self: Sized;
}
impl<'a> TryFrom<Vec<String>> for Box<dyn Pattern<'a>> {
fn try_from(s: Vec<String>) -> Result<Self, ParseError> {
type Error = ParseError;
fn try_from(s: Vec<String>) -> Result<Self, Self::Error> {
match s[0].as_str() {
"unit" => Unit::parse(s),
"val" => Value::parse(s),
"unit" => Ok(Box::new(Unit::parse(s).unwrap()) as Box<dyn Pattern>),
"val" => Ok(Box::new(Value::parse(s).unwrap()) as Box<dyn Pattern>),
_ => Err("No Match".into())
}
}
@ -87,7 +88,7 @@ impl<'a> Pattern<'a> for Value {
})
}
},
Err(e) => Err(e)
Err(e) => Err(Box::new(e))
}
}
}
@ -96,4 +97,4 @@ impl<'a> Pattern<'a> for Value {
pub fn parse_line(v: Vec<String>) -> Result<Box<dyn Pattern<'static>>, ParseError> {
let res: Result<Box<dyn Pattern>, ParseError> = v.try_into();
res
}
}