Skip to content

Commit

Permalink
refactor: get the generate_object macro working
Browse files Browse the repository at this point in the history
  • Loading branch information
DaRacci committed Dec 10, 2024
1 parent 6d75eb7 commit 44adef7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 37 deletions.
4 changes: 2 additions & 2 deletions crates/backup/src/sources/op/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

use crate::config::runtime::Runtime;
use crate::sources::downloader::Downloader;
use crate::sources::getter::{CliGetter, CommandFiller};
use crate::sources::getter::CommandFiller;
use crate::sources::op::cli;
use crate::sources::op::core::OnePasswordCore;
use amt_lib::pathed::Pathed;
use anyhow::{anyhow, Context, Result};
use anyhow::Result;
use macros::CommonFields;
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display, Formatter};
Expand Down
74 changes: 48 additions & 26 deletions crates/backup/src/sources/op/mod.rs
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
Expand All @@ -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;
Expand All @@ -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};
}
19 changes: 10 additions & 9 deletions crates/backup/src/sources/op/v2.rs
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
Expand All @@ -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
}
});

0 comments on commit 44adef7

Please sign in to comment.