So, this is utterly baffling to me and I can't figure out why Rust has decided to do this. I have this function declaration, which seems perfectly valid to me:
pub fn parse_command(command_string: String) -> Option<ParsedCommand> {
Yet, for some reason, I get this when trying to build it:
error[E0308]: mismatched types
--> src\command_parser.rs:63:1
|
63 | None
| ^^^^ expected (), found enum `std::option::Option`
|
= note: expected type `()`
found type `std::option::Option<_>`
What is making the compiler do this, and is there (anyway) way I can permanently prevent this from happening? The entire function is below:
pub fn parse_command(command_string: String) -> Option<ParsedCommand> {
let mut cmd_iter = command_string.split_whitespace();
let (_, mut upper) = cmd_iter.size_hint();
// Verify that we indeed do have a fully interpretable command string
if upper.is_none() {
None
}
let upper = upper.unwrap();
if upper == 0 {
None
}
let cmd = cmd_iter.next().unwrap();
let mut args: Vec<Vec<u8>> = Vec::new();
let mut count: usize = 0;
let mut hash: Vec<u8> = Vec::new();
loop {
let mut arg = cmd_iter.next();
if arg.is_none() {
break;
}
let arg = arg.unwrap();
count += 1;
// What to do with this argument?
if count == upper {
// This is a hash. We don't include it in the arg list.
match BASE64.decode(arg.as_bytes()) {
Ok(h) => {
hash = h;
break;
}
Err(_) => return None,
}
} else {
// This is a command argument
match BASE64.decode(arg.as_bytes()) {
Ok(a) => args.push(a),
Err(_) => return None,
}
}
}
// Before we do anything else, confirm that this hash is valid.
let mut buf: Vec<u8> = Vec::new();
for arg in args.iter() {
buf.reserve_exact(arg.len());
buf.extend(arg);
buf.push(0x20);
}
if buf.ends_with(&[0x20]) {
buf.pop();
buf.shrink_to_fit();
}
let mut generated_hash = [0u8; 64];
hash_xof(MessageDigest::shake_256(), buf.as_slice(), &mut hash).unwrap();
if hash != generated_hash.to_vec() {
// Invalid hash, abort command parsing
None
}
// Return this data, we're done
Some(ParsedCommand {
command: cmd.to_owned(),
args: args,
})
}