Skip to content

Commit

Permalink
Skip serialization for bool fields if the value matches the default…
Browse files Browse the repository at this point in the history
… value (#21)
  • Loading branch information
Turbo87 authored Sep 18, 2023
1 parent c5c52e1 commit 6485dcf
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ fn default_true() -> bool {
true
}

fn is_true(value: &bool) -> bool {
*value
}

impl Manifest<Value> {
/// Parse contents of a `Cargo.toml` file already loaded as a byte slice.
///
Expand Down Expand Up @@ -420,21 +424,21 @@ pub struct Product {
pub name: Option<String>,

/// A flag for enabling unit tests for this product. This is used by `cargo test`.
#[serde(default = "default_true")]
#[serde(default = "default_true", skip_serializing_if = "is_true")]
pub test: bool,

/// A flag for enabling documentation tests for this product. This is only relevant
/// for libraries, it has no effect on other sections. This is used by
/// `cargo test`.
#[serde(default = "default_true")]
#[serde(default = "default_true", skip_serializing_if = "is_true")]
pub doctest: bool,

/// A flag for enabling benchmarks for this product. This is used by `cargo bench`.
#[serde(default = "default_true")]
#[serde(default = "default_true", skip_serializing_if = "is_true")]
pub bench: bool,

/// A flag for enabling documentation of this product. This is used by `cargo doc`.
#[serde(default = "default_true")]
#[serde(default = "default_true", skip_serializing_if = "is_true")]
pub doc: bool,

/// If the product is meant to be a compiler plugin, this field must be set to true
Expand All @@ -450,7 +454,7 @@ pub struct Product {
/// If set to false, `cargo test` will omit the `--test` flag to rustc, which
/// stops it from generating a test harness. This is useful when the binary being
/// built manages the test runner itself.
#[serde(default = "default_true")]
#[serde(default = "default_true", skip_serializing_if = "is_true")]
pub harness: bool,

/// If set then a product can be configured to use a different edition than the
Expand Down Expand Up @@ -725,13 +729,13 @@ pub struct Package<Metadata = Value> {
/// The default binary to run by cargo run.
pub default_run: Option<String>,

#[serde(default = "default_true")]
#[serde(default = "default_true", skip_serializing_if = "is_true")]
pub autobins: bool,
#[serde(default = "default_true")]
#[serde(default = "default_true", skip_serializing_if = "is_true")]
pub autoexamples: bool,
#[serde(default = "default_true")]
#[serde(default = "default_true", skip_serializing_if = "is_true")]
pub autotests: bool,
#[serde(default = "default_true")]
#[serde(default = "default_true", skip_serializing_if = "is_true")]
pub autobenches: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub publish: Option<MaybeInherited<Publish>>,
Expand Down

0 comments on commit 6485dcf

Please sign in to comment.