Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion complex-example/catalyst/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use serde::{Deserialize, Serialize};
use struct_patch::Catalyst;
use substrate::Base;
use substrate::{Base, PhoneNumber};

#[derive(Default, Catalyst)]
#[catalyst(bind = Base)]
// The Substrate has `#[serde(...)]` on fields , and catalyst keep_field_attribute
// so the complex should have the corresponding Serialize derive
#[catalyst(keep_field_attribute)]
#[complex(attribute(derive(Debug, Deserialize, Serialize)))]
#[complex(override_field_attribute("filed_numbers", serde(default)))]
#[allow(dead_code)]
struct Amyloid {
pub extra_bool: bool,
Expand Down Expand Up @@ -39,6 +40,7 @@ impl AmyloidComplex {
#[catalyst(keep_field_attribute)]
#[catalyst(exclude_field_attributes = ["serde"])]
#[complex(attribute(derive(Debug, Deserialize)))]
#[complex(override_field_attribute("filed_numbers", serde(default)))]
#[allow(dead_code)]
struct ExcludedAmyloid {
pub extra_bool: bool,
Expand All @@ -51,6 +53,7 @@ struct ExcludedAmyloid {
#[complex(attribute(derive(Default, Deserialize)))]
#[complex(override_field_attribute("field_string", serde(default = "default_str")))]
#[complex(override_field_attribute("field_string", serde(rename = "renamed_field")))]
#[complex(override_field_attribute("filed_numbers", serde(default)))]
struct SmallAmyloid {
pub extra_bool: bool,
}
Expand Down Expand Up @@ -95,6 +98,10 @@ private_number = 0
extra_bool = false
extra_string = ""
extra_private_number = 0

[filed_numbers]
country_code = 0
local_numbers = ""
"#
);
let toml_str = r#" field_bool = true
Expand Down
2 changes: 1 addition & 1 deletion complex-example/substrate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ struct-patch.workspace = true
serde = { version = "1", features = ["derive"] }

[dev-dependencies]
syn = { version = "3", features = ["full"] }
syn = { version = "2", features = ["full"] }
syn-serde = { version = "0.3.2", features = ["json"] }
15 changes: 12 additions & 3 deletions complex-example/substrate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(unused)]
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use struct_patch::Substrate;

// TODO verify no rename on a struct using Substrate
Expand All @@ -10,6 +10,13 @@ pub struct Base {
pub field_string: String,
pub field_option: Option<usize>,
private_number: u8,
pub filed_numbers: PhoneNumber,
}

#[derive(Debug, Deserialize, Serialize, Default)]
pub struct PhoneNumber {
country_code: u8,
local_numbers: String,
}

impl Base {
Expand All @@ -30,7 +37,7 @@ mod tests {
fn expose_works() {
assert_eq!(
Base::expose_content(),
r#"{"named":[{"attrs":[{"style":"outer","meta":{"list":{"path":{"segments":[{"ident":"serde"}]},"delimiter":"paren","tokens":[{"ident":"default"}]}}}],"vis":"pub","ident":"field_bool","colon_token":true,"ty":{"path":{"segments":[{"ident":"bool"}]}}},{"vis":"pub","ident":"field_string","colon_token":true,"ty":{"path":{"segments":[{"ident":"String"}]}}},{"vis":"pub","ident":"field_option","colon_token":true,"ty":{"path":{"segments":[{"ident":"Option","arguments":{"angle_bracketed":{"args":[{"type":{"path":{"segments":[{"ident":"usize"}]}}}]}}}]}}},{"ident":"private_number","colon_token":true,"ty":{"path":{"segments":[{"ident":"u8"}]}}}]}"#
r#"{"named":[{"attrs":[{"style":"outer","meta":{"list":{"path":{"segments":[{"ident":"serde"}]},"delimiter":"paren","tokens":[{"ident":"default"}]}}}],"vis":"pub","ident":"field_bool","colon_token":true,"ty":{"path":{"segments":[{"ident":"bool"}]}}},{"vis":"pub","ident":"field_string","colon_token":true,"ty":{"path":{"segments":[{"ident":"String"}]}}},{"vis":"pub","ident":"field_option","colon_token":true,"ty":{"path":{"segments":[{"ident":"Option","arguments":{"angle_bracketed":{"args":[{"type":{"path":{"segments":[{"ident":"usize"}]}}}]}}}]}}},{"ident":"private_number","colon_token":true,"ty":{"path":{"segments":[{"ident":"u8"}]}}},{"vis":"pub","ident":"filed_numbers","colon_token":true,"ty":{"path":{"segments":[{"ident":"PhoneNumber"}]}}}]}"#
);

let _fields: syn::Fields = syn_serde::json::from_str(&Base::expose_content()).unwrap();
Expand All @@ -43,6 +50,7 @@ mod tests {
"test".to_string(),
Some(100),
7u8,
PhoneNumber::default(),
);
assert_eq!(b.field_bool, true);
assert_eq!(b.field_string, "test");
Expand All @@ -57,8 +65,9 @@ mod tests {
"test".to_string(),
Some(100),
7u8,
PhoneNumber::default(),
);
let (field_bool, field_string, field_option, private_number) = b.__substrate_unpack();
let (field_bool, field_string, field_option, private_number, _filed_numbers) = b.__substrate_unpack();

assert_eq!(field_bool, true);
assert_eq!(field_string, "test");
Expand Down
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
cargo publish -p struct-patch
'';
checkCatalystScript = pkgs.writeShellScriptBin "check-catalyst" ''
set -ex
cd $(git rev-parse --show-toplevel 2>/dev/null)
cd complex-example
cargo test -p substrate
Expand Down