Skip to content

Commit d1e2dc2

Browse files
committed
Generalise Validation::and, rename original to ::_and
For a future change we need a version that gives access to the Success value from both arguments.
1 parent 93a073d commit d1e2dc2

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ fn by_name(
372372

373373
// Independently report problems about whether it's a derivation and the callPackage
374374
// variant.
375-
is_derivation_result.and(variant_result)
375+
is_derivation_result.and_(variant_result)
376376
}
377377
};
378378
Ok(

src/structure.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn check_structure(
9696
.into()
9797
});
9898

99-
let result = result.and(validation::sequence_(duplicate_results));
99+
let result = result.and_(validation::sequence_(duplicate_results));
100100

101101
let package_results = entries
102102
.into_iter()
@@ -111,7 +111,7 @@ pub fn check_structure(
111111
})
112112
.collect_vec()?;
113113

114-
result.and(validation::sequence(package_results))
114+
result.and_(validation::sequence(package_results))
115115
})
116116
})
117117
.collect_vec()?;
@@ -147,7 +147,7 @@ fn check_package(
147147
};
148148

149149
let correct_relative_package_dir = relative_dir_for_package(&package_name);
150-
let result = result.and(if relative_package_dir != correct_relative_package_dir {
150+
let result = result.and_(if relative_package_dir != correct_relative_package_dir {
151151
// Only show this error if we have a valid shard and package name.
152152
// If one of those is wrong, you should fix that first.
153153
if shard_name_valid && package_name_valid {
@@ -164,15 +164,15 @@ fn check_package(
164164
});
165165

166166
let package_nix_path = package_path.join(PACKAGE_NIX_FILENAME);
167-
let result = result.and(if !package_nix_path.exists() {
167+
let result = result.and_(if !package_nix_path.exists() {
168168
npv_143::PackageNixMissing::new(package_name.clone()).into()
169169
} else if !package_nix_path.is_file() {
170170
npv_144::PackageNixIsNotFile::new(package_name.clone()).into()
171171
} else {
172172
Success(())
173173
});
174174

175-
let result = result.and(references::check_references(
175+
let result = result.and_(references::check_references(
176176
nix_file_store,
177177
&relative_package_dir,
178178
&relative_package_dir.to_path(path),

src/validation.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,17 @@ impl<A> Validation<A> {
7575
impl Validation<()> {
7676
/// Combine two validations, both of which need to be successful for the return value to be
7777
/// successful. The `Problem`s of both sides are returned concatenated.
78-
pub fn and<A>(self, other: Validation<A>) -> Validation<A> {
78+
pub fn and_<B>(self, other: Validation<B>) -> Validation<B> {
79+
self.and(other, |(), b| b)
80+
}
81+
}
82+
83+
impl<A> Validation<A> {
84+
/// Combine two validations, both of which need to be successful for the return value to be
85+
/// successful. The `Problem`s of both sides are returned concatenated.
86+
pub fn and<B, C, F: FnOnce(A, B) -> C>(self, other: Validation<B>, f: F) -> Validation<C> {
7987
match (self, other) {
80-
(Success(_), Success(right_value)) => Success(right_value),
88+
(Success(a), Success(b)) => Success(f(a, b)),
8189
(Failure(errors_l), Failure(errors_r)) => Failure(concat([errors_l, errors_r])),
8290
(Failure(errors), Success(_)) | (Success(_), Failure(errors)) => Failure(errors),
8391
}

0 commit comments

Comments
 (0)