|
| 1 | +use std::{collections::HashMap, fs, path::Path}; |
| 2 | + |
| 3 | +use miette::Report; |
| 4 | +use tap::Pipe; |
| 5 | +#[cfg(feature = "profiling")] |
| 6 | +use tracing::instrument; |
| 7 | +use walkdir::WalkDir; |
| 8 | +use wax::Pattern; |
| 9 | + |
| 10 | +use super::Error; |
| 11 | +use crate::{helpers, FileFormat, FILE_EXTENSIONS_GLOB}; |
| 12 | + |
| 13 | +#[derive(Debug)] |
| 14 | +pub struct Defaults(HashMap<String, (String, FileFormat)>); |
| 15 | + |
| 16 | +impl Defaults { |
| 17 | + pub fn for_path(&self, path: impl AsRef<str>) -> Option<&(String, FileFormat)> { |
| 18 | + for path in Path::new(path.as_ref()).ancestors() { |
| 19 | + if let Some(defaults) = self.0.get(helpers::absolutize_virtually(path).unwrap().as_str()) { |
| 20 | + return Some(defaults); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + None |
| 25 | + } |
| 26 | + |
| 27 | + #[cfg_attr(feature = "profiling", instrument)] |
| 28 | + pub fn from_path(dotfiles_path: &Path) -> Result<Defaults, Box<Error>> { |
| 29 | + let defaults = helpers::glob_from_vec(&["**".to_owned()], format!("/{{dots,defaults}}.{FILE_EXTENSIONS_GLOB}").as_str().pipe(Some)).unwrap(); |
| 30 | + |
| 31 | + let paths = WalkDir::new(dotfiles_path) |
| 32 | + .into_iter() |
| 33 | + .collect::<Result<Vec<_>, _>>() |
| 34 | + .map_err(Error::WalkingDotfiles) |
| 35 | + .map_err(Box::new)?; |
| 36 | + |
| 37 | + let absolutized = paths |
| 38 | + .into_iter() |
| 39 | + .filter(|e| !e.file_type().is_dir()) |
| 40 | + .map(|d| { |
| 41 | + let path = d.path().strip_prefix(dotfiles_path).map(Path::to_path_buf).map_err(Error::PathStrip)?; |
| 42 | + let absolutized = helpers::absolutize_virtually(&path).map_err(|e| Error::ParseName(path.to_string_lossy().to_string(), e))?; |
| 43 | + let absolutized_dir = helpers::absolutize_virtually(path.parent().unwrap()).map_err(|e| Error::ParseName(path.to_string_lossy().to_string(), e))?; |
| 44 | + Ok::<_, Error>((absolutized, absolutized_dir, path)) |
| 45 | + }) |
| 46 | + .collect::<Result<Vec<_>, _>>() |
| 47 | + .map_err(Box::new)?; |
| 48 | + |
| 49 | + absolutized |
| 50 | + .into_iter() |
| 51 | + .filter(|e| defaults.is_match(e.0.as_str())) |
| 52 | + .map(|e| (e.1, e.2)) |
| 53 | + .map(|e| { |
| 54 | + if e.1.file_name().unwrap().to_string_lossy().starts_with("dots.") { |
| 55 | + let path = e.1.to_string_lossy().to_string(); |
| 56 | + println!( |
| 57 | + "Warning: {:?}", |
| 58 | + Report::new(Error::DotsDeprecated( |
| 59 | + e.1.extension().unwrap().to_string_lossy().to_string(), |
| 60 | + (path.rfind("dots").unwrap(), "dots".len()).into(), |
| 61 | + path |
| 62 | + )) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + ( |
| 67 | + e.0, |
| 68 | + ( |
| 69 | + fs::read_to_string(dotfiles_path.join(&e.1)).map_err(|err| Error::ReadingDot(e.1.clone(), err))?, |
| 70 | + FileFormat::try_from(e.1.as_path()).unwrap(), |
| 71 | + ), |
| 72 | + ) |
| 73 | + .pipe(Ok) |
| 74 | + }) |
| 75 | + .collect::<Result<HashMap<_, _>, _>>() |
| 76 | + .map(Defaults) |
| 77 | + .map_err(Box::new) |
| 78 | + } |
| 79 | +} |
0 commit comments