From 22b57d5ad9a233c79bbe4cecfddd0b7271b19b34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Sun, 27 Dec 2015 18:19:13 +0100 Subject: [PATCH] Implement FromStr for FileIDType --- src/storage/file/id.rs | 5 +++-- src/storage/file/id_type.rs | 15 +++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/storage/file/id.rs b/src/storage/file/id.rs index 913c38c9..36f40cdb 100644 --- a/src/storage/file/id.rs +++ b/src/storage/file/id.rs @@ -4,6 +4,7 @@ use std::fmt::{Debug, Display, Formatter}; use std::fmt; use std::path::PathBuf; use std::result::Result; +use std::str::FromStr; use regex::Regex; @@ -58,12 +59,12 @@ impl FileID { debug!(" Hash Name: {:?}", hashname); debug!(" Hash: {:?}", hash); - FileIDType::parse(hashname).map(|idtype| { + FileIDType::from_str(hashname).map(|idtype| { Some(FileID { id: FileHash::from(hash), id_type: idtype, }) - }) + }).ok() }).unwrap_or({ debug!("Did not match"); debug!("It is no path, actually. So we assume it is an ID already"); diff --git a/src/storage/file/id_type.rs b/src/storage/file/id_type.rs index 91fc8858..d13c8bab 100644 --- a/src/storage/file/id_type.rs +++ b/src/storage/file/id_type.rs @@ -1,5 +1,6 @@ use std::convert::{From, Into}; use std::error::Error; +use std::str::FromStr; #[derive(Debug)] #[derive(Clone)] @@ -10,12 +11,18 @@ pub enum FileIDType { UUID, } -impl FileIDType { +pub enum FileIDTypeParseError { + UnknownType +} - pub fn parse(s: &str) -> Option { - unimplemented!() +impl FromStr for FileIDType { + type Err = FileIDTypeParseError; + fn from_str(s: &str) -> Result { + match s { + "UUID" => Ok(FileIDType::UUID), + _ => Err(FileIDTypeParseError::UnknownType) + } } - } impl Into for FileIDType {