let version fail gracefully

This commit is contained in:
Max Bradbury 2020-06-18 19:56:33 +01:00
parent 97cf500a8a
commit b468c680b3
1 changed files with 15 additions and 6 deletions

View File

@ -55,13 +55,19 @@ pub struct Version {
pub minor: u8,
}
#[derive(Debug)]
pub struct InvalidVersion;
impl Version {
fn from(str: &str) -> Version {
fn from(str: &str) -> Result<Version, InvalidVersion> {
let parts: Vec<&str> = str.split(".").collect();
assert_eq!(parts.len(), 2);
Version {
major: parts[0].parse().unwrap(),
minor: parts[1].parse().unwrap(),
if parts.len() == 2 {
Ok(Version {
major: parts[0].parse().unwrap(),
minor: parts[1].parse().unwrap(),
})
} else {
Err (InvalidVersion)
}
}
}
@ -142,7 +148,10 @@ impl Game {
for segment in segments {
if segment.starts_with("# BITSY VERSION") {
let segment = segment.replace("# BITSY VERSION ", "");
version = Some(Version::from(&segment));
let segment = Version::from(&segment);
if segment.is_ok() {
version = Some(segment.unwrap());
}
} else if segment.starts_with("! ROOM_FORMAT") {
let segment = segment.replace("! ROOM_FORMAT ", "");
room_format = Some(RoomFormat::from(&segment).unwrap());