Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Serialization and Deserialization for Verbosity #114

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

joshka
Copy link
Contributor

@joshka joshka commented Sep 20, 2024

Verbosity is serialized and deserialized using the title case of the
VerbosityFilter (e.g. "Debug")

The serde dependency is gated behind an optional feature flag.

Added conversion methods between Verbosity and VerbosityFilter to
simplify the implementation and derived PartialEq, Eq impls on
types where this was necesary for testing.

Fixes: #88

src/lib.rs Outdated Show resolved Hide resolved
src/serde.rs Fixed Show fixed Hide fixed
src/serde.rs Fixed Show fixed Hide fixed
src/serde.rs Fixed Show fixed Hide fixed
src/serde.rs Fixed Show fixed Hide fixed
src/serde.rs Fixed Show fixed Hide fixed
src/serde.rs Fixed Show fixed Hide fixed
src/lib.rs Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Sep 20, 2024

Pull Request Test Coverage Report for Build 12205777991

Details

  • 8 of 12 (66.67%) changed or added relevant lines in 1 file are covered.
  • 8 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.5%) to 49.541%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/lib.rs 8 12 66.67%
Files with Coverage Reduction New Missed Lines %
src/lib.rs 8 49.28%
Totals Coverage Status
Change from base Build 12101205082: -0.5%
Covered Lines: 54
Relevant Lines: 109

💛 - Coveralls

src/serde.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
src/serde.rs Outdated Show resolved Hide resolved
Copy link
Contributor Author

@joshka joshka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will:

  • remove rstest
  • serialize to/from LevelFilter instead of Option(Level)
  • add a specific test for TOML serialization
  • Fix the github lints
  • split the i8 change out to another commit (and will raise another PR for it if necessary)

Cargo.toml Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/serde.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
Copy link
Contributor Author

@joshka joshka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving to LevelFilter made it easy to derive serialize / deserialize using From<LevelFilter> instead of a custom implementation.

Cargo.toml Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/serde.rs Outdated Show resolved Hide resolved
src/serde.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Outdated
}

// round-trips
let toml = "meaning_of_life = 42\nverbose = \"DEBUG\"\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I feel about deserializing to all-caps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this irks me a little too, but being consistent with the existing behavior won out over my ideal approach. If I was designing the log crate, I think I'd have made that serialization lower case too.

Here, this can deserialize from either case, but serializes to upper case. It comes from the serialization of log::LevelFilter. That means upper case is consistent with any other serialization that works with log level serialization (and is consistent with the usual display of the level in log outputs).

If you did want to avoid this, it would require going back to using a custom serialization implementation. This doesn't seem worth the extra code and tests to me. I prefer the simplicity even if that comes with a slightly ugly default which is consistent with other things doing the same thing.

I also suspect the serialization side of this will be used much less frequently than the deserialization side, and the serialization side will also often be more focused on programs re-reading the format rather than humans.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On one hand, if people are serializing LevelFilter, this keeps us consistent with that. On the other hand, this will produce results inconsistent with every other serialized value. This isn't just important here but for other cases like schemars. I think I want to let this sit for a time to mull over this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no problem :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eb58de2 serializes to VerbosityFilter instead of this, which doesn't have a deserialization implementation that handles upper case tokens.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That replaced SCREAMING CASE with Title case

assert_eq!(toml, "meaning_of_life = 42\nverbosity = \"Debug\"\n");

I feel like lower case is much more likely to align with a users configuration format design (as most are likely kebab-case or snake_case)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw eb58de2 just changed the value but it doesn't answer the question about interoperability

src/lib.rs Fixed Show fixed Hide fixed
src/lib.rs Fixed Show fixed Hide fixed
src/lib.rs Fixed Show fixed Hide fixed
@joshka joshka force-pushed the jm/serde branch 2 times, most recently from 2a3fb79 to 99d104e Compare September 26, 2024 19:14
src/lib.rs Fixed Show resolved Hide resolved
@joshka
Copy link
Contributor Author

joshka commented Nov 29, 2024

In eb58de2, I rewrote this based on the 3.0 release. Notably this uses the VerbosityFilter to de/serialize instead of log::LogLevelFilter which means that it only handles Title case deserialization. I've also simplified the style and quantity of tests significantly.

Verbosity is serialized and deserialized using the title case of the
VerbosityFilter (e.g. "Debug")

The `serde` dependency is gated behind an optional feature flag.

Added conversion methods between Verbosity and VerbosityFilter to
simplify the implementation and derived PartialEq, Eq impls on
types where this was necesary for testing.

Fixes: clap-rs#88
@@ -70,8 +70,21 @@ pub mod log;
pub mod tracing;

/// Logging flags to `#[command(flatten)]` into your CLI
#[derive(clap::Args, Debug, Clone, Default)]
#[derive(clap::Args, Debug, Clone, Default, PartialEq, Eq)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR seems to be mixing a lot of different things

  • Making types support Eq
  • More From impls
  • A refactor with how the count math is done

etc

Ideally these would at least be split into individual commits

  • We can track them in release notes more easily
  • I can better see how all of the parts for a single change fit together

Cargo.toml Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Serialize & Deserialize
3 participants