Skip to content

Commit 44adef7

Browse files
committed
refactor: get the generate_object macro working
1 parent 6d75eb7 commit 44adef7

File tree

3 files changed

+60
-37
lines changed

3 files changed

+60
-37
lines changed

crates/backup/src/sources/op/account.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
use crate::config::runtime::Runtime;
1818
use crate::sources::downloader::Downloader;
19-
use crate::sources::getter::{CliGetter, CommandFiller};
19+
use crate::sources::getter::CommandFiller;
2020
use crate::sources::op::cli;
2121
use crate::sources::op::core::OnePasswordCore;
2222
use amt_lib::pathed::Pathed;
23-
use anyhow::{anyhow, Context, Result};
23+
use anyhow::Result;
2424
use macros::CommonFields;
2525
use serde::{Deserialize, Serialize};
2626
use std::fmt::{Debug, Display, Formatter};
Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
@@ -10,8 +10,8 @@
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

1717
pub 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]
4947
macro_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
}

crates/backup/src/sources/op/v2.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
@@ -10,17 +10,18 @@
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

1717
use crate::generate_object;
1818

1919
generate_object!(Account {
20-
account_name > a => String > String {
21-
22-
},
23-
[ name: String, uuid: String ] for tags: Vec<String> => {
24-
25-
},
20+
account_name: String > a: String,
21+
[ name: String, uuid: String ] > tags: Vec<String> => {
22+
let mut tags = Vec::new();
23+
tags.push(name);
24+
tags.push(uuid);
25+
tags
26+
}
2627
});

0 commit comments

Comments
 (0)