args checking

This commit is contained in:
jane 2021-12-28 17:49:16 -05:00
parent 1f55bb8c31
commit 6d58bafa02
2 changed files with 42 additions and 12 deletions

View file

@ -35,7 +35,7 @@ fn main() {
//pattern parsing //pattern parsing
let mul_string = format_multiline(concat!( let mul_string = format_multiline(concat!(
"val 0 0 255", "val 0 0 255\n",
"val r 255" "val r 255"
)); ));
let res: Result<Vec<Box<dyn Pattern>>, ParseError> = mul_string.iter() let res: Result<Vec<Box<dyn Pattern>>, ParseError> = mul_string.iter()
@ -44,6 +44,9 @@ fn main() {
if res.is_ok() { if res.is_ok() {
pattern = res.unwrap(); pattern = res.unwrap();
} }
else {
println!("ret err {:?}", res.unwrap_err());
}
//pattern management //pattern management
loop { loop {

View file

@ -54,19 +54,43 @@ impl Pattern for Value {
} }
} }
fn parse(args: Vec<String>) -> Result<Self, ParseError> { fn parse(args: Vec<String>) -> Result<Self, ParseError> {
let param1 = args[1].parse::<u8>(); let param1 =
let param2 = args[2].parse::<u8>(); if args.len() >= 1 {
let param3 = args[3].parse::<u8>(); match args[1].parse::<u8>() {
if param1.is_ok() && param2.is_ok() && param3.is_ok() { Ok(i) => Some(i),
Err(_) => None
}
} else {
None
}
let param2 =
if args.len() >= 2 {
match args[2].parse::<u8>() {
Ok(i) => Some(i),
Err(_) => None
}
} else {
None
}
let param3 =
if args.len() >= 3 {
match args[3].parse::<u8>() {
Ok(i) => Some(i),
Err(_) => None
}
} else {
None
}
if param1.is_some() && param2.is_some() && param3.is_some() {
Ok(Value{ Ok(Value{
r: Some(param1.unwrap()), r: param1,
g: Some(param2.unwrap()), g: param2,
b: Some(param3.unwrap()) b: param3
}) })
} }
else { else if args.len() >= 2 {
match param2 { match param2 {
Ok(i) => match args[1].as_str() { Some(i) => match args[1].as_str() {
"r" => { "r" => {
Ok(Value { Ok(Value {
r: Some(i), r: Some(i),
@ -88,11 +112,14 @@ impl Pattern for Value {
b: Some(i) b: Some(i)
}) })
} }
_ => Err("unreachable".into()) _ => Err("no rgb".into())
}, },
Err(e) => Err(Box::new(e)) None => Err("no param2".into())
} }
} }
else {
Err("incorrect number of arguments".into())
}
} }
} }