use clap::Parser;
use crate::{commands::tx, config::address, tx::builder, xdr};
#[derive(Parser, Debug, Clone)]
#[group(skip)]
pub struct Cmd {
#[command(flatten)]
pub tx: tx::Args,
#[clap(flatten)]
pub op: Args,
}
#[derive(Debug, clap::Args, Clone)]
#[allow(clippy::struct_excessive_bools, clippy::doc_markdown)]
pub struct Args {
#[arg(long)]
pub trustor: address::UnresolvedMuxedAccount,
#[arg(long)]
pub asset: builder::Asset,
#[arg(long, conflicts_with = "clear_authorize")]
pub set_authorize: bool,
#[arg(long, conflicts_with = "clear_authorize_to_maintain_liabilities")]
pub set_authorize_to_maintain_liabilities: bool,
#[arg(long, conflicts_with = "clear_trustline_clawback_enabled")]
pub set_trustline_clawback_enabled: bool,
#[arg(long)]
pub clear_authorize: bool,
#[arg(long)]
pub clear_authorize_to_maintain_liabilities: bool,
#[arg(long)]
pub clear_trustline_clawback_enabled: bool,
}
impl TryFrom<&Cmd> for xdr::OperationBody {
type Error = tx::args::Error;
fn try_from(cmd: &Cmd) -> Result<Self, Self::Error> {
let mut set_flags = 0;
let mut set_flag = |flag: xdr::TrustLineFlags| set_flags |= flag as u32;
if cmd.op.set_authorize {
set_flag(xdr::TrustLineFlags::AuthorizedFlag);
}
if cmd.op.set_authorize_to_maintain_liabilities {
set_flag(xdr::TrustLineFlags::AuthorizedToMaintainLiabilitiesFlag);
}
if cmd.op.set_trustline_clawback_enabled {
set_flag(xdr::TrustLineFlags::TrustlineClawbackEnabledFlag);
}
let mut clear_flags = 0;
let mut clear_flag = |flag: xdr::TrustLineFlags| clear_flags |= flag as u32;
if cmd.op.clear_authorize {
clear_flag(xdr::TrustLineFlags::AuthorizedFlag);
}
if cmd.op.clear_authorize_to_maintain_liabilities {
clear_flag(xdr::TrustLineFlags::AuthorizedToMaintainLiabilitiesFlag);
}
if cmd.op.clear_trustline_clawback_enabled {
clear_flag(xdr::TrustLineFlags::TrustlineClawbackEnabledFlag);
}
Ok(xdr::OperationBody::SetTrustLineFlags(
xdr::SetTrustLineFlagsOp {
trustor: cmd.tx.resolve_account_id(&cmd.op.trustor)?,
asset: cmd.tx.resolve_asset(&cmd.op.asset)?,
clear_flags,
set_flags,
},
))
}
}