Implement: FileID::from(&String), FileID::from(String)
This commit is contained in:
parent
2c3dd521b8
commit
dbf9ada732
1 changed files with 56 additions and 3 deletions
|
@ -6,10 +6,15 @@ use std::path::{Path, PathBuf};
|
||||||
use std::convert::From;
|
use std::convert::From;
|
||||||
use std::convert::Into;
|
use std::convert::Into;
|
||||||
|
|
||||||
|
use regex::Regex;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
#[derive(Eq)]
|
||||||
// #[derive(Display)]
|
// #[derive(Display)]
|
||||||
pub enum FileIDType {
|
pub enum FileIDType {
|
||||||
|
NONE,
|
||||||
UUID,
|
UUID,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,6 +37,10 @@ impl FileID {
|
||||||
self.id.is_some()
|
self.id.is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_type(&self) -> FileIDType {
|
||||||
|
self.id_type.clone()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Debug for FileID {
|
impl Debug for FileID {
|
||||||
|
@ -71,15 +80,52 @@ impl Into<String> for FileID {
|
||||||
impl From<String> for FileID {
|
impl From<String> for FileID {
|
||||||
|
|
||||||
fn from(s: String) -> FileID {
|
fn from(s: String) -> FileID {
|
||||||
unimplemented!()
|
FileID::from(&s)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a String> for FileID {
|
impl<'a> From<&'a String> for FileID {
|
||||||
|
|
||||||
fn from(s: &'a String) -> FileID {
|
fn from(string: &'a String) -> FileID {
|
||||||
unimplemented!()
|
|
||||||
|
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<FileID, FileIDError>;
|
pub type FileIDResult = Result<FileID, FileIDError>;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Reference in a new issue