Skip to content

Commit

Permalink
refactor the atrocity i wrote originally.
Browse files Browse the repository at this point in the history
  • Loading branch information
duckinator committed Dec 16, 2024
1 parent 8b91f84 commit 4d8e0f3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
7 changes: 5 additions & 2 deletions cargo-dist/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,12 @@ pub enum DistError {
},

/// One or more required tools are missing.
#[error("One or more required tools are missing.")]
#[error("The following tools are required to run this task, but are missing:\n{tools:#?}")]
#[diagnostic(help("Please install the tools mentioned above and try again."))]
EnvToolsMissing {},
EnvToolsMissing {
/// the names of the missing tools
tools: Vec<String>,
},

/// Unknown target requested
#[error(
Expand Down
53 changes: 28 additions & 25 deletions cargo-dist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ pub mod tasks;
#[cfg(test)]
mod tests;

fn tool_status(tool: Result<&Tool, DistError>) -> Result<String, String> {
match tool {
Ok(tool) => Ok(format!("✅ located {}", tool.cmd)),
Err(DistError::ToolMissing { tool: ref name }) => Err(format!("🗙 missing {}", name)),
Err(_) => unreachable!("tool_status() was given a DistError that wasn't DistError::ToolMissing. This is a bug in dist."),
}
}

/// dist env test -- make sure we have everything we need for a build.
pub fn do_env_test(cfg: &Config) -> DistResult<()> {
let (dist, mut manifest) = tasks::gather_work(cfg)?;

Check failure on line 62 in cargo-dist/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

variable does not need to be mutable

error: variable does not need to be mutable --> cargo-dist/src/lib.rs:62:16 | 62 | let (dist, mut manifest) = tasks::gather_work(cfg)?; | ----^^^^^^^^ | | | help: remove this `mut` | = note: `-D unused-mut` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_mut)]`

Check failure on line 62 in cargo-dist/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `manifest`

error: unused variable: `manifest` --> cargo-dist/src/lib.rs:62:20 | 62 | let (dist, mut manifest) = tasks::gather_work(cfg)?; | ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_manifest` | = note: `-D unused-variables` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_variables)]`

Check failure on line 62 in cargo-dist/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

variable does not need to be mutable

error: variable does not need to be mutable --> cargo-dist/src/lib.rs:62:16 | 62 | let (dist, mut manifest) = tasks::gather_work(cfg)?; | ----^^^^^^^^ | | | help: remove this `mut` | = note: `-D unused-mut` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_mut)]`

Check failure on line 62 in cargo-dist/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `manifest`

error: unused variable: `manifest` --> cargo-dist/src/lib.rs:62:20 | 62 | let (dist, mut manifest) = tasks::gather_work(cfg)?; | ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_manifest` | = note: `-D unused-variables` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_variables)]`
Expand Down Expand Up @@ -96,29 +88,40 @@ pub fn do_env_test(cfg: &Config) -> DistResult<()> {
*/
}

let commands: Vec<Result<String, String>> = vec![
// These are all of the tools we can check for.
//
// bool::then(f) returns an Option, so we start with a
// Vec<Option<Result<&Tool, DistResult>>>.
let all_tools: Vec<Option<DistResult<&Tool>>> = vec![
need_cargo_auditable.then(|| tools.cargo_auditable()),
need_cargo_cyclonedx.then(|| tools.cargo_cyclonedx()),
need_omnibor.then(|| tools.omnibor()),
need_xwin.then(|| tools.cargo_xwin()),
need_zigbuild.then(|| tools.cargo_zigbuild()),
]
.into_iter()
.filter_map(|t| t)
.filter(|t| t.is_err())
.map(|t| tool_status(t))
.filter(|t| t.is_err())
.collect();

let mut okay = true;
for command in commands {
if let Err(s) = command {
okay = false;
println!("{}", s);
}
}
];

// Drop `None`s, then extract the values from the remaining `Option`s.
let needed_tools = all_tools.into_iter().flatten();

// For each tool we need,
let missing: Vec<String> = needed_tools
.filter_map(|t| match t {
// The tool was found.
Ok(_) => None,
// The tool is missing
Err(DistError::ToolMissing { tool: ref name }) => Some(name.to_owned()),
// This should never happen, but I can't find a way to enforce
// it at the type system level. ;~; -@duckinator
Err(_) => unreachable!(
"do_env_test() got an Err that wasn't DistError::ToolMissing. This is a dist bug."
),
})
.collect();

okay.then(|| ()).ok_or(DistError::EnvToolsMissing {})
missing
.is_empty()
.then_some(())
.ok_or(DistError::EnvToolsMissing { tools: missing })
}

/// dist build -- actually build binaries and installers!
Expand Down

0 comments on commit 4d8e0f3

Please sign in to comment.