11/*
2- * Copyright (c ) 2024. James Draycott < [email protected] > 2+ * Copyright (C ) 2024. James Draycott [email protected] 33 *
44 * This program is free software: you can redistribute it and/or modify
55 * it under the terms of the GNU General Public License as published by
1010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1111 * See the GNU General Public License for more details.
1212 *
13- * You should have received a copy of the GNU General Public License along with this program.
14- * If not, see < https://www.gnu.org/licenses/> .
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see https://www.gnu.org/licenses/.
1515 */
1616
1717pub mod account;
@@ -37,52 +37,74 @@ pub mod v2;
3737/// ```rust
3838/// use backup::generate_object;
3939///
40- /// generate_object!(Account, {
41- /// pub username {
42- /// type: String,
43- /// required: true,
44- /// };
40+ /// generate_object!(Account {
41+ /// account_name: String
42+ /// > account_name_other: String
4543/// });
4644/// ```
4745///
4846#[ macro_export]
4947macro_rules! generate_object {
5048 ( $object_name: ident {
5149 $(
52- $field: ident > $one_pux_field: ident => $cli_type: ty > $one_pux_type: ty $transform_block: block
50+ $( #[ cfg( $cli_meta: meta) ] ) *
51+ $cli_field: ident: $cli_type: ty
52+ > $( #[ cfg( $one_pux_meta: meta) ] ) *
53+ $one_pux_field: ident: $one_pux_type: ty
54+ $( => $transform_block: block) ?
5355 ) ,* $( , ) ?
5456
5557 $(
56- [ $( $multi_field: ident: $multi_type: ty) ,+ $( , ) ? ] for $multi_one_pux_field: ident: $multi_one_pux_type: ty => $multi_transform_block: block
58+ [
59+ $(
60+ $( #[ cfg( $multi_cli_meta: meta) ] ) *
61+ $multi_cli_field: ident: $multi_cli_type: ty
62+ ) ,+ $( , ) ?
63+ ] > $( #[ cfg( $multi_one_pux_meta: meta) ] ) *
64+ $multi_one_pux_field: ident: $multi_one_pux_type: ty => $multi_transform_block: block
5765 ) ,* $( , ) ?
58- } ) => { $crate:: generate_object!( impl $object_name
59- [ $( $field, $one_pux_field, $cli_type, $one_pux_type, $transform_block) ,* ]
60- [ $( [ $( $multi_field: $multi_type) ,+ ] for $multi_one_pux_field: $multi_one_pux_type => $multi_transform_block) ,* ]
61- ) ; } ;
62-
63- ( impl $object_name: ident
64- [ $( $cli_field: ident, $one_pux_field: ident, $cli_type: ty, $one_pux_type: ty, $transform_block: block) ,* ]
65- [ $( [ $( $multi_field: ident: $multi_type: ty) ,+ ] for $multi_one_pux_field: ident: $multi_one_pux_type: ty => $multi_transform_block: block) ,* ]
66- ) => { paste:: paste! {
66+ } ) => { paste:: paste! {
6767 /// The first layer struct which is gotten from the OnePassword CLI.
6868 pub struct [ < $object_name Cli >] {
69- pub $( $cli_field: $cli_type) ,*
70- $( $( pub $multi_field: $multi_type) ,+) ,*
69+ $(
70+ $( #[ cfg( $cli_meta) ] ) *
71+ pub $cli_field: $cli_type,
72+ ) *
73+ $( $(
74+ $( #[ cfg( $multi_cli_meta) ] ) *
75+ pub $multi_cli_field: $multi_cli_type,
76+ ) * ) *
7177 }
7278
7379 /// The second layer struct which is compatible with the 1Pux format.
7480 pub struct $object_name {
75- pub $( $one_pux_field: $one_pux_type) ,*
76- $( pub $multi_one_pux_field: $multi_one_pux_type) ,*
81+ $(
82+ $( #[ cfg( $one_pux_meta) ] ) *
83+ pub $one_pux_field: $one_pux_type,
84+ ) *
85+ $(
86+ $( #[ cfg( $multi_one_pux_meta) ] ) *
87+ pub $multi_one_pux_field: $multi_one_pux_type,
88+ ) *
7789 }
7890
7991 impl From <[ < $object_name Cli >] > for $object_name {
8092 fn from( cli: [ < $object_name Cli >] ) -> Self {
81- Self {
82- $( $one_pux_field: $transform_block, ) *
83- $( $multi_one_pux_field: $multi_transform_block, ) *
93+ $(
94+ let $one_pux_field = $crate:: generate_object!( @transform_or_into cli $cli_field $( $transform_block) ?) ;
95+ ) *
96+ $(
97+ let $multi_one_pux_field = ( |$( $multi_cli_field) ,+| $multi_transform_block) ( $( cli. $multi_cli_field) ,+) ;
98+ ) *
99+
100+ $object_name {
101+ $( $one_pux_field, ) *
102+ $( $multi_one_pux_field, ) *
84103 }
85104 }
86105 }
87106 } } ;
107+
108+ ( @transform_or_into $self: ident $field: ident) => { $self. $field. into( ) } ;
109+ ( @transform_or_into $self: ident $field: ident $transform_block: block) => { $transform_block} ;
88110}
0 commit comments