brown/bro/
clone_dir_structure.rs

1use crate::BroPath;
2use crate::bro;
3use crate::BrownError;
4use crate::path_exists;
5/// The clone_dir_structure takes a source folder and destination folder names. It will then create a destination folder and clone the source folder/directory structure in to that. 
6pub fn clone_dir_structure<'a>(source:&str,destination:&str)
7->Result<Vec<String>,BrownError>{
8//=== check if destination folder exists
9match path_exists(source) {
10   true=>{},
11   false=>{return Err(BrownError::DirNotFound)},
12}
13//============= Step One    
14let source_str = bro:: 
15dir_structure_to_string(source)?;
16
17//============ Step Two  
18let bro_path = BroPath::new();
19let paths_changed_parent = 
20bro_path.change_destination(&source_str, source, destination)?;
21
22let mutated = 
23bro_path.remove_dot_slash_all(&paths_changed_parent)?;
24//============ Step Three
25let mutated_str:Vec<&str>  = 
26mutated.iter().map(|s| &**s).collect();
27
28let x = 
29   bro::create_dir_structure
30      (&mutated_str);
31
32   match x {
33   Ok(_item)=>{
34            Ok(mutated) 
35      },
36   Err(e)=>{return Err(e);},
37   }
38}
39    
40mod tests {
41use crate::test_helper::TestHelper;
42use super::*;    
43use super::super::*;
44#[test]
45fn manual_test(){
46      let th = TestHelper::
47      new("dir963");
48      
49      let _ = th.create_parent_dir();
50      let _ = th.create_dir("a");
51      let _ = th.create_dir("a/a1");
52      let _ = th.create_dir("b");
53      let _ = th.create_dir("b/b1");
54      
55let final_outcome  = 
56clone_dir_structure("dir963","dir97")
57.unwrap();
58//  println!("{:?}",final_outcome);    
59      
60      // println!("{:#?}",outcome);
61      let compare = [
62         "dir97",
63         "dir97/b",
64         "dir97/a",
65         "dir97/b/b1",
66         "dir97/a/a1",
67      ];
68      // in assert eq which ever you write first matter write the outcome first or it will raise errors about &str and String comaprison.
69      assert_eq!(final_outcome,compare);
70      let _ =bro::remove_dir_brute("dir97");
71      let _ = th.tear_down();
72}
73}