Skip to content

Commit

Permalink
feat: rename to unresolvedX to make it names clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored and gitbutler-client committed Dec 2, 2024
1 parent e554fe2 commit 7d710e8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 34 deletions.
8 changes: 4 additions & 4 deletions cmd/soroban-cli/src/commands/contract/arg_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::xdr::{
use crate::commands::txn_result::TxnResult;
use crate::config::{
self,
sc_address::{self, ScAddress},
sc_address::{self, UnresolvedScAddress},
};
use soroban_spec_tools::Spec;

Expand Down Expand Up @@ -254,10 +254,10 @@ pub fn output_to_string(
}

fn resolve_address(addr_or_alias: &str, config: &config::Args) -> Result<String, Error> {
let sc_address: ScAddress = addr_or_alias.parse().unwrap();
let sc_address: UnresolvedScAddress = addr_or_alias.parse().unwrap();
let account = match sc_address {
ScAddress::Address(addr) => addr.to_string(),
addr @ ScAddress::Alias(_) => {
UnresolvedScAddress::Resolved(addr) => addr.to_string(),
addr @ UnresolvedScAddress::Alias(_) => {
let addr = addr.resolve(&config.locator, &config.get_network()?.network_passphrase)?;
match addr {
xdr::ScAddress::Account(account) => account.to_string(),
Expand Down
16 changes: 8 additions & 8 deletions cmd/soroban-cli/src/config/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ pub enum Error {
CannotSign(xdr::MuxedAccount),
}

impl FromStr for Address {
impl FromStr for UnresolvedMuxedAccount {
type Err = Error;

fn from_str(value: &str) -> Result<Self, Self::Err> {
Ok(xdr::MuxedAccount::from_str(value).map_or_else(
|_| Address::AliasOrSecret(value.to_string()),
Address::MuxedAccount,
|_| UnresolvedMuxedAccount::AliasOrSecret(value.to_string()),
UnresolvedMuxedAccount::Resolved,
))
}
}

impl Address {
impl UnresolvedMuxedAccount {
pub fn resolve_muxed_account(
&self,
locator: &locator::Args,
hd_path: Option<usize>,
) -> Result<xdr::MuxedAccount, Error> {
match self {
Address::MuxedAccount(muxed_account) => Ok(muxed_account.clone()),
Address::AliasOrSecret(alias) => {
UnresolvedMuxedAccount::Resolved(muxed_account) => Ok(muxed_account.clone()),
UnresolvedMuxedAccount::AliasOrSecret(alias) => {
Self::resolve_muxed_account_with_alias(alias, locator, hd_path)
}
}
Expand All @@ -66,8 +66,8 @@ impl Address {

pub fn resolve_secret(&self, locator: &locator::Args) -> Result<secret::Secret, Error> {
match &self {
Address::MuxedAccount(muxed_account) => Err(Error::CannotSign(muxed_account.clone())),
Address::AliasOrSecret(alias) => Ok(locator.read_identity(alias)?),
UnresolvedMuxedAccount::Resolved(muxed_account) => Err(Error::CannotSign(muxed_account.clone())),
UnresolvedMuxedAccount::AliasOrSecret(alias) => Ok(locator.read_identity(alias)?),
}
}
}
20 changes: 10 additions & 10 deletions cmd/soroban-cli/src/config/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ pub struct Data {

/// Address can be either a contract address, C.. or eventually an alias of a contract address.
#[derive(Clone, Debug)]
pub enum ContractAddress {
ContractId(stellar_strkey::Contract),
pub enum UnresolvedContract {
Resolved(stellar_strkey::Contract),
Alias(String),
}

impl Default for ContractAddress {
impl Default for UnresolvedContract {
fn default() -> Self {
ContractAddress::Alias(String::default())
UnresolvedContract::Alias(String::default())
}
}

impl FromStr for ContractAddress {
impl FromStr for UnresolvedContract {
type Err = Infallible;

fn from_str(value: &str) -> Result<Self, Self::Err> {
Ok(stellar_strkey::Contract::from_str(value).map_or_else(
|_| ContractAddress::Alias(value.to_string()),
ContractAddress::ContractId,
|_| UnresolvedContract::Alias(value.to_string()),
UnresolvedContract::Resolved,
))
}
}

impl ContractAddress {
impl UnresolvedContract {
pub fn resolve_contract_id(
&self,
locator: &locator::Args,
network_passphrase: &str,
) -> Result<stellar_strkey::Contract, locator::Error> {
match self {
ContractAddress::ContractId(contract) => Ok(*contract),
ContractAddress::Alias(alias) => {
UnresolvedContract::Resolved(contract) => Ok(*contract),
UnresolvedContract::Alias(alias) => {
Self::resolve_alias(alias, locator, network_passphrase)
}
}
Expand Down
25 changes: 13 additions & 12 deletions cmd/soroban-cli/src/config/sc_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ use std::str::FromStr;

use crate::xdr;

use super::{address, locator, ContractAddress};
use super::{address, locator, UnresolvedContract};

/// `ScAddress` can be either a resolved `xdr::ScAddress` or an alias of a `Contract` or `MuxedAccount`.
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug)]
pub enum ScAddress {
Address(xdr::ScAddress),
pub enum UnresolvedScAddress {
Resolved(xdr::ScAddress),
Alias(String),
}

impl Default for ScAddress {
impl Default for UnresolvedScAddress {
fn default() -> Self {
ScAddress::Alias(String::default())
UnresolvedScAddress::Alias(String::default())
}
}

Expand All @@ -27,27 +28,27 @@ pub enum Error {
AccountAliasNotFound(String),
}

impl FromStr for ScAddress {
impl FromStr for UnresolvedScAddress {
type Err = Error;

fn from_str(value: &str) -> Result<Self, Self::Err> {
Ok(xdr::ScAddress::from_str(value)
.map_or_else(|_| ScAddress::Alias(value.to_string()), ScAddress::Address))
.map_or_else(|_| UnresolvedScAddress::Alias(value.to_string()), UnresolvedScAddress::Resolved))
}
}

impl ScAddress {
impl UnresolvedScAddress {
pub fn resolve(
self,
locator: &locator::Args,
network_passphrase: &str,
) -> Result<xdr::ScAddress, Error> {
let alias = match self {
ScAddress::Address(addr) => return Ok(addr),
ScAddress::Alias(alias) => alias,
UnresolvedScAddress::Resolved(addr) => return Ok(addr),
UnresolvedScAddress::Alias(alias) => alias,
};
let contract = ContractAddress::resolve_alias(&alias, locator, network_passphrase);
let muxed_account = super::Address::resolve_muxed_account_with_alias(&alias, locator, None);
let contract = UnresolvedContract::resolve_alias(&alias, locator, network_passphrase);
let muxed_account = super::UnresolvedMuxedAccount::resolve_muxed_account_with_alias(&alias, locator, None);
match (contract, muxed_account) {
(Ok(contract), _) => Ok(xdr::ScAddress::Contract(xdr::Hash(contract.0))),
(_, Ok(muxed_account)) => Ok(xdr::ScAddress::Account(muxed_account.account_id())),
Expand Down

0 comments on commit 7d710e8

Please sign in to comment.