-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Refactor contract ID handling in Args module.
- Loading branch information
1 parent
122df31
commit 90287f8
Showing
11 changed files
with
104 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,49 @@ | ||
use std::collections::HashMap; | ||
use std::{collections::HashMap, convert::Infallible, str::FromStr}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::locator; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
pub struct Data { | ||
pub ids: HashMap<String, String>, | ||
} | ||
|
||
/// 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), | ||
Alias(String), | ||
} | ||
|
||
impl Default for ContractAddress { | ||
fn default() -> Self { | ||
ContractAddress::Alias(String::default()) | ||
} | ||
} | ||
|
||
impl FromStr for ContractAddress { | ||
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, | ||
)) | ||
} | ||
} | ||
|
||
impl ContractAddress { | ||
pub fn resolve_contract_id( | ||
&self, | ||
locator: &locator::Args, | ||
network_passphrase: &str, | ||
) -> Result<stellar_strkey::Contract, locator::Error> { | ||
match self { | ||
ContractAddress::ContractId(muxed_account) => Ok(*muxed_account), | ||
ContractAddress::Alias(alias) => locator | ||
.get_contract_id(alias, network_passphrase)? | ||
.ok_or_else(|| locator::Error::ContractNotFound(alias.to_owned())), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters