Skip to content

Commit

Permalink
Refactor check_nix_file to use to_problem closure
Browse files Browse the repository at this point in the history
  • Loading branch information
willbush committed Mar 24, 2024
1 parent 7eaa754 commit d32d4cf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ fn by_name_override(
location: Location,
relative_location_file: RelativePathBuf,
) -> validation::Validation<ratchet::RatchetState<ratchet::ManualDefinition>> {
let to_problem = |kind| -> NixpkgsProblem {
let to_problem = |kind| {
NixpkgsProblem::ByNameOverrideProblem(ByNameOverrideError {
package_name: attribute_name.to_owned(),
file: relative_location_file,
Expand Down
37 changes: 19 additions & 18 deletions src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,34 +130,35 @@ fn check_nix_file(
return Success(());
};

let to_problem = |kind| {
NixpkgsProblem::NixFileProblem(NixFileError {
relative_package_dir: relative_package_dir.to_owned(),
subpath: subpath.to_owned(),
line: nix_file.line_index.line(node.text_range().start().into()),
text: node.text().to_string(),
kind,
})
};

use crate::nix_file::ResolvedPath;

let error_or_none = match nix_file.static_resolve_path(path, absolute_package_dir) {
ResolvedPath::Interpolated => Some(NixFileErrorKind::PathInterpolation),
ResolvedPath::SearchPath => Some(NixFileErrorKind::SearchPath),
ResolvedPath::Outside => Some(NixFileErrorKind::OutsidePathReference),
match nix_file.static_resolve_path(path, absolute_package_dir) {
ResolvedPath::Interpolated => {
to_problem(NixFileErrorKind::PathInterpolation).into()
}
ResolvedPath::SearchPath => to_problem(NixFileErrorKind::SearchPath).into(),
ResolvedPath::Outside => to_problem(NixFileErrorKind::OutsidePathReference).into(),
ResolvedPath::Unresolvable(e) => {
Some(NixFileErrorKind::UnresolvablePathReference {
to_problem(NixFileErrorKind::UnresolvablePathReference {
io_error: e.to_string(),
})
.into()
}
ResolvedPath::Within(..) => {
// No need to handle the case of it being inside the directory, since we scan through the
// entire directory recursively anyways
None
Success(())
}
};
if let Some(kind) = error_or_none {
NixpkgsProblem::NixFileProblem(NixFileError {
relative_package_dir: relative_package_dir.to_owned(),
subpath: subpath.to_owned(),
line: nix_file.line_index.line(node.text_range().start().into()),
text: node.text().to_string(),
kind,
})
.into()
} else {
Success(())
}
}),
))
Expand Down

0 comments on commit d32d4cf

Please sign in to comment.