This crate provides a set of alternative customizable #[derive] attributes for Rust. To help you covnert between structs simplify.
```rust use struct_convert::Convert;
struct A { value: i64, }
struct B { value: i64, }
struct C { value: i64, }
mod some_mod { pub struct Remote { pub value: i64, } }
fn main() {}
fn testproxy() { let c = C { value: 8 }; let a: A = c.into(); let b: B = a.into(); debugassert_eq!(8, b.value);
let r = some_mod::Remote { value: 7 };
let a2: A = r.into();
debug_assert_eq!(7, a2.value);
} ```
Chech the examples for more!
Combination of Derivative and sturct-convert
```rust use derivative::Derivative; use struct_convert::Convert; use time::OffsetDateTime;
struct SomeStruct { name: String, #[derivative(Default(value = "OffsetDateTime::nowutc()"))] #[convertfield(from="A", ignore)] at: OffsetDateTime, }
struct A { name: String, }
fn main() { let a = A{ name: "A".to_string() }; let ss: SomeStruct = a.into(); println!("{:?}", ss); // SomeStruct { name: "A", at: 2023-03-03 6:13:32.5684174 +00:00:00 } } ```
Inner stuct convert.
```rust use struct_convert::Convert;
struct BInner { name: String, }
struct B { bid: i64, inner: BInner, }
struct A { #[convert_field(rename = "bid")] id: i64,
inner: AInner,
}
struct AInner { name: String, }
fn main() { let a = A { id: 2, inner: AInner { name: String::from("AInner"), }, }; let b: B = a.into(); debugasserteq!( B { bid: 2, inner: BInner { name: String::from("AInner") }, }, b ); } ```
Option field convert.
```rust use struct_convert::Convert;
struct BInner { name: String, }
struct B {
name: String,
name2: String,
optstr: Option
struct A {
#[convert_field(unwrap)]
name: Option
#[convert_field(unwrap)]
name2: Option<String>,
#[convert_field(option)]
opt_str: String,
#[convert_field(option)]
opt_str2: Option<String>,
}
struct AInner { name: String, }
fn main() { let a = A { name: Some("Jack".tostring()), optstr: String::from("str"), optstr2: Some(String::from("Option")), name2: None, }; let b: B = a.into(); debugasserteq!( B { name: "Jack".tostring(), optstr: Some(String::from("str")), optstr2: Some(String::from("Option")), name2: "".to_string() }, b ); } ```
Ignore Some fileds.
```rust use struct_convert::Convert;
struct B { num: String, name: String, }
struct A { #[convert_field(ignore)] id: i64,
#[convert_field(to_string)]
num: i64,
#[convert_field(unwrap)]
name: Option<String>,
}
fn main() { let a = A { id: 2, num: 1, name: Some("Jack".tostring()), }; let b: B = a.into(); debugasserteq!( B { num: "1".tostring(), name: "Jack".to_string(), }, b ); } ```
convert B from A ```rust use struct_convert::Convert;
struct BInner { name: String, }
struct B {
#[convert_field(rename = "id")]
bid: i64,
#[convert_field(to_string)]
num: String,
#[convert_field(unwrap)]
name: String,
inner: BInner,
#[convert_field(wrap)]
opt_str: Option<String>,
opt_str2: Option<String>
}
struct A {
ignoref: i64,
id: i64,
num: i64,
name: Option
struct AInner { name: String, }
fn main() { let a = A { id: 2, num: 1, name: Some("Jack".tostring()), inner: AInner { name: String::from("AInner"), }, optstr: String::from("str"), optstr2: Some(String::from("Option")), ignoref: 1, }; let b: B = a.into(); debugasserteq!( B { num: "1".tostring(), bid: 2, name: "Jack".tostring(), inner: BInner { name: String::from("AInner") }, optstr: Some(String::from("str")), optstr2: Some(String::from("Option")) }, b ); } ```
Convert with custom function.
```rust use struct_convert::Convert;
struct B { bid: i64, }
struct A { #[convertfield(rename = "bid", customfn = "strtoi64")] id_str: String, }
struct C { #[convertfield(rename = "bid", customfn = "to_point")] point: Point, }
struct Point(i64, i64);
fn strtoi64(a: &A) -> i64 { a.id_str.parse().unwrap() }
fn to_point(b: &B) -> Point { Point(b.bid, b.bid) }
fn main() { let a = A { idstr: "4".into() }; let b: B = a.into(); debugasserteq!(B { bid: 4 }, b); let c: C = b.into(); debugassert_eq!(C { point: Point(4, 4) }, c);
}
```
More examples look here.
Welcome PR.