|
| 1 | +use crate::problem::npv_170; |
| 2 | +use relative_path::RelativePath; |
| 3 | +use relative_path::RelativePathBuf; |
| 4 | +use rnix::ast::Expr::Str; |
| 5 | +use std::collections::BTreeMap; |
| 6 | +use std::path::Path; |
| 7 | + |
| 8 | +use crate::nix_file::NixFileStore; |
| 9 | +use crate::validation::ResultIteratorExt; |
| 10 | +use crate::validation::Validation::Success; |
| 11 | +use crate::{nix_file, ratchet, structure, validation}; |
| 12 | + |
| 13 | +pub fn check_files( |
| 14 | + nixpkgs_path: &Path, |
| 15 | + nix_file_store: &mut NixFileStore, |
| 16 | +) -> validation::Result<BTreeMap<RelativePathBuf, ratchet::File>> { |
| 17 | + process_nix_files(nixpkgs_path, nix_file_store, |nix_file| { |
| 18 | + Ok(Success(ratchet::File { })) |
| 19 | + }) |
| 20 | +} |
| 21 | + |
| 22 | +fn collect_nix_files( |
| 23 | + base: &Path, |
| 24 | + dir: &RelativePath, |
| 25 | + files: &mut Vec<RelativePathBuf>, |
| 26 | +) -> anyhow::Result<()> { |
| 27 | + for entry in structure::read_dir_sorted(&dir.to_path(base))? { |
| 28 | + let mut relative_path = dir.to_relative_path_buf(); |
| 29 | + relative_path.push(entry.file_name().to_string_lossy().into_owned()); |
| 30 | + |
| 31 | + let absolute_path = entry.path(); |
| 32 | + |
| 33 | + if absolute_path.is_symlink() { |
| 34 | + continue; |
| 35 | + } |
| 36 | + if absolute_path.is_dir() { |
| 37 | + collect_nix_files(base, &relative_path, files)? |
| 38 | + } else if absolute_path.extension().is_some_and(|x| x == "nix") { |
| 39 | + files.push(relative_path) |
| 40 | + } |
| 41 | + } |
| 42 | + Ok(()) |
| 43 | +} |
| 44 | + |
| 45 | +fn process_nix_files<F: Fn(&nix_file::NixFile) -> validation::Result<ratchet::File>>( |
| 46 | + nixpkgs_path: &Path, |
| 47 | + nix_file_store: &mut NixFileStore, |
| 48 | + f: F, |
| 49 | +) -> validation::Result<BTreeMap<RelativePathBuf, ratchet::File>> { |
| 50 | + let files = { |
| 51 | + let mut files = vec![]; |
| 52 | + collect_nix_files(nixpkgs_path, &RelativePathBuf::new(), &mut files)?; |
| 53 | + files |
| 54 | + }; |
| 55 | + |
| 56 | + let file_results: Vec<validation::Validation<(RelativePathBuf, ratchet::File)>> = files |
| 57 | + .into_iter() |
| 58 | + .map(|path| { |
| 59 | + let nix_file = nix_file_store.get(&path.to_path(nixpkgs_path))?; |
| 60 | + let val = f(nix_file)?.map(|file| (path, file)); |
| 61 | + Ok::<_, anyhow::Error>(val) |
| 62 | + }) |
| 63 | + .collect_vec()?; |
| 64 | + |
| 65 | + Ok(validation::sequence(file_results).map(|entries| entries.into_iter().collect())) |
| 66 | +} |
0 commit comments