soroban_cli/config/
alias.rs

1use std::{collections::HashMap, convert::Infallible, str::FromStr};
2
3use serde::{Deserialize, Serialize};
4
5use super::locator;
6
7#[derive(Serialize, Deserialize, Default)]
8pub struct Data {
9    pub ids: HashMap<String, String>,
10}
11
12/// Address can be either a contract address, C.. or eventually an alias of a contract address.
13#[derive(Clone, Debug)]
14pub enum UnresolvedContract {
15    Resolved(stellar_strkey::Contract),
16    Alias(String),
17}
18
19impl Default for UnresolvedContract {
20    fn default() -> Self {
21        UnresolvedContract::Alias(String::default())
22    }
23}
24
25impl FromStr for UnresolvedContract {
26    type Err = Infallible;
27
28    fn from_str(value: &str) -> Result<Self, Self::Err> {
29        Ok(stellar_strkey::Contract::from_str(value).map_or_else(
30            |_| UnresolvedContract::Alias(value.to_string()),
31            UnresolvedContract::Resolved,
32        ))
33    }
34}
35
36impl UnresolvedContract {
37    pub fn resolve_contract_id(
38        &self,
39        locator: &locator::Args,
40        network_passphrase: &str,
41    ) -> Result<stellar_strkey::Contract, locator::Error> {
42        match self {
43            UnresolvedContract::Resolved(contract) => Ok(*contract),
44            UnresolvedContract::Alias(alias) => {
45                Self::resolve_alias(alias, locator, network_passphrase)
46            }
47        }
48    }
49
50    pub fn resolve_alias(
51        alias: &str,
52        locator: &locator::Args,
53        network_passphrase: &str,
54    ) -> Result<stellar_strkey::Contract, locator::Error> {
55        locator
56            .get_contract_id(alias, network_passphrase)?
57            .ok_or_else(|| locator::Error::ContractNotFound(alias.to_owned()))
58    }
59}