I'm trying to create a generic impl for generating a From/Into based on different field types.
I'm seeing the following issues:
error[E0425]: cannot find value `item` in this scope
--> src/lib.rs:23:21
|
23 | $param: item.$param,
| ^^^^ not found in this scope
...
65 | create_impl! { TargetStruct, InputStruct, { field1: Option<String>, field2: Option<Uuid> }}
| ------------------------------------------------------------------------------------------- in this macro invocation
Can anyone point me in the right direction on how to get this to work / if it's possible. At this point, I'm uncertain of how to pass the input parameter to the rules.
Thanks!
#[macro_use]
macro_rules! create_impl {
( @ $target:ident, $input:ident, { } -> ($($result:tt)*) ) => (
impl From<$input> for $target {
fn from(item: $input) -> Self {
Self {
$($result)*
..Default::default()
}
}
});
( @ $target:ident, $input:ident, { $param:ident : Option<Uuid>, $($rest:tt)* } -> ($($result:tt)*) ) => (
create_impl!(@ $target, $input, { $($rest)* } -> (
$($result)*
$param: item.$param.map(|v| v.to_string()),
));
);
( @ $target:ident, $input:ident, { $param:ident : $type:ty, $($rest:tt)* } -> ($($result:tt)*) ) => (
create_impl!(@ $target, $input, { $($rest)* } -> (
$($result)*
$param: item.$param,
));
);
( @ $target:ident, $input:ident, { $param:ident : $type:ty, $($rest:tt)* } -> ($($result:tt)*) ) => (
create_impl!(@ $target, $input, { $($rest)* } -> (
$($result)*
$param: item.$param,
));
);
( $target:ident, $input:ident, { $( $param:ident : $type:ty ),* $(,)* } ) => (
create_impl!(@ $target, $input, { $($param : $type,)* } -> ());
);
}