diff --git a/crates/backup/src/sources/op/account.rs b/crates/backup/src/sources/op/account.rs index 38cdb35..4f7279c 100644 --- a/crates/backup/src/sources/op/account.rs +++ b/crates/backup/src/sources/op/account.rs @@ -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}; diff --git a/crates/backup/src/sources/op/mod.rs b/crates/backup/src/sources/op/mod.rs index 2d06fb4..27184d9 100644 --- a/crates/backup/src/sources/op/mod.rs +++ b/crates/backup/src/sources/op/mod.rs @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024. James Draycott + * Copyright (C) 2024. James Draycott me@racci.dev * * 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 . + * 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,11 +37,9 @@ 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 /// }); /// ``` /// @@ -49,40 +47,64 @@ pub mod v2; 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}; } diff --git a/crates/backup/src/sources/op/v2.rs b/crates/backup/src/sources/op/v2.rs index 1534f2a..23dead3 100644 --- a/crates/backup/src/sources/op/v2.rs +++ b/crates/backup/src/sources/op/v2.rs @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024. James Draycott + * Copyright (C) 2024. James Draycott me@racci.dev * * 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 . + * 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 => { - - }, + account_name: String > a: String, + [ name: String, uuid: String ] > tags: Vec => { + let mut tags = Vec::new(); + tags.push(name); + tags.push(uuid); + tags + } });