Skip to content

Commit

Permalink
Merge pull request #2 from N4D1K-lgtm/dev
Browse files Browse the repository at this point in the history
fix: Corrected derived `From` implementation
  • Loading branch information
N4D1K-lgtm authored May 4, 2024
2 parents ba33a13 + 41c024f commit 4676dee
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ assert!(settings.debug)
- The `prefix` is applied per field or for the entire struct, but is ignored if `#[config(key = "_")]` is used.
- All configuration structs must implement [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html).
- Custom types used in configuration struct fields must implement [`Deserialize`](https://docs.rs/serde/latest/serde/trait.Deserialize.html),
[`Clone`](https://doc.rust-lang.org/std/clone/trait.Clone.html),
[`Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html) and
[`Default`](https://doc.rust-lang.org/std/default/trait.Default.html).
[`Clone`](https://doc.rust-lang.org/std/clone/trait.Clone.html), and
[`Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html).
- [`Option`](https://doc.rust-lang.org/std/option/enum.Option.html) is not currently compatible with `#[config(nest)]`
on types that implement [`Confgr`](https://docs.rs/confgr/latest/confgr/core/trait.Confgr.html).

Expand Down
2 changes: 1 addition & 1 deletion crates/confgr_derive/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn generate_conversion_impl(
}
} else {
quote! {
#field_name: #LAYER_PARAMETER.#field_name.unwrap_or_default(),
#field_name: #LAYER_PARAMETER.#field_name.unwrap_or_else(|| #name::default().#field_name),
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion crates/confgr_derive/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn generate_layer(
#[automatically_derived]
impl Default for #layer_name {
fn default() -> Self {
#name::default().into()
#name::default().into()
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/foreign.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use confgr::prelude::*;
use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
struct NonDefaultType {
value: String,
}

fn default_non_default_type() -> NonDefaultType {
NonDefaultType {
value: "default_value".to_string(),
}
}

#[derive(Config)]
struct TestConfig {
#[config(skip)]
foreign_type: NonDefaultType,
}

impl Default for TestConfig {
fn default() -> Self {
Self {
foreign_type: default_non_default_type(),
}
}
}

#[test]
fn test_non_default_foreign_type_is_valid() {
let config = TestConfig::load_config();
assert_eq!(config.foreign_type.value, "default_value");
}

0 comments on commit 4676dee

Please sign in to comment.