/ src / setup / paths.rs
paths.rs
 1  use std::{fs, io::ErrorKind, path::PathBuf};
 2  use struct_iterable::Iterable;
 3  
 4  #[derive(Iterable)]
 5  pub struct Paths {
 6      pub data: PathBuf,
 7      pub artifacts: PathBuf,
 8      pub the_book: PathBuf,
 9      pub rust_guide: PathBuf,
10  }
11  
12  impl Paths {
13      pub fn init() -> Self {
14          let parent = std::env::current_dir().expect("Failed to provide current directory");
15          let data = parent.join("data");
16          let artifacts = parent.join("artifacts");
17          let the_book = data.join("the_book");
18          let rust_guide = data.join("rust_guide");
19  
20          Self {
21              data,
22              artifacts,
23              the_book,
24              rust_guide,
25          }
26      }
27  }
28  
29  pub fn make_fundamental_directories() {
30      let paths = Paths::init();
31  
32      for (_, field_value) in paths.iter() {
33          // This .iter() method results from the the application of the Iterable trait to the Paths struct
34          if let Some(dir) = field_value.downcast_ref::<PathBuf>() {
35              match fs::create_dir(dir) {
36                  Ok(_) => log::info!("Created directory {}", dir.display()),
37                  Err(e) => {
38                      if e.kind() == ErrorKind::AlreadyExists {
39                          log::warn!("Directory \"{}\" already exists", dir.display())
40                      } else {
41                          log::error!("Unable to create directory \"{}\"", dir.display())
42                      }
43                  }
44              }
45          };
46      }
47  }