diff --git a/src/storage/file_id.rs b/src/storage/file_id.rs index 5b2be2d8..03e507e0 100644 --- a/src/storage/file_id.rs +++ b/src/storage/file_id.rs @@ -6,10 +6,15 @@ use std::path::{Path, PathBuf}; use std::convert::From; use std::convert::Into; +use regex::Regex; + #[derive(Debug)] #[derive(Clone)] +#[derive(PartialEq)] +#[derive(Eq)] // #[derive(Display)] pub enum FileIDType { + NONE, UUID, } @@ -32,6 +37,10 @@ impl FileID { self.id.is_some() } + pub fn get_type(&self) -> FileIDType { + self.id_type.clone() + } + } impl Debug for FileID { @@ -71,15 +80,52 @@ impl Into for FileID { impl From for FileID { fn from(s: String) -> FileID { - unimplemented!() + FileID::from(&s) } } impl<'a> From<&'a String> for FileID { - fn from(s: &'a String) -> FileID { - unimplemented!() + fn from(string: &'a String) -> FileID { + + let regex = Regex::new(r"([:alnum:]*)-([:upper:]*)-([A-Za-z0-9-_]*)\.(.*)").unwrap(); + let s = string.split("/").last().unwrap_or(""); + + debug!("Regex build: {:?}", regex); + debug!("Matching string: '{}'", s); + regex.captures(s).and_then(|capts| { + // first one is the whole string, index 1-N are the matches. + if capts.len() != 5 { + debug!("Matches, but not expected number of groups"); + return None; + } + debug!("Matches: {}", capts.len()); + + let modname = capts.at(1).unwrap(); + let hashname = capts.at(2).unwrap(); + let mut hash = capts.at(3).unwrap(); + + debug!("Destructure FilePath to ID:"); + debug!(" FilePath: {:?}", s); + debug!(" Module Name: {:?}", modname); + debug!(" Hash Name: {:?}", hashname); + debug!(" Hash: {:?}", hash); + + let idtype = select_id_type_from_str(hashname); + match idtype { + FileIDType::NONE => hash = "INVALID", + _ => {}, + } + + Some(FileID::new(idtype, String::from(hash))) + }).unwrap_or({ + debug!("Did not match"); + FileID { + id_type: FileIDType::NONE, + id: None, + } + }) } } @@ -146,6 +192,13 @@ impl<'a> Display for FileIDError { } +fn select_id_type_from_str(s: &str) -> FileIDType { + match s { + "UUID" => FileIDType::UUID, + _ => FileIDType::NONE, + } +} + pub type FileIDResult = Result; #[cfg(test)]