-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: get the generate_object macro working
- Loading branch information
Showing
3 changed files
with
60 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2024. James Draycott <[email protected]> | ||
* Copyright (C) 2024. James Draycott [email protected] | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
|
@@ -10,8 +10,8 @@ | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with this program. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
pub mod account; | ||
|
@@ -37,52 +37,74 @@ pub mod v2; | |
/// ```rust | ||
/// use backup::generate_object; | ||
/// | ||
/// generate_object!(Account, { | ||
/// pub username { | ||
/// type: String, | ||
/// required: true, | ||
/// }; | ||
/// generate_object!(Account { | ||
/// account_name: String | ||
/// > account_name_other: String | ||
/// }); | ||
/// ``` | ||
/// | ||
#[macro_export] | ||
macro_rules! generate_object { | ||
($object_name:ident { | ||
$( | ||
$field:ident > $one_pux_field:ident => $cli_type:ty > $one_pux_type:ty $transform_block:block | ||
$(#[cfg($cli_meta:meta)])* | ||
$cli_field:ident: $cli_type:ty | ||
> $(#[cfg($one_pux_meta:meta)])* | ||
$one_pux_field:ident: $one_pux_type:ty | ||
$(=> $transform_block:block)? | ||
),* $(,)? | ||
|
||
$( | ||
[ $($multi_field:ident: $multi_type:ty),+ $(,)? ] for $multi_one_pux_field:ident: $multi_one_pux_type:ty => $multi_transform_block:block | ||
[ | ||
$( | ||
$(#[cfg($multi_cli_meta:meta)])* | ||
$multi_cli_field:ident: $multi_cli_type:ty | ||
),+ $(,)? | ||
] > $(#[cfg($multi_one_pux_meta:meta)])* | ||
$multi_one_pux_field:ident: $multi_one_pux_type:ty => $multi_transform_block:block | ||
),* $(,)? | ||
}) => { $crate::generate_object!(impl $object_name | ||
[ $($field, $one_pux_field, $cli_type, $one_pux_type, $transform_block),* ] | ||
[ $([ $($multi_field: $multi_type),+ ] for $multi_one_pux_field: $multi_one_pux_type => $multi_transform_block),* ] | ||
); }; | ||
|
||
(impl $object_name:ident | ||
[ $($cli_field:ident, $one_pux_field:ident, $cli_type:ty, $one_pux_type:ty, $transform_block:block),* ] | ||
[ $([ $($multi_field:ident: $multi_type:ty),+ ] for $multi_one_pux_field:ident: $multi_one_pux_type:ty => $multi_transform_block:block),* ] | ||
) => {paste::paste! { | ||
}) => {paste::paste! { | ||
/// The first layer struct which is gotten from the OnePassword CLI. | ||
pub struct [< $object_name Cli >] { | ||
pub $($cli_field: $cli_type),* | ||
$($(pub $multi_field: $multi_type),+),* | ||
$( | ||
$(#[cfg($cli_meta)])* | ||
pub $cli_field: $cli_type, | ||
)* | ||
$($( | ||
$(#[cfg($multi_cli_meta)])* | ||
pub $multi_cli_field: $multi_cli_type, | ||
)*)* | ||
} | ||
|
||
/// The second layer struct which is compatible with the 1Pux format. | ||
pub struct $object_name { | ||
pub $($one_pux_field: $one_pux_type),* | ||
$(pub $multi_one_pux_field: $multi_one_pux_type),* | ||
$( | ||
$(#[cfg($one_pux_meta)])* | ||
pub $one_pux_field: $one_pux_type, | ||
)* | ||
$( | ||
$(#[cfg($multi_one_pux_meta)])* | ||
pub $multi_one_pux_field: $multi_one_pux_type, | ||
)* | ||
} | ||
|
||
impl From<[< $object_name Cli >]> for $object_name { | ||
fn from(cli: [< $object_name Cli >]) -> Self { | ||
Self { | ||
$($one_pux_field: $transform_block,)* | ||
$($multi_one_pux_field: $multi_transform_block,)* | ||
$( | ||
let $one_pux_field = $crate::generate_object!(@transform_or_into cli $cli_field $($transform_block)?); | ||
)* | ||
$( | ||
let $multi_one_pux_field = (|$($multi_cli_field),+| $multi_transform_block)($(cli.$multi_cli_field),+); | ||
)* | ||
|
||
$object_name { | ||
$($one_pux_field,)* | ||
$($multi_one_pux_field,)* | ||
} | ||
} | ||
} | ||
}}; | ||
|
||
(@transform_or_into $self:ident $field:ident) => {$self.$field.into()}; | ||
(@transform_or_into $self:ident $field:ident $transform_block:block) => {$transform_block}; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2024. James Draycott <[email protected]> | ||
* Copyright (C) 2024. James Draycott [email protected] | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
|
@@ -10,17 +10,18 @@ | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with this program. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see https://www.gnu.org/licenses/. | ||
*/ | ||
|
||
use crate::generate_object; | ||
|
||
generate_object!(Account { | ||
account_name > a => String > String { | ||
|
||
}, | ||
[ name: String, uuid: String ] for tags: Vec<String> => { | ||
|
||
}, | ||
account_name: String > a: String, | ||
[ name: String, uuid: String ] > tags: Vec<String> => { | ||
let mut tags = Vec::new(); | ||
tags.push(name); | ||
tags.push(uuid); | ||
tags | ||
} | ||
}); |