Skip to content

Commit 22603ba

Browse files
committed
Introduce boilerplate for checking Nix files
1 parent 692d211 commit 22603ba

File tree

4 files changed

+107
-15
lines changed

4 files changed

+107
-15
lines changed

src/eval.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::BTreeMap;
12
use std::path::{Path, PathBuf};
23
use std::{env, fs, process};
34

@@ -156,7 +157,7 @@ pub fn check_values(
156157
nixpkgs_path: &Path,
157158
nix_file_store: &mut NixFileStore,
158159
package_names: &[String],
159-
) -> validation::Result<ratchet::Nixpkgs> {
160+
) -> validation::Result<BTreeMap<String, ratchet::Package>> {
160161
let work_dir = tempfile::Builder::new()
161162
.prefix("nixpkgs-vet")
162163
.tempdir()
@@ -255,9 +256,7 @@ pub fn check_values(
255256
.collect_vec()?,
256257
);
257258

258-
Ok(check_result.map(|elems| ratchet::Nixpkgs {
259-
packages: elems.into_iter().collect(),
260-
}))
259+
Ok(check_result.map(|elems| elems.into_iter().collect()))
261260
}
262261

263262
/// Handle the evaluation result for an attribute in `pkgs/by-name`, making it a validation result.

src/files.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

src/main.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// #![allow(clippy::missing_const_for_fn)]
1111

1212
mod eval;
13+
mod files;
1314
mod location;
1415
mod nix_file;
1516
mod problem;
@@ -21,6 +22,7 @@ mod validation;
2122

2223
use anyhow::Context as _;
2324
use clap::Parser;
25+
use std::collections::BTreeMap;
2426
use std::path::{Path, PathBuf};
2527
use std::process::ExitCode;
2628
use std::{panic, thread};
@@ -113,20 +115,30 @@ fn check_nixpkgs(nixpkgs_path: &Path) -> validation::Result<ratchet::Nixpkgs> {
113115
)
114116
})?;
115117

116-
if !nixpkgs_path.join(structure::BASE_SUBPATH).exists() {
117-
// No pkgs/by-name directory, always valid
118-
return Ok(Success(ratchet::Nixpkgs::default()));
119-
}
120-
121118
let mut nix_file_store = NixFileStore::default();
122-
let structure = check_structure(&nixpkgs_path, &mut nix_file_store)?;
123119

124-
// Only if we could successfully parse the structure, we do the evaluation checks
125-
let result = structure.result_map(|package_names| {
126-
eval::check_values(&nixpkgs_path, &mut nix_file_store, package_names.as_slice())
127-
})?;
120+
let package_result = {
121+
if !nixpkgs_path.join(structure::BASE_SUBPATH).exists() {
122+
// No pkgs/by-name directory, always valid
123+
Success(BTreeMap::new())
124+
} else {
125+
let structure = check_structure(&nixpkgs_path, &mut nix_file_store)?;
126+
127+
// Only if we could successfully parse the structure, we do the evaluation checks
128+
structure.result_map(|package_names| {
129+
eval::check_values(&nixpkgs_path, &mut nix_file_store, package_names.as_slice())
130+
})?
131+
}
132+
};
133+
134+
let file_result = files::check_files(&nixpkgs_path, &mut nix_file_store)?;
128135

129-
Ok(result)
136+
Ok(
137+
package_result.and(file_result, |packages, files| ratchet::Nixpkgs {
138+
packages,
139+
files,
140+
}),
141+
)
130142
}
131143

132144
#[cfg(test)]

src/ratchet.rs

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//!
33
//! Each type has a `compare` method that validates the ratchet checks for that item.
44
5+
use relative_path::RelativePath;
56
use std::collections::BTreeMap;
67

78
use relative_path::RelativePathBuf;
@@ -15,6 +16,7 @@ use crate::validation::{self, Validation, Validation::Success};
1516
pub struct Nixpkgs {
1617
/// The ratchet values for all packages
1718
pub packages: BTreeMap<String, Package>,
19+
pub files: BTreeMap<RelativePathBuf, File>,
1820
}
1921

2022
impl Nixpkgs {
@@ -27,6 +29,9 @@ impl Nixpkgs {
2729
.into_iter()
2830
.map(|(name, pkg)| Package::compare(&name, from.packages.get(&name), &pkg)),
2931
)
32+
.and_(validation::sequence_(to.files.into_iter().map(
33+
|(name, file)| File::compare(&name, from.files.get(&name), &file),
34+
)))
3035
}
3136
}
3237

@@ -57,6 +62,16 @@ impl Package {
5762
}
5863
}
5964

65+
pub struct File {
66+
}
67+
68+
impl File {
69+
/// Validates the ratchet checks for a top-level package
70+
pub fn compare(_name: &RelativePath, _optional_from: Option<&Self>, _to: &Self) -> Validation<()> {
71+
Success(())
72+
}
73+
}
74+
6075
/// The ratchet state of a generic ratchet check.
6176
pub enum RatchetState<Ratchet: ToProblem> {
6277
/// The ratchet is loose. It can be tightened more. In other words, this is the legacy state

0 commit comments

Comments
 (0)