use crate::errors::IOError;
use std::path::Path;
use uuid::Uuid;
pub fn generate_id() -> String {
Uuid::new_v4().to_string()
}
pub fn check_path<S>(target: S) -> Result<String, IOError>
where
S: Into<String>,
{
let s = target.into();
let path = Path::new(&s);
if !path.exists() {
Err(IOError::InvalidPath(s))
} else {
Ok(path.canonicalize()?.to_string_lossy().to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate_id() {
let id = generate_id();
assert_eq!(id.len(), 36);
}
#[test]
fn test_check_path() {
let valid_path = check_path("./src".to_string());
assert!(valid_path.is_ok());
let current_path = std::env::current_dir()
.unwrap()
.to_string_lossy()
.to_string();
assert_eq!(current_path + "/src", valid_path.unwrap());
let invalid_path = check_path("/not_exist".to_string());
assert!(invalid_path.is_err());
}
}