Skip to content

Commit

Permalink
Replace RatchetErrorKind with is_new and is_empty bools
Browse files Browse the repository at this point in the history
  • Loading branch information
willbush committed Mar 26, 2024
1 parent 2ed5984 commit c57206d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
24 changes: 9 additions & 15 deletions src/nixpkgs_problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,8 @@ pub struct RatchetError {
pub package_name: String,
pub call_package_path: Option<RelativePathBuf>,
pub file: RelativePathBuf,
pub kind: RatchetErrorKind,
}

#[derive(Clone)]
pub enum RatchetErrorKind {
MovedOutOfByName,
MovedOutOfByNameEmptyArg,
NewPackageNotUsingByName,
NewPackageNotUsingByNameEmptyArg,
pub is_new: bool,
pub is_empty: bool,
}

impl fmt::Display for NixpkgsProblem {
Expand Down Expand Up @@ -364,7 +357,8 @@ impl fmt::Display for NixpkgsProblem {
package_name,
call_package_path,
file,
kind,
is_new,
is_empty,
}) => {
let call_package_arg =
if let Some(path) = &call_package_path {
Expand All @@ -374,16 +368,16 @@ impl fmt::Display for NixpkgsProblem {
};
let relative_package_file = structure::relative_file_for_package(package_name);

match kind {
RatchetErrorKind::MovedOutOfByNameEmptyArg =>
match (is_new, is_empty) {
(false, true) =>
writedoc!(
f,
"
- Attribute `pkgs.{package_name}` was previously defined in {relative_package_file}, but is now manually defined as `callPackage {call_package_arg} {{ /* ... */ }}` in {file}.
Please move the package back and remove the manual `callPackage`.
",
),
RatchetErrorKind::MovedOutOfByName =>
(false, false) =>
// This can happen if users mistakenly assume that for custom arguments,
// pkgs/by-name can't be used.
writedoc!(
Expand All @@ -393,7 +387,7 @@ impl fmt::Display for NixpkgsProblem {
While the manual `callPackage` is still needed, it's not necessary to move the package files.
",
),
RatchetErrorKind::NewPackageNotUsingByNameEmptyArg =>
(true, true) =>
writedoc!(
f,
"
Expand All @@ -403,7 +397,7 @@ impl fmt::Display for NixpkgsProblem {
Since the second `callPackage` argument is `{{ }}`, no manual `callPackage` in {file} is needed anymore.
",
),
RatchetErrorKind::NewPackageNotUsingByName =>
(true, false) =>
writedoc!(
f,
"
Expand Down
10 changes: 3 additions & 7 deletions src/ratchet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! Each type has a `compare` method that validates the ratchet checks for that item.
use crate::nix_file::CallPackageArgumentInfo;
use crate::nixpkgs_problem::{NixpkgsProblem, RatchetError, RatchetErrorKind};
use crate::nixpkgs_problem::{NixpkgsProblem, RatchetError};
use crate::validation::{self, Validation, Validation::Success};
use relative_path::RelativePathBuf;
use std::collections::HashMap;
Expand Down Expand Up @@ -157,12 +157,8 @@ impl ToNixpkgsProblem for UsesByName {
package_name: name.to_owned(),
call_package_path: to.relative_path.clone(),
file: file.to_owned(),
kind: match (optional_from, to.empty_arg) {
(Some(()), true) => RatchetErrorKind::MovedOutOfByNameEmptyArg,
(Some(()), false) => RatchetErrorKind::MovedOutOfByName,
(None, true) => RatchetErrorKind::NewPackageNotUsingByNameEmptyArg,
(None, false) => RatchetErrorKind::NewPackageNotUsingByName,
},
is_new: optional_from.is_none(),
is_empty: to.empty_arg,
})
}
}

0 comments on commit c57206d

Please sign in to comment.