From 7c100e773b38dbd0c6c0f5bfa2a6ff12df659f35 Mon Sep 17 00:00:00 2001 From: Martijn Gribnau Date: Fri, 18 Oct 2024 22:42:14 +0200 Subject: [PATCH] Report the CargoWorkspace event The event contains the names (NB: not the package id's) of packages which were detected in the current cargo workspace. Possibly must be sent optionally in the future, because we should also support non cargo workspaces. --- src/context.rs | 21 +++++++++++++++++++-- src/lib.rs | 5 ++++- src/reporter/event.rs | 5 +++++ src/reporter/event/cargo_workspace.rs | 19 +++++++++++++++++++ 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/reporter/event/cargo_workspace.rs diff --git a/src/context.rs b/src/context.rs index ed06778f..b2154ca6 100644 --- a/src/context.rs +++ b/src/context.rs @@ -74,6 +74,16 @@ impl Context { } } + pub fn environment_context(&self) -> &EnvironmentContext { + match self { + Context::Find(ctx) => &ctx.environment, + Context::List(ctx) => &ctx.environment, + Context::Set(ctx) => &ctx.environment, + Context::Show(ctx) => &ctx.environment, + Context::Verify(ctx) => &ctx.environment, + } + } + /// Returns the inner find context, if it was present. pub fn to_find_context(self) -> Option { if let Self::Find(ctx) = self { @@ -314,15 +324,22 @@ impl EnvironmentContext { #[derive(Clone, Debug)] pub struct WorkspacePackages { - _selected: Vec, + selected: Vec, } impl WorkspacePackages { pub fn from_iter(selected: impl IntoIterator) -> Self { Self { - _selected: selected.into_iter().collect(), + selected: selected.into_iter().collect(), } } + + pub fn names(&self) -> Vec { + self.selected + .iter() + .map(|pkg| pkg.name.to_string()) + .collect() + } } #[derive(Clone, Copy, Debug, Default, PartialEq, ValueEnum)] diff --git a/src/lib.rs b/src/lib.rs index 6a3c89d0..803ce8e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ pub use crate::sub_command::{Find, List, Set, Show, SubCommand, Verify}; use crate::check::RustupToolchainCheck; use crate::context::ReleaseSource; use crate::error::{CargoMSRVError, TResult}; -use crate::reporter::event::{Meta, SubcommandInit}; +use crate::reporter::event::{CargoWorkspace, Meta, SubcommandInit}; use crate::reporter::{Event, Reporter}; use rust_releases::semver; @@ -60,6 +60,9 @@ pub(crate) mod writer; pub fn run_app(ctx: &Context, reporter: &impl Reporter) -> TResult<()> { reporter.report_event(Meta::default())?; + reporter.report_event(CargoWorkspace::new( + ctx.environment_context().workspace_packages.names(), + ))?; reporter.report_event(SubcommandInit::new(ctx.reporting_name()))?; match ctx { diff --git a/src/reporter/event.rs b/src/reporter/event.rs index 07eebb7c..bad4dbbb 100644 --- a/src/reporter/event.rs +++ b/src/reporter/event.rs @@ -11,6 +11,7 @@ pub use scope::TestScopeGenerator; pub use auxiliary_output::{ AuxiliaryOutput, Destination, Item as AuxiliaryOutputItem, MsrvKind, ToolchainFileKind, }; +pub use cargo_workspace::CargoWorkspace; pub use check_method::{CheckMethod, Method}; pub use check_result::CheckResult; pub use check_toolchain::CheckToolchain; @@ -41,6 +42,7 @@ mod types; // specific events mod auxiliary_output; +mod cargo_workspace; mod check_method; mod check_result; mod check_toolchain; @@ -123,6 +125,9 @@ pub enum Message { // setup Meta(Meta), + // package selection + CargoWorkspace(CargoWorkspace), + // get rust-releases index FetchIndex(FetchIndex), // todo! UnableToConfirmValidReleaseVersion(UnableToConfirmValidReleaseVersion), diff --git a/src/reporter/event/cargo_workspace.rs b/src/reporter/event/cargo_workspace.rs new file mode 100644 index 00000000..7d73df37 --- /dev/null +++ b/src/reporter/event/cargo_workspace.rs @@ -0,0 +1,19 @@ +use crate::reporter::{Event, Message}; + +#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)] +#[serde(rename_all = "snake_case")] +pub struct CargoWorkspace { + package_names: Vec, +} + +impl CargoWorkspace { + pub fn new(package_names: Vec) -> Self { + Self { package_names } + } +} + +impl From for Event { + fn from(it: CargoWorkspace) -> Self { + Message::CargoWorkspace(it).into() + } +}