Skip to content

Commit

Permalink
fix: use serde-env-field-wrap 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mrshiposha committed Nov 7, 2023
1 parent 07dfdff commit b59e44d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "serde-env-field"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
authors = ["Daniel Shiposha"]
license = "MIT"
Expand All @@ -14,7 +14,7 @@ keywords = ["serde", "environment", "variables", "env", "proc-macro"]
serde = { version = "1.0", features = ["derive"] }
serde-untagged = "0.1.1"
shellexpand = "3.1.0"
serde-env-field-wrap = "0.1.1"
serde-env-field-wrap = "0.3.0"

[dev-dependencies]
derive_more = "0.99.17"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ will attempt to deserialize the `T` type directly from the data.

The `EnvField` works nicely with `Option`, `Vec`, and `#[serde(default)]`.

Also, the crate provides the `env_field_wrap` attribute that wraps all the fields of a struct or an enum with the `EnvField` type.
Also, the crate provides the [env_field_wrap](https://docs.rs/serde-env-field/latest/serde_env_field/attr.env_field_wrap.html) attribute that wraps all the fields of a struct or an enum with the `EnvField` type.
The attribute also honors the optional and vector fields.

#### `EnvField` Example
Expand Down
27 changes: 21 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,34 +263,49 @@ use serde_untagged::{de::Error as UntaggedError, UntaggedEnumVisitor};
///
/// ```
/// # use serde::{Serialize, Deserialize};
/// # use serde_env_field::env_field_wrap;
/// # use serde_env_field::{env_field_wrap, EnvField, UseDeserialize};
/// #[env_field_wrap]
/// #[derive(Serialize, Deserialize)]
/// struct Example {
/// // Will become `TwoGenerics<EnvField<String>, EnvField<i32>>`
/// // instead of `EnvField<TwoGenerics<String, i32>>`.
/// // Will become
/// // `Generics<EnvField<String>, EnvField<i32>, EnvField<Variants, UseDeserialize>>`
/// // instead of
/// // `EnvField<Generics<String, i32, EnvField<Variants, UseDeserialize>>>`.
/// //
/// // Note: the `TwoGenerics` don't need to implement the `FromStr` in this case.
/// // Note:
/// // * if a generic is already wrapped into the `EnvField`, it *won't* be wrapped again.
/// // * the `Generics` don't need to implement the `FromStr` in this case.
/// #[env_field_wrap(generics_only)]
/// generics: TwoGenerics<String, i32>,
/// generics: Generics<String, i32, EnvField<Variants, UseDeserialize>>,
/// }
///
/// #[derive(Serialize, Deserialize)]
/// struct TwoGenerics<A, B> {
/// struct Generics<A, B, C> {
/// a: A,
/// b: B,
/// c: C,
/// }
///
/// #[derive(Serialize, Deserialize, Debug)]
/// #[serde(rename_all = "kebab-case")]
/// enum Variants {
/// FirstVariant,
/// SecondVariant,
/// }
///
/// std::env::set_var("GENERICS_STR", "env string");
/// std::env::set_var("GENERICS_I32", "517");
/// std::env::set_var("GENERICS_VARIANT", "first-variant");
/// let de: Example = toml::from_str(r#"
/// [generics]
/// a = "$GENERICS_STR"
/// b = "$GENERICS_I32"
/// c = "$GENERICS_VARIANT"
/// "#).unwrap();
///
/// assert_eq!(&de.generics.a, "env string");
/// assert_eq!(de.generics.b, 517);
/// assert!(matches!(*de.generics.c, Variants::FirstVariant));
///
/// ```
pub use serde_env_field_wrap::env_field_wrap;
Expand Down
16 changes: 14 additions & 2 deletions tests/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,31 +630,43 @@ fn test_wrap_generics_only() {
#[derive(Serialize, Deserialize, Debug)]
struct Test {
#[env_field_wrap(generics_only)]
generics: TwoGenerics<String, i32>,
generics: Generics<String, i32, EnvField<Variants, UseDeserialize>>,
}

#[derive(Serialize, Deserialize, Debug)]
struct TwoGenerics<A, B> {
struct Generics<A, B, C> {
a: A,
b: B,
c: C,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
enum Variants {
FirstVariant,
SecondVariant,
}

env::set_var("GENERICS_STR", "env string");
env::set_var("GENERICS_I32", "517");
env::set_var("GENERICS_VARIANT", "first-variant");
de_se_de_test::<Test>(
r#"
[generics]
a = "$GENERICS_STR"
b = "$GENERICS_I32"
c = "$GENERICS_VARIANT"
"#,
|de| {
assert_eq!(&de.generics.a, "env string");
assert_eq!(de.generics.b, 517);
assert!(matches!(*de.generics.c, Variants::FirstVariant));
},
indoc! {r#"
[generics]
a = "env string"
b = 517
c = "first-variant"
"#},
);

Expand Down

0 comments on commit b59e44d

Please sign in to comment.