diff options
| author | mo khan <mo@mokhan.ca> | 2025-06-20 11:33:25 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-06-20 11:33:25 -0600 |
| commit | 3f3bb7ae388995e4b8fa500e0668b99346861a4d (patch) | |
| tree | f41e91f833285fde8819b2b83d5b218b86aea859 | |
| parent | 8f74196b3692338f308ceeeaabbad2f37dd5806a (diff) | |
feat: add build and build_with functions
| -rw-r--r-- | Cargo.lock | 7 | ||||
| -rw-r--r-- | src/lib.rs | 39 |
2 files changed, 41 insertions, 5 deletions
diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0232955 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "please" +version = "0.1.0" @@ -1,14 +1,43 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +pub fn build<T: Default>() -> T { + T::default() +} + +pub fn build_with<T, F>(initializer: F) -> T +where + T: Default, + F: std::ops::FnOnce(&mut T), +{ + let mut item = build::<T>(); + initializer(&mut item); + item } #[cfg(test)] mod tests { use super::*; + #[derive(Default)] + struct Person { + name: String, + age: i32, + } + #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + fn build_test_it_builds_a_new_instance_with_default_values() { + let item = build::<Person>(); + + assert_eq!(item.name, ""); + assert_eq!(item.age, 0); + } + + #[test] + fn build_with_test_initializes_the_new_instance() { + let item = build_with(|item: &mut Person| { + item.name = String::from("pidge"); + item.age = 42; + }); + + assert_eq!(item.name, "pidge"); + assert_eq!(item.age, 42); } } |
