Skip to content

Instantly share code, notes, and snippets.

@snoyberg
Created October 23, 2018 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snoyberg/568899dc3ae6c82e54809efe283e4473 to your computer and use it in GitHub Desktop.
Save snoyberg/568899dc3ae6c82e54809efe283e4473 to your computer and use it in GitHub Desktop.
#[derive(Debug)]
struct Frame {
width: u32,
height: u32,
}
#[derive(Debug)]
enum ParseError {
TooFewArgs,
TooManyArgs,
InvalidInteger(String),
}
fn parse_args() -> Result<Frame, ParseError> {
use self::ParseError::*;
let mut args = std::env::args().skip(1);
let width_str = match args.next() {
None => return Err(TooFewArgs),
Some(width_str) => width_str,
};
let height_str = match args.next() {
None => return Err(TooFewArgs),
Some(height_str) => height_str,
};
match args.next() {
Some(_) => return Err(TooManyArgs),
None => (),
}
let width = match width_str.parse() {
Err(_) => return Err(InvalidInteger(width_str)),
Ok(width) => width,
};
let height = match height_str.parse() {
Err(_) => return Err(InvalidInteger(height_str)),
Ok(height) => height,
};
Ok(Frame {
width,
height,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment