From b468c680b344b990c6e09240e92c487e35e22703 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Thu, 18 Jun 2020 19:56:33 +0100 Subject: [PATCH] let version fail gracefully --- src/game.rs | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/game.rs b/src/game.rs index ea71f20..65c5c30 100644 --- a/src/game.rs +++ b/src/game.rs @@ -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 { 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());