casper_client/cli/
dictionary_item_str_params.rs1#[cfg(doc)]
2use casper_types::{account::AccountHash, HashAddr};
3use casper_types::{Key, URef};
4
5#[cfg(doc)]
6use crate::types::{Account, Contract};
7use crate::{cli::CliError, rpcs::DictionaryItemIdentifier};
8
9pub enum DictionaryItemStrParams<'a> {
11 AccountNamedKey {
13 account_hash: &'a str,
16 dictionary_name: &'a str,
18 dictionary_item_key: &'a str,
20 },
21 ContractNamedKey {
23 hash_addr: &'a str,
26 dictionary_name: &'a str,
28 dictionary_item_key: &'a str,
30 },
31 URef {
33 seed_uref: &'a str,
35 dictionary_item_key: &'a str,
37 },
38 Dictionary(&'a str),
42}
43
44impl<'a> TryFrom<DictionaryItemStrParams<'a>> for DictionaryItemIdentifier {
45 type Error = CliError;
46
47 fn try_from(
48 params: DictionaryItemStrParams<'a>,
49 ) -> Result<DictionaryItemIdentifier, Self::Error> {
50 match params {
51 DictionaryItemStrParams::AccountNamedKey {
52 account_hash,
53 dictionary_name,
54 dictionary_item_key,
55 } => {
56 let key = Key::from_formatted_str(account_hash).map_err(|error| {
57 CliError::FailedToParseKey {
58 context: "dictionary item account named key",
59 error,
60 }
61 })?;
62 let account_hash = key.into_account().ok_or(CliError::InvalidArgument {
63 context: "dictionary item account named key",
64 error: "not an account hash".to_string(),
65 })?;
66 Ok(DictionaryItemIdentifier::new_from_account_info(
67 account_hash,
68 dictionary_name.to_string(),
69 dictionary_item_key.to_string(),
70 ))
71 }
72 DictionaryItemStrParams::ContractNamedKey {
73 hash_addr,
74 dictionary_name,
75 dictionary_item_key,
76 } => {
77 let key = Key::from_formatted_str(hash_addr).map_err(|error| {
78 CliError::FailedToParseKey {
79 context: "dictionary item contract named key",
80 error,
81 }
82 })?;
83 let hash_addr = key.into_hash().ok_or(CliError::InvalidArgument {
84 context: "dictionary item contract named key",
85 error: "not a hash-addr".to_string(),
86 })?;
87 Ok(DictionaryItemIdentifier::new_from_contract_info(
88 hash_addr,
89 dictionary_name.to_string(),
90 dictionary_item_key.to_string(),
91 ))
92 }
93 DictionaryItemStrParams::URef {
94 seed_uref,
95 dictionary_item_key,
96 } => {
97 let seed_uref = URef::from_formatted_str(seed_uref).map_err(|error| {
98 CliError::FailedToParseURef {
99 context: "dictionary item uref",
100 error,
101 }
102 })?;
103 Ok(DictionaryItemIdentifier::new_from_seed_uref(
104 seed_uref,
105 dictionary_item_key.to_string(),
106 ))
107 }
108 DictionaryItemStrParams::Dictionary(address) => {
109 let key = Key::from_formatted_str(address).map_err(|error| {
110 CliError::FailedToParseKey {
111 context: "dictionary item address",
112 error,
113 }
114 })?;
115 Ok(DictionaryItemIdentifier::new_from_item_key(key)?)
116 }
117 }
118 }
119}