Skip to content

Commit

Permalink
Report the CargoWorkspace event
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
foresterre committed Oct 18, 2024
1 parent 5c396b5 commit 7c100e7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FindContext> {
if let Self::Find(ctx) = self {
Expand Down Expand Up @@ -314,15 +324,22 @@ impl EnvironmentContext {

#[derive(Clone, Debug)]
pub struct WorkspacePackages {
_selected: Vec<cargo_metadata::Package>,
selected: Vec<cargo_metadata::Package>,
}

impl WorkspacePackages {
pub fn from_iter(selected: impl IntoIterator<Item = cargo_metadata::Package>) -> Self {
Self {
_selected: selected.into_iter().collect(),
selected: selected.into_iter().collect(),
}
}

pub fn names(&self) -> Vec<String> {
self.selected
.iter()
.map(|pkg| pkg.name.to_string())
.collect()
}
}

#[derive(Clone, Copy, Debug, Default, PartialEq, ValueEnum)]
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/reporter/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,7 @@ mod types;

// specific events
mod auxiliary_output;
mod cargo_workspace;
mod check_method;
mod check_result;
mod check_toolchain;
Expand Down Expand Up @@ -123,6 +125,9 @@ pub enum Message {
// setup
Meta(Meta),

// package selection
CargoWorkspace(CargoWorkspace),

// get rust-releases index
FetchIndex(FetchIndex), // todo!
UnableToConfirmValidReleaseVersion(UnableToConfirmValidReleaseVersion),
Expand Down
19 changes: 19 additions & 0 deletions src/reporter/event/cargo_workspace.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

impl CargoWorkspace {
pub fn new(package_names: Vec<String>) -> Self {
Self { package_names }
}
}

impl From<CargoWorkspace> for Event {
fn from(it: CargoWorkspace) -> Self {
Message::CargoWorkspace(it).into()
}
}

0 comments on commit 7c100e7

Please sign in to comment.