add error if command not found

This commit is contained in:
rick 2023-04-22 15:12:27 +02:00
parent bec4429c3a
commit f9bb10d104
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA
2 changed files with 15 additions and 29 deletions

View file

@ -136,17 +136,17 @@ fn parse_without_addr(c: char) -> Command {
}
*/
fn parse(c: char, addr: Address) -> Command {
fn parse(c: char, addr: Address) -> Result<Command, String> {
match c {
'p' => Command::Print(addr),
'n' => Command::Number(addr),
'q' => Command::Quit(false),
'Q' => Command::Quit(true),
_ => Command::Help,
'p' => Ok(Command::Print(addr)),
'n' => Ok(Command::Number(addr)),
'q' => Ok(Command::Quit(false)),
'Q' => Ok(Command::Quit(true)),
_ => Ok(Command::Help),
}
}
pub fn parse_command(c: char, addr: Address) -> Command {
pub fn parse_command(c: char, addr: Address) -> Result<Command, String> {
parse(c, addr)
/*
if let Some(a) = addr {

View file

@ -33,20 +33,6 @@ fn read_address(input: &str) -> Result<&str, Address> {
* Le char doit être une commande valide.
*/
/*
fn parse_multi_addr(addr: &str) -> Address {
//let extract = take_till::<&str, &str, Error>(|c| c == ',');
let extract = take_till::<&str, &str, Error<&str>>(|c| c != ",");
if let Ok((still, before)) = extract(addr) {
if let Ok((still, after)) = extract(addr) {
}
}
Address::Null
}
*/
fn check_address(c: char) -> bool {
c.is_digit(10) || c == '$' || c == ' ' || c == '.'
}
@ -76,13 +62,11 @@ fn parse_line(input: &str) -> Result<Command, String> {
let tmp = (address, command).parse(input);
if let Ok((input, (a, c))) = tmp {
let addr = parse_address(a);
Ok(parse_command(c, addr))
parse_command(c, addr)
} else if let Err(Error((_, t))) = tmp {
println!("?");
Err(String::from("bruh moment"))
Err(String::from("Erreur de parsing"))
} else {
println!("bruh");
Err(String::from("bruh moment"))
Err(String::from("Grosse erreur"))
}
}
@ -103,13 +87,15 @@ fn main() {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(n) => {
if let Ok(c) = parse_line(&input) {
let result_parse = parse_line(&input);
if let Ok(c) = result_parse {
let tmp = execute_command(&mut buffer_struct, c);
if tmp {
quit = true;
}
} else {
//println!("coucou");
} else if let Err(e) = result_parse {
println!("?");
println!("{}", e);
}
},
Err(e) => println!("error"),