diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index 18cf1b93..84e8c3bc 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -13,3 +13,30 @@ pub fn exec(cmd: &mut Command) -> Result<()> { } Ok(()) } + +#[derive(Debug)] +pub struct Errors(Vec); + +impl Errors { + pub fn new(errors: Vec) -> Self { + Self(errors) + } +} + +impl std::fmt::Display for Errors +where + E: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let Self(errors) = self; + for (i, error) in errors.iter().enumerate() { + if i != 0 { + writeln!(f)?; + } + write!(f, "{:?}", error)?; + } + Ok(()) + } +} + +impl std::error::Error for Errors where E: std::fmt::Debug {} diff --git a/xtask/src/public_api.rs b/xtask/src/public_api.rs index b5b58f07..0ca83f65 100644 --- a/xtask/src/public_api.rs +++ b/xtask/src/public_api.rs @@ -10,6 +10,7 @@ use cargo_metadata::{Metadata, Package}; use clap::Parser; use dialoguer::{theme::ColorfulTheme, Confirm}; use diff::{lines, Result as Diff}; +use xtask::Errors; #[derive(Debug, Parser)] pub struct Options { @@ -37,34 +38,36 @@ pub fn public_api(options: Options, metadata: Metadata) -> Result<()> { workspace_root, packages, .. - } = &metadata; + } = metadata; - let errors = packages - .iter() - .fold(String::new(), |mut buf, Package { name, publish, .. }| { - if !matches!(publish, Some(publish) if publish.is_empty()) { - match check_package_api(name, toolchain, bless, workspace_root.as_std_path()) { - Ok(diff) => { - if !diff.is_empty() { - writeln!( - &mut buf, - "{name} public API changed; re-run with --bless. diff:\n{diff}" - ) - .unwrap(); - } - } - Err(err) => { - writeln!(&mut buf, "{name} failed to check public API: {err}").unwrap(); - } + let errors: Vec<_> = packages + .into_iter() + .map(|Package { name, publish, .. }| { + if matches!(publish, Some(publish) if publish.is_empty()) { + Ok(()) + } else { + let diff = check_package_api(&name, toolchain, bless, workspace_root.as_std_path()) + .with_context(|| format!("{name} failed to check public API"))?; + if diff.is_empty() { + Ok(()) + } else { + Err(anyhow::anyhow!( + "{name} public API changed; re-run with --bless. diff:\n{diff}" + )) } } - buf - }); + }) + .filter_map(|result| match result { + Ok(()) => None, + Err(err) => Some(err), + }) + .collect(); - if !errors.is_empty() { - bail!("public API errors:\n{errors}") + if errors.is_empty() { + Ok(()) + } else { + Err(Errors::new(errors).into()) } - Ok(()) } fn check_package_api( diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 15ee7035..2deca1ad 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -13,7 +13,7 @@ use std::{ use anyhow::{anyhow, bail, Context as _, Result}; use cargo_metadata::{Artifact, CompilerMessage, Message, Target}; use clap::Parser; -use xtask::{exec, AYA_BUILD_INTEGRATION_BPF}; +use xtask::{exec, Errors, AYA_BUILD_INTEGRATION_BPF}; #[derive(Parser)] enum Environment { @@ -104,24 +104,6 @@ where Ok(executables) } -#[derive(Debug)] -struct Errors(Vec); - -impl std::fmt::Display for Errors { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let Self(errors) = self; - for (i, error) in errors.iter().enumerate() { - if i != 0 { - writeln!(f)?; - } - write!(f, "{:?}", error)?; - } - Ok(()) - } -} - -impl std::error::Error for Errors {} - /// Build and run the project. pub fn run(opts: Options) -> Result<()> { let Options { @@ -547,7 +529,7 @@ pub fn run(opts: Options) -> Result<()> { if errors.is_empty() { Ok(()) } else { - Err(Errors(errors).into()) + Err(Errors::new(errors).into()) } } }