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

View file

@ -54,19 +54,43 @@ impl Pattern for Value {
}
}
fn parse(args: Vec<String>) -> Result<Self, ParseError> {
let param1 = args[1].parse::<u8>();
let param2 = args[2].parse::<u8>();
let param3 = args[3].parse::<u8>();
if param1.is_ok() && param2.is_ok() && param3.is_ok() {
let param1 =
if args.len() >= 1 {
match args[1].parse::<u8>() {
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{
r: Some(param1.unwrap()),
g: Some(param2.unwrap()),
b: Some(param3.unwrap())
r: param1,
g: param2,
b: param3
})
}
else {
else if args.len() >= 2 {
match param2 {
Ok(i) => match args[1].as_str() {
Some(i) => match args[1].as_str() {
"r" => {
Ok(Value {
r: Some(i),
@ -88,11 +112,14 @@ impl Pattern for Value {
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())
}
}
}