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
Add 'schemars' feature for deriving JSON schema #31
Conversation
+1 on this from me. Although, you can probably get away with not using serde by deriving the JsonSchema yourself. |
Cargo.toml
Outdated
[features] | ||
default = [] | ||
# Implements "serde::Serialize" and "serde::Deserialize". | ||
serde = ["serde_crate"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just by making the crate optional (optional = true
, as already done) you can enable it using a feature flag of the crate name itself.
So then this line is not needed and serde
feature can just be used immediately.
For more info: https://stackoverflow.com/a/39759592/2037998
This way some of the changes in src/ipnet_serde.rs
are no longer needed to. That will make the PR a bit simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just pushed something, take a look. Let me know if that is good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see the changes anywhere. Where did you push them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and collapsed this, avoiding the renaming.
cargo build/test --features serde
and cargo build/test --features json
are both happy.
Thanks @smklein, @ralpha, and @xfbs. Sorry for the long delay. Per the conversation. Shouldn't we be able to just have |
Sounds good! |
Yes that would work. But the change in: - match try!(data.variant()) {
+ match data.variant()? { I would keep too. The |
Avoided making |
#[cfg(feature = "serde")] | ||
extern crate serde_crate; | ||
extern crate serde; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code can be removed. This is no longer needed in Rust with edition = "2018"
, so the edition might have to be set. But can also happen in other PR.
If this compiles and works it looks all good to me. @krisprice I might suggest added an edition to the crate, best to use Other then those remarks it looks all good. |
Context
schemars is a library which allows emission of JSON schema documents based purely on a type. It's a lot like serde (and has compatibility with serde) but allows emission of JSON schemas without a "value" - it operates purely based on type.
This is super handy for things that consume JSON schemas to define types - at Oxide (my employer), we use schemars to produce OpenAPI specs for our interfaces (and consequently, types that appear in our APIs derive
Serialize
,Deserialize
, andJsonSchema
).Similar to the serde types,
derive(JsonSchema)
only works if all the contained types also implementJsonSchema
.Okay, what about this PR tho
This PR adds a new "feature" to ipnet, enabling the derivation of the
JsonSchema
type forIpNet
types.Similar to the existing
serde
feature, a newjson
feature provides a variant of this crate with the derives enabled.For callers who want to use it: This enables usage of the
IpNet
types within otherJsonSchema
objects.For callers who don't want it: It's behind a feature flag, so it remains completely unnoticeable (and has no impact on the compiled library).