-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement patch for Box and Option (#31)
- Loading branch information
1 parent
a38c91e
commit 0f1a765
Showing
11 changed files
with
334 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#[cfg(feature = "option")] | ||
use struct_patch::Patch; | ||
|
||
#[cfg(feature = "option")] | ||
fn main() { | ||
#[derive(Default, Debug, PartialEq, Patch)] | ||
#[patch(attribute(derive(Debug, Default)))] | ||
struct User { | ||
name: String, | ||
#[patch(name = "Option<AddressPatch>")] | ||
address: Option<Address>, | ||
} | ||
|
||
#[derive(Default, Debug, PartialEq, Patch)] | ||
#[patch(attribute(derive(Debug, Default)))] | ||
struct Address { | ||
street: Option<String>, | ||
country: String, | ||
} | ||
|
||
impl From<AddressPatch> for Address { | ||
fn from(patch: AddressPatch) -> Self { | ||
let mut address = Address::default(); | ||
address.apply(patch); | ||
address | ||
} | ||
} | ||
|
||
let mut user = User { | ||
name: String::from("Thomas"), | ||
address: None, | ||
}; | ||
let mut patch: UserPatch = User::new_empty_patch(); | ||
|
||
patch.address = Some(Some(AddressPatch { | ||
country: Some("France".to_string()), | ||
..Default::default() | ||
})); | ||
|
||
user.apply(patch); | ||
|
||
assert_eq!( | ||
user, | ||
User { | ||
name: String::from("Thomas"), | ||
address: Some(Address { | ||
street: None, | ||
country: String::from("France"), | ||
}), | ||
} | ||
); | ||
} | ||
|
||
#[cfg(not(feature = "option"))] | ||
fn main() { | ||
println!("Please enable the 'option' feature to run this example"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.