casper_client/cli/
dictionary_item_str_params.rs1#[cfg(doc)]
2use casper_types::{
3 account::{Account, AccountHash},
4 Contract, HashAddr,
5};
6use casper_types::{Key, URef};
7
8use crate::{cli::CliError, rpcs::DictionaryItemIdentifier};
9
10pub enum DictionaryItemStrParams<'a> {
12 AccountNamedKey {
14 account_hash: &'a str,
17 dictionary_name: &'a str,
19 dictionary_item_key: &'a str,
21 },
22 ContractNamedKey {
24 hash_addr: &'a str,
27 dictionary_name: &'a str,
29 dictionary_item_key: &'a str,
31 },
32 EntityNamedKey {
34 entity_addr: &'a str,
37 dictionary_name: &'a str,
39 dictionary_item_key: &'a str,
41 },
42 URef {
44 seed_uref: &'a str,
46 dictionary_item_key: &'a str,
48 },
49 Dictionary(&'a str),
53}
54
55impl<'a> TryFrom<DictionaryItemStrParams<'a>> for DictionaryItemIdentifier {
56 type Error = CliError;
57
58 fn try_from(
59 params: DictionaryItemStrParams<'a>,
60 ) -> Result<DictionaryItemIdentifier, Self::Error> {
61 match params {
62 DictionaryItemStrParams::AccountNamedKey {
63 account_hash,
64 dictionary_name,
65 dictionary_item_key,
66 } => {
67 let key = Key::from_formatted_str(account_hash).map_err(|error| {
68 CliError::FailedToParseKey {
69 context: "dictionary item account named key",
70 error,
71 }
72 })?;
73 let account_hash = key.into_account().ok_or(CliError::InvalidArgument {
74 context: "dictionary item account named key",
75 error: "not an account hash".to_string(),
76 })?;
77 Ok(DictionaryItemIdentifier::new_from_account_info(
78 account_hash,
79 dictionary_name.to_string(),
80 dictionary_item_key.to_string(),
81 ))
82 }
83 DictionaryItemStrParams::ContractNamedKey {
84 hash_addr,
85 dictionary_name,
86 dictionary_item_key,
87 } => {
88 let key = Key::from_formatted_str(hash_addr).map_err(|error| {
89 CliError::FailedToParseKey {
90 context: "dictionary item contract named key",
91 error,
92 }
93 })?;
94 let hash_addr = key.into_hash_addr().ok_or(CliError::InvalidArgument {
95 context: "dictionary item contract named key",
96 error: "not a hash-addr".to_string(),
97 })?;
98 Ok(DictionaryItemIdentifier::new_from_contract_info(
99 hash_addr,
100 dictionary_name.to_string(),
101 dictionary_item_key.to_string(),
102 ))
103 }
104 DictionaryItemStrParams::EntityNamedKey {
105 entity_addr,
106 dictionary_name,
107 dictionary_item_key,
108 } => {
109 let key = Key::from_formatted_str(entity_addr).map_err(|error| {
110 CliError::FailedToParseKey {
111 context: "dictionary item entity named key",
112 error,
113 }
114 })?;
115
116 let entity_addr = if let Key::AddressableEntity(addr) = key {
117 addr
118 } else {
119 return Err(CliError::InvalidArgument {
120 context: "dictionary item entity named key",
121 error: "not a entity-addr".to_string(),
122 });
123 };
124 Ok(DictionaryItemIdentifier::new_from_entity_info(
125 entity_addr,
126 dictionary_name.to_string(),
127 dictionary_item_key.to_string(),
128 ))
129 }
130 DictionaryItemStrParams::URef {
131 seed_uref,
132 dictionary_item_key,
133 } => {
134 let seed_uref = URef::from_formatted_str(seed_uref).map_err(|error| {
135 CliError::FailedToParseURef {
136 context: "dictionary item uref",
137 error,
138 }
139 })?;
140 Ok(DictionaryItemIdentifier::new_from_seed_uref(
141 seed_uref,
142 dictionary_item_key.to_string(),
143 ))
144 }
145 DictionaryItemStrParams::Dictionary(address) => {
146 let key = Key::from_formatted_str(address).map_err(|error| {
147 CliError::FailedToParseKey {
148 context: "dictionary item address",
149 error,
150 }
151 })?;
152 Ok(DictionaryItemIdentifier::new_from_item_key(key)?)
153 }
154 }
155 }
156}