Implement FromStr for FileIDType

This commit is contained in:
Marcel Müller 2015-12-27 18:19:13 +01:00 committed by Matthias Beyer
parent 65aa98607e
commit 22b57d5ad9
2 changed files with 14 additions and 6 deletions

View File

@ -4,6 +4,7 @@ use std::fmt::{Debug, Display, Formatter};
use std::fmt; use std::fmt;
use std::path::PathBuf; use std::path::PathBuf;
use std::result::Result; use std::result::Result;
use std::str::FromStr;
use regex::Regex; use regex::Regex;
@ -58,12 +59,12 @@ impl FileID {
debug!(" Hash Name: {:?}", hashname); debug!(" Hash Name: {:?}", hashname);
debug!(" Hash: {:?}", hash); debug!(" Hash: {:?}", hash);
FileIDType::parse(hashname).map(|idtype| { FileIDType::from_str(hashname).map(|idtype| {
Some(FileID { Some(FileID {
id: FileHash::from(hash), id: FileHash::from(hash),
id_type: idtype, id_type: idtype,
}) })
}) }).ok()
}).unwrap_or({ }).unwrap_or({
debug!("Did not match"); debug!("Did not match");
debug!("It is no path, actually. So we assume it is an ID already"); debug!("It is no path, actually. So we assume it is an ID already");

View File

@ -1,5 +1,6 @@
use std::convert::{From, Into}; use std::convert::{From, Into};
use std::error::Error; use std::error::Error;
use std::str::FromStr;
#[derive(Debug)] #[derive(Debug)]
#[derive(Clone)] #[derive(Clone)]
@ -10,12 +11,18 @@ pub enum FileIDType {
UUID, UUID,
} }
impl FileIDType { pub enum FileIDTypeParseError {
UnknownType
}
pub fn parse(s: &str) -> Option<FileIDType> { impl FromStr for FileIDType {
unimplemented!() type Err = FileIDTypeParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"UUID" => Ok(FileIDType::UUID),
_ => Err(FileIDTypeParseError::UnknownType)
}
} }
} }
impl Into<String> for FileIDType { impl Into<String> for FileIDType {