1 use std::fs; 2 use std::path::PathBuf; 3 4 use super::utils::remove_dir_if_exists; 5 6 #[derive(Debug, Clone)] 7 pub(crate) struct Dirs { 8 pub(crate) source_dir: PathBuf, 9 pub(crate) download_dir: PathBuf, 10 pub(crate) build_dir: PathBuf, 11 pub(crate) dist_dir: PathBuf, 12 pub(crate) frozen: bool, 13 } 14 15 #[doc(hidden)] 16 #[derive(Debug, Copy, Clone)] 17 pub(crate) enum PathBase { 18 Source, 19 Download, 20 Build, 21 Dist, 22 } 23 24 impl PathBase { to_path(self, dirs: &Dirs) -> PathBuf25 fn to_path(self, dirs: &Dirs) -> PathBuf { 26 match self { 27 PathBase::Source => dirs.source_dir.clone(), 28 PathBase::Download => dirs.download_dir.clone(), 29 PathBase::Build => dirs.build_dir.clone(), 30 PathBase::Dist => dirs.dist_dir.clone(), 31 } 32 } 33 } 34 35 #[derive(Debug, Copy, Clone)] 36 pub(crate) enum RelPath { 37 Base(PathBase), 38 Join(&'static RelPath, &'static str), 39 } 40 41 impl RelPath { 42 pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source); 43 pub(crate) const DOWNLOAD: RelPath = RelPath::Base(PathBase::Download); 44 pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build); 45 pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist); 46 47 pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts"); 48 pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches"); 49 join(&'static self, suffix: &'static str) -> RelPath50 pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath { 51 RelPath::Join(self, suffix) 52 } 53 to_path(&self, dirs: &Dirs) -> PathBuf54 pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf { 55 match self { 56 RelPath::Base(base) => base.to_path(dirs), 57 RelPath::Join(base, suffix) => base.to_path(dirs).join(suffix), 58 } 59 } 60 ensure_exists(&self, dirs: &Dirs)61 pub(crate) fn ensure_exists(&self, dirs: &Dirs) { 62 fs::create_dir_all(self.to_path(dirs)).unwrap(); 63 } 64 ensure_fresh(&self, dirs: &Dirs)65 pub(crate) fn ensure_fresh(&self, dirs: &Dirs) { 66 let path = self.to_path(dirs); 67 remove_dir_if_exists(&path); 68 fs::create_dir_all(path).unwrap(); 69 } 70 } 71