casper_client/cli/
dictionary_item_str_params.rs

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