Skip to content

Commit

Permalink
feat: Allow to serialize all Cargo Options with Serde. (#15)
Browse files Browse the repository at this point in the history
* feat: Allow to serialize all Cargo Options with Serde.

This change adds an optional feature that makes all Cargo Options serializable and deserializable.
This allows to use this library to read and write Cargo Options in json, toml, or any other file format
supported by Serde.

* Update Cargo.toml

Co-authored-by: messense <[email protected]>

* Update feature name.

* Apply serde default to allow missing fields.

---------

Co-authored-by: messense <[email protected]>
  • Loading branch information
calavera and messense authored Dec 5, 2024
1 parent c71b706 commit 3d52290
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ repository = "https://github.com/messense/cargo-options"
[dependencies]
anstyle = "1.0.2"
clap = { version = "4.0.0", features = ["derive", "env", "wrap_help", "unstable-styles"] }
serde = { version = "1", features = ["derive"], optional = true }

[dev-dependencies]
trycmd = { version = "0.14.0", features = ["examples"] }

[features]
serde = ["dep:serde"]
26 changes: 26 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::process::Command;

use clap::{ArgAction, Parser};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::common::CommonOptions;
use crate::heading;

Expand All @@ -14,24 +17,30 @@ use crate::heading;
after_help = "Run `cargo help build` for more detailed information."
)]
#[group(skip)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Build {
#[command(flatten)]
#[cfg_attr(feature = "serde", serde(flatten))]
pub common: CommonOptions,

/// Path to Cargo.toml
#[arg(long, value_name = "PATH", help_heading = heading::MANIFEST_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub manifest_path: Option<PathBuf>,

/// Build artifacts in release mode, with optimizations
#[arg(short = 'r', long, help_heading = heading::COMPILATION_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub release: bool,

/// Ignore `rust-version` specification in packages
#[arg(long)]
#[cfg_attr(feature = "serde", serde(default))]
pub ignore_rust_version: bool,

/// Output build graph in JSON (unstable)
#[arg(long, help_heading = heading::COMPILATION_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub unit_graph: bool,

/// Package to build (see `cargo help pkgid`)
Expand All @@ -43,10 +52,12 @@ pub struct Build {
num_args=0..=1,
help_heading = heading::PACKAGE_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub packages: Vec<String>,

/// Build all packages in the workspace
#[arg(long, help_heading = heading::PACKAGE_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub workspace: bool,

/// Exclude packages from the build
Expand All @@ -56,14 +67,17 @@ pub struct Build {
action = ArgAction::Append,
help_heading = heading::PACKAGE_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub exclude: Vec<String>,

/// Alias for workspace (deprecated)
#[arg(long, help_heading = heading::PACKAGE_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub all: bool,

/// Build only this package's library
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub lib: bool,

/// Build only the specified binary
Expand All @@ -74,10 +88,12 @@ pub struct Build {
num_args=0..=1,
help_heading = heading::TARGET_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub bin: Vec<String>,

/// Build all binaries
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub bins: bool,

/// Build only the specified example
Expand All @@ -88,10 +104,12 @@ pub struct Build {
num_args=0..=1,
help_heading = heading::TARGET_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub example: Vec<String>,

/// Build all examples
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub examples: bool,

/// Build only the specified test target
Expand All @@ -101,10 +119,12 @@ pub struct Build {
action = ArgAction::Append,
help_heading = heading::TARGET_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub test: Vec<String>,

/// Build all tests
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub tests: bool,

/// Build only the specified bench target
Expand All @@ -114,26 +134,32 @@ pub struct Build {
action = ArgAction::Append,
help_heading = heading::TARGET_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub bench: Vec<String>,

/// Build all benches
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub benches: bool,

/// Build all targets
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub all_targets: bool,

/// Copy final artifacts to this directory (unstable)
#[arg(long, alias = "out-dir", value_name = "PATH", help_heading = heading::COMPILATION_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub artifact_dir: Option<PathBuf>,

/// Output the build plan in JSON (unstable)
#[arg(long, help_heading = heading::COMPILATION_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub build_plan: bool,

/// Outputs a future incompatibility report at the end of the build (unstable)
#[arg(long)]
#[cfg_attr(feature = "serde", serde(default))]
pub future_incompat_report: bool,
}

Expand Down
26 changes: 26 additions & 0 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ use std::process::Command;

use clap::{ArgAction, Parser};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::common::CommonOptions;
use crate::heading;

/// `cargo check` options which are also a subset of `cargo clippy`
#[derive(Clone, Debug, Default, Parser)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct CheckOptions {
/// Package to build (see `cargo help pkgid`)
#[arg(
Expand All @@ -19,10 +23,12 @@ pub struct CheckOptions {
num_args=0..=1,
help_heading = heading::PACKAGE_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub packages: Vec<String>,

/// Check all packages in the workspace
#[arg(long, help_heading = heading::PACKAGE_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub workspace: bool,

/// Exclude packages from the build
Expand All @@ -32,14 +38,17 @@ pub struct CheckOptions {
action = ArgAction::Append,
help_heading = heading::PACKAGE_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub exclude: Vec<String>,

/// Alias for workspace (deprecated)
#[arg(long, help_heading = heading::PACKAGE_SELECTION,)]
#[cfg_attr(feature = "serde", serde(default))]
pub all: bool,

/// Check only this package's library
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub lib: bool,

/// Check only the specified binary
Expand All @@ -50,10 +59,12 @@ pub struct CheckOptions {
num_args=0..=1,
help_heading = heading::TARGET_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub bin: Vec<String>,

/// Check all binaries
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub bins: bool,

/// Check only the specified example
Expand All @@ -64,10 +75,12 @@ pub struct CheckOptions {
num_args=0..=1,
help_heading = heading::TARGET_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub example: Vec<String>,

/// Check all examples
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub examples: bool,

/// Check only the specified test target
Expand All @@ -77,10 +90,12 @@ pub struct CheckOptions {
action = ArgAction::Append,
help_heading = heading::TARGET_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub test: Vec<String>,

/// Check all tests
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub tests: bool,

/// Check only the specified bench target
Expand All @@ -90,18 +105,22 @@ pub struct CheckOptions {
action = ArgAction::Append,
help_heading = heading::TARGET_SELECTION,
)]
#[cfg_attr(feature = "serde", serde(default))]
pub bench: Vec<String>,

/// Check all benches
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub benches: bool,

/// Check all targets
#[arg(long, help_heading = heading::TARGET_SELECTION)]
#[cfg_attr(feature = "serde", serde(default))]
pub all_targets: bool,

/// Outputs a future incompatibility report at the end of the build (unstable)
#[arg(long)]
#[cfg_attr(feature = "serde", serde(default))]
pub future_incompat_report: bool,
}

Expand Down Expand Up @@ -162,27 +181,34 @@ impl CheckOptions {
after_help = "Run `cargo help check` for more detailed information."
)]
#[group(skip)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Check {
#[command(flatten)]
#[cfg_attr(feature = "serde", serde(flatten))]
pub common: CommonOptions,

#[command(flatten)]
#[cfg_attr(feature = "serde", serde(flatten))]
pub check: CheckOptions,

/// Path to Cargo.toml
#[arg(long, value_name = "PATH", help_heading = heading::MANIFEST_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub manifest_path: Option<PathBuf>,

/// Build artifacts in release mode, with optimizations
#[arg(short = 'r', long, help_heading = heading::COMPILATION_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub release: bool,

/// Ignore `rust-version` specification in packages
#[arg(long)]
#[cfg_attr(feature = "serde", serde(default))]
pub ignore_rust_version: bool,

/// Output build graph in JSON (unstable)
#[arg(long, help_heading = heading::COMPILATION_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub unit_graph: bool,
}

Expand Down
13 changes: 13 additions & 0 deletions src/clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use std::process::Command;

use clap::Parser;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::check::CheckOptions;
use crate::common::CommonOptions;
use crate::heading;
Expand All @@ -15,39 +18,49 @@ use crate::heading;
after_help = "Run `cargo help clippy` for more detailed information."
)]
#[group(skip)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Clippy {
#[command(flatten)]
#[cfg_attr(feature = "serde", serde(flatten))]
pub common: CommonOptions,

#[command(flatten)]
#[cfg_attr(feature = "serde", serde(flatten))]
pub check: CheckOptions,

/// Path to Cargo.toml
#[arg(long, value_name = "PATH", help_heading = heading::MANIFEST_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub manifest_path: Option<PathBuf>,

/// Build artifacts in release mode, with optimizations
#[arg(short = 'r', long, help_heading = heading::COMPILATION_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub release: bool,

/// Ignore `rust-version` specification in packages
#[arg(long)]
#[cfg_attr(feature = "serde", serde(default))]
pub ignore_rust_version: bool,

/// Output build graph in JSON (unstable)
#[arg(long, help_heading = heading::COMPILATION_OPTIONS)]
#[cfg_attr(feature = "serde", serde(default))]
pub unit_graph: bool,

/// Ignore dependencies, run only on crate
#[arg(long)]
#[cfg_attr(feature = "serde", serde(default))]
pub no_deps: bool,

/// Automatically apply lint suggestions (see `cargo help clippy`)
#[arg(long)]
#[cfg_attr(feature = "serde", serde(default))]
pub fix: bool,

/// Arguments passed to rustc.
#[arg(value_name = "args", trailing_var_arg = true, num_args = 0..)]
#[cfg_attr(feature = "serde", serde(default))]
pub args: Vec<String>,
}

Expand Down
Loading

0 comments on commit 3d52290

Please sign in to comment.