1use crate::errors::IOError;
6use std::path::Path;
7use uuid::Uuid;
8
9pub fn generate_id() -> String {
20 Uuid::new_v4().to_string()
21}
22
23pub fn check_path<S>(target: S) -> Result<String, IOError>
39where
40 S: Into<String>,
41{
42 let s = target.into();
43 let path = Path::new(&s);
44 if !path.exists() {
45 Err(IOError::InvalidPath(s))
46 } else {
47 Ok(path.canonicalize()?.to_string_lossy().to_string())
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_generate_id() {
57 let id = generate_id();
58 assert_eq!(id.len(), 36);
59 }
60
61 #[test]
62 fn test_check_path() {
63 let valid_path = check_path("./src".to_string());
64 assert!(valid_path.is_ok());
65 let current_path = std::env::current_dir()
67 .unwrap()
68 .to_string_lossy()
69 .to_string();
70 assert_eq!(current_path + "/src", valid_path.unwrap());
72 let invalid_path = check_path("/not_exist".to_string());
73 assert!(invalid_path.is_err());
74 }
75}