casper_client/cli/
dictionary_item_str_params.rs

1#[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
9/// Various ways of uniquely identifying a dictionary item.
10pub enum DictionaryItemStrParams<'a> {
11    /// A dictionary item identified via an [`Account`]'s named keys.
12    AccountNamedKey {
13        /// The [`AccountHash`] as a formatted string, identifying the account whose named keys
14        /// contains `dictionary_name`.
15        account_hash: &'a str,
16        /// The named key under which the dictionary seed `URef` is stored.
17        dictionary_name: &'a str,
18        /// The key within the dictionary under which the item is held.
19        dictionary_item_key: &'a str,
20    },
21    /// A dictionary item identified via a [`Contract`]'s named keys.
22    ContractNamedKey {
23        /// The [`HashAddr`] as a formatted string, identifying the contract whose named keys
24        /// contains `dictionary_name`.
25        hash_addr: &'a str,
26        /// The named key under which the dictionary seed `URef` is stored.
27        dictionary_name: &'a str,
28        /// The key within the dictionary under which the item is held.
29        dictionary_item_key: &'a str,
30    },
31    /// A dictionary item identified via its seed [`URef`].
32    URef {
33        /// The dictionary's seed `URef` as a formatted string.
34        seed_uref: &'a str,
35        /// The key within the dictionary under which the item is held.
36        dictionary_item_key: &'a str,
37    },
38    /// A dictionary item identified via its unique address derived from the dictionary's seed
39    /// `URef` and the item's key within the dictionary.  The key must be a `Key::Dictionary`
40    /// variant as a formatted string.
41    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}