Skip to content

Commit

Permalink
Merge #328
Browse files Browse the repository at this point in the history
328: with_prefix macro - Visibility qualifier for generated module r=jonasbb a=elpiel

Closes #327 

I'm not sure how to document this and what is the preferred way to add the tests, so I decided to open a PR directly

Co-authored-by: Lachezar Lechev <elpiel93@gmail.com>
Co-authored-by: Jonas Bushart <jonas@bushart.org>
  • Loading branch information
3 people committed Jun 18, 2021
2 parents 42225c4 + a2a30d3 commit 7dca3d2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 32 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [1.9.4] - 2021-06-18

### Fixed

* `with_prefix!` now supports an optional visibility modifier. (#327, #328)
If not specified `pub(self)` is assumed.

```rust
with_prefix!(prefix_active "active_"); // => mod {...}
with_prefix!(pub prefix_active "active_"); // => pub mod {...}
with_prefix!(pub(crate) prefix_active "active_"); // => pub(crate) mod {...}
with_prefix!(pub(in other_mod) prefix_active "active_"); // => pub(in other_mod) mod {...}
```

Thanks to @elpiel for raising and fixing the issue.

## [1.9.3] - 2021-06-14

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ authors = [
"Marcin Kaźmierczak",
]
name = "serde_with"
version = "1.9.3"
version = "1.9.4"

categories = ["encoding"]
description = "Custom de/serialization functions for Rust's serde"
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies.serde_with]
version = "1.9.3"
version = "1.9.4"
features = [ "..." ]
```

Expand Down Expand Up @@ -124,15 +124,15 @@ Foo {
}
```

[`DisplayFromStr`]: https://docs.rs/serde_with/1.9.3/serde_with/struct.DisplayFromStr.html
[`with_prefix!`]: https://docs.rs/serde_with/1.9.3/serde_with/macro.with_prefix.html
[display_fromstr]: https://docs.rs/serde_with/1.9.3/serde_with/rust/display_fromstr/index.html
[feature flags]: https://docs.rs/serde_with/1.9.3/serde_with/guide/feature_flags/index.html
[skip_serializing_none]: https://docs.rs/serde_with/1.9.3/serde_with/attr.skip_serializing_none.html
[StringWithSeparator]: https://docs.rs/serde_with/1.9.3/serde_with/rust/struct.StringWithSeparator.html
[user guide]: https://docs.rs/serde_with/1.9.3/serde_with/guide/index.html
[`DisplayFromStr`]: https://docs.rs/serde_with/1.9.4/serde_with/struct.DisplayFromStr.html
[`with_prefix!`]: https://docs.rs/serde_with/1.9.4/serde_with/macro.with_prefix.html
[display_fromstr]: https://docs.rs/serde_with/1.9.4/serde_with/rust/display_fromstr/index.html
[feature flags]: https://docs.rs/serde_with/1.9.4/serde_with/guide/feature_flags/index.html
[skip_serializing_none]: https://docs.rs/serde_with/1.9.4/serde_with/attr.skip_serializing_none.html
[StringWithSeparator]: https://docs.rs/serde_with/1.9.4/serde_with/rust/struct.StringWithSeparator.html
[user guide]: https://docs.rs/serde_with/1.9.4/serde_with/guide/index.html
[with-annotation]: https://serde.rs/field-attrs.html#with
[as-annotation]: https://docs.rs/serde_with/1.9.3/serde_with/guide/serde_as/index.html
[as-annotation]: https://docs.rs/serde_with/1.9.4/serde_with/guide/serde_as/index.html

## License

Expand Down
8 changes: 4 additions & 4 deletions serde_with_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ fn field_has_attribute(field: &Field, namespace: &str, name: &str) -> bool {
/// }
/// ```
///
/// [`serde_as`]: https://docs.rs/serde_with/1.9.3/serde_with/guide/index.html
/// [re-exporting `serde_as`]: https://docs.rs/serde_with/1.9.3/serde_with/guide/serde_as/index.html#re-exporting-serde_as
/// [`serde_as`]: https://docs.rs/serde_with/1.9.4/serde_with/guide/index.html
/// [re-exporting `serde_as`]: https://docs.rs/serde_with/1.9.4/serde_with/guide/serde_as/index.html#re-exporting-serde_as
#[proc_macro_attribute]
pub fn serde_as(args: TokenStream, input: TokenStream) -> TokenStream {
#[derive(FromMeta, Debug)]
Expand Down Expand Up @@ -783,7 +783,7 @@ fn replace_infer_type_with_type(to_replace: Type, replacement: &Type) -> Type {
/// [`Display`]: std::fmt::Display
/// [`FromStr`]: std::str::FromStr
/// [cargo-toml-rename]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml
/// [serde-as-crate]: https://docs.rs/serde_with/1.9.3/serde_with/guide/serde_as/index.html#re-exporting-serde_as
/// [serde-as-crate]: https://docs.rs/serde_with/1.9.4/serde_with/guide/serde_as/index.html#re-exporting-serde_as
/// [serde-crate]: https://serde.rs/container-attrs.html#crate
#[proc_macro_derive(DeserializeFromStr, attributes(serde_with))]
pub fn derive_deserialize_fromstr(item: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -893,7 +893,7 @@ fn deserialize_fromstr(input: DeriveInput, serde_with_crate_path: Path) -> Token
/// [`Display`]: std::fmt::Display
/// [`FromStr`]: std::str::FromStr
/// [cargo-toml-rename]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml
/// [serde-as-crate]: https://docs.rs/serde_with/1.9.3/serde_with/guide/serde_as/index.html#re-exporting-serde_as
/// [serde-as-crate]: https://docs.rs/serde_with/1.9.4/serde_with/guide/serde_as/index.html#re-exporting-serde_as
/// [serde-crate]: https://serde.rs/container-attrs.html#crate
#[proc_macro_derive(SerializeDisplay, attributes(serde_with))]
pub fn derive_serialize_display(item: TokenStream) -> TokenStream {
Expand Down
30 changes: 15 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#![doc(test(attr(warn(rust_2018_idioms))))]
// Not needed for 2018 edition and conflicts with `rust_2018_idioms`
#![doc(test(no_crate_inject))]
#![doc(html_root_url = "https://docs.rs/serde_with/1.9.3")]
#![doc(html_root_url = "https://docs.rs/serde_with/1.9.4")]
// Necessary to silence the warning about clippy::unknown_clippy_lints on nightly
#![allow(renamed_and_removed_lints)]
// Rust 1.45: introduction of `strip_prefix` used by clippy::manual_strip
Expand Down Expand Up @@ -61,7 +61,7 @@
//!
//! ```toml
//! [dependencies.serde_with]
//! version = "1.9.3"
//! version = "1.9.4"
//! features = [ "..." ]
//! ```
//!
Expand Down Expand Up @@ -200,15 +200,15 @@
//! # }
//! ```
//!
//! [`DisplayFromStr`]: https://docs.rs/serde_with/1.9.3/serde_with/struct.DisplayFromStr.html
//! [`with_prefix!`]: https://docs.rs/serde_with/1.9.3/serde_with/macro.with_prefix.html
//! [display_fromstr]: https://docs.rs/serde_with/1.9.3/serde_with/rust/display_fromstr/index.html
//! [feature flags]: https://docs.rs/serde_with/1.9.3/serde_with/guide/feature_flags/index.html
//! [skip_serializing_none]: https://docs.rs/serde_with/1.9.3/serde_with/attr.skip_serializing_none.html
//! [StringWithSeparator]: https://docs.rs/serde_with/1.9.3/serde_with/rust/struct.StringWithSeparator.html
//! [user guide]: https://docs.rs/serde_with/1.9.3/serde_with/guide/index.html
//! [`DisplayFromStr`]: https://docs.rs/serde_with/1.9.4/serde_with/struct.DisplayFromStr.html
//! [`with_prefix!`]: https://docs.rs/serde_with/1.9.4/serde_with/macro.with_prefix.html
//! [display_fromstr]: https://docs.rs/serde_with/1.9.4/serde_with/rust/display_fromstr/index.html
//! [feature flags]: https://docs.rs/serde_with/1.9.4/serde_with/guide/feature_flags/index.html
//! [skip_serializing_none]: https://docs.rs/serde_with/1.9.4/serde_with/attr.skip_serializing_none.html
//! [StringWithSeparator]: https://docs.rs/serde_with/1.9.4/serde_with/rust/struct.StringWithSeparator.html
//! [user guide]: https://docs.rs/serde_with/1.9.4/serde_with/guide/index.html
//! [with-annotation]: https://serde.rs/field-attrs.html#with
//! [as-annotation]: https://docs.rs/serde_with/1.9.3/serde_with/guide/serde_as/index.html
//! [as-annotation]: https://docs.rs/serde_with/1.9.4/serde_with/guide/serde_as/index.html

#[doc(hidden)]
pub extern crate serde;
Expand Down Expand Up @@ -344,7 +344,7 @@ impl Separator for CommaSeparator {
/// # }
/// ```
///
/// [serde_as]: https://docs.rs/serde_with/1.9.3/serde_with/attr.serde_as.html
/// [serde_as]: https://docs.rs/serde_with/1.9.4/serde_with/attr.serde_as.html
#[derive(Copy, Clone, Debug, Default)]
pub struct As<T: ?Sized>(PhantomData<T>);

Expand Down Expand Up @@ -810,7 +810,7 @@ pub struct BytesOrString;
/// ```
///
/// [`chrono::Duration`]: chrono_crate::Duration
/// [feature flag]: https://docs.rs/serde_with/1.9.3/serde_with/guide/feature_flags/index.html
/// [feature flag]: https://docs.rs/serde_with/1.9.4/serde_with/guide/feature_flags/index.html
#[derive(Copy, Clone, Debug, Default)]
pub struct DurationSeconds<
FORMAT: formats::Format = u64,
Expand Down Expand Up @@ -936,7 +936,7 @@ pub struct DurationSeconds<
/// ```
///
/// [`chrono::Duration`]: chrono_crate::Duration
/// [feature flag]: https://docs.rs/serde_with/1.9.3/serde_with/guide/feature_flags/index.html
/// [feature flag]: https://docs.rs/serde_with/1.9.4/serde_with/guide/feature_flags/index.html
#[derive(Copy, Clone, Debug, Default)]
pub struct DurationSecondsWithFrac<
FORMAT: formats::Format = f64,
Expand Down Expand Up @@ -1133,7 +1133,7 @@ pub struct DurationNanoSecondsWithFrac<
///
/// [`SystemTime`]: std::time::SystemTime
/// [DateTime]: chrono_crate::DateTime
/// [feature flag]: https://docs.rs/serde_with/1.9.3/serde_with/guide/feature_flags/index.html
/// [feature flag]: https://docs.rs/serde_with/1.9.4/serde_with/guide/feature_flags/index.html
#[derive(Copy, Clone, Debug, Default)]
pub struct TimestampSeconds<
FORMAT: formats::Format = i64,
Expand Down Expand Up @@ -1262,7 +1262,7 @@ pub struct TimestampSeconds<
///
/// [`SystemTime`]: std::time::SystemTime
/// [DateTime]: chrono_crate::DateTime
/// [feature flag]: https://docs.rs/serde_with/1.9.3/serde_with/guide/feature_flags/index.html
/// [feature flag]: https://docs.rs/serde_with/1.9.4/serde_with/guide/feature_flags/index.html
#[derive(Copy, Clone, Debug, Default)]
pub struct TimestampSecondsWithFrac<
FORMAT: formats::Format = f64,
Expand Down
12 changes: 9 additions & 3 deletions src/with_prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ use std::fmt;
/// Serialize with an added prefix on every field name and deserialize by
/// trimming away the prefix.
///
/// You can set the visibility of the generated module by prefixing the module name with a module visibility.
/// `with_prefix!(pub(crate) prefix_foo "foo_");` creates a module with `pub(crate)` visibility.
/// The visibility is optional and by default `pub(self)`, i.e., private visibility is assumed.
///
/// **Note:** Use of this macro is incompatible with applying the [`deny_unknown_fields`] attribute
/// on the container.
/// While deserializing, it will always warn about unknown fields, even though they are processed
Expand Down Expand Up @@ -70,7 +74,8 @@ use std::fmt;
/// }
///
/// with_prefix!(prefix_player1 "player1_");
/// with_prefix!(prefix_player2 "player2_");
/// // You can also set the visibility of the generated prefix module, the default is private.
/// with_prefix!(pub prefix_player2 "player2_");
/// #
/// # const EXPECTED: &str = r#"{
/// # "player1_name": "name1",
Expand Down Expand Up @@ -102,8 +107,9 @@ use std::fmt;
/// [issue-with_prefix-deny_unknown_fields]: https://github.com/jonasbb/serde_with/issues/57
#[macro_export]
macro_rules! with_prefix {
($module:ident $prefix:expr) => {
mod $module {
($module:ident $prefix:expr) => {$crate::with_prefix!(pub(self) $module $prefix);};
($vis:vis $module:ident $prefix:expr) => {
$vis mod $module {
use $crate::serde::{Deserialize, Deserializer, Serialize, Serializer};
use $crate::with_prefix::WithPrefix;

Expand Down

0 comments on commit 7dca3d2

Please sign in to comment.