Created
October 6, 2015 07:20
-
-
Save anonymous/85a457f11e2633590829 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::str::FromStr; | |
#[derive(Debug)] | |
enum Version { Version1, Version2 } | |
#[derive(Debug)] | |
enum ParseError { InvalidSyntax, UnsupportedVersion } | |
fn parse_version(line: String) -> Result<Version, ParseError> { | |
let mut split = line.split("="); | |
if split.clone().count() != 2 { | |
// old config without version number, assume Version1 | |
return Ok(Version::Version1); | |
} | |
if split.next().unwrap() != "version" { | |
return Err(ParseError::InvalidSyntax); | |
} | |
let version = match i32::from_str(split.next().unwrap()).unwrap() { | |
1 => Version::Version1, | |
2 => Version::Version2, | |
_ => return Err(ParseError::UnsupportedVersion), | |
}; | |
Ok(version) | |
} | |
fn main() { | |
println!("{:?}", parse_version(String::from("something"))); | |
println!("{:?}", parse_version(String::from("version=1"))); | |
println!("{:?}", parse_version(String::from("version=2"))); | |
println!("{:?}", parse_version(String::from("version=3"))); | |
println!("{:?}", parse_version(String::from("something=3"))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment