snarkvm_synthesizer_program/logic/command/
remove.rs1use crate::{FinalizeOperation, FinalizeStoreTrait, Opcode, Operand, RegistersTrait, StackTrait};
17use console::{network::prelude::*, program::Identifier};
18
19#[derive(Clone, PartialEq, Eq, Hash)]
22pub struct Remove<N: Network> {
23 mapping: Identifier<N>,
25 operands: [Operand<N>; 1],
27}
28
29impl<N: Network> Remove<N> {
30 #[inline]
32 pub const fn opcode() -> Opcode {
33 Opcode::Command("remove")
34 }
35
36 #[inline]
38 pub fn operands(&self) -> &[Operand<N>] {
39 &self.operands
40 }
41
42 #[inline]
44 pub const fn mapping_name(&self) -> &Identifier<N> {
45 &self.mapping
46 }
47
48 #[inline]
50 pub const fn key(&self) -> &Operand<N> {
51 &self.operands[0]
52 }
53}
54
55impl<N: Network> Remove<N> {
56 pub fn finalize(
58 &self,
59 stack: &impl StackTrait<N>,
60 store: &impl FinalizeStoreTrait<N>,
61 registers: &mut impl RegistersTrait<N>,
62 ) -> Result<Option<FinalizeOperation<N>>> {
63 if !store.contains_mapping_speculative(stack.program_id(), &self.mapping)? {
65 bail!("Mapping '{}/{}' does not exist", stack.program_id(), self.mapping);
66 }
67
68 let key = registers.load_plaintext(stack, self.key())?;
70 store.remove_key_value(*stack.program_id(), self.mapping, &key)
72 }
73}
74
75impl<N: Network> Parser for Remove<N> {
76 fn parse(string: &str) -> ParserResult<Self> {
78 let (string, _) = Sanitizer::parse(string)?;
80 let (string, _) = tag(*Self::opcode())(string)?;
82 let (string, _) = Sanitizer::parse_whitespaces(string)?;
84
85 let (string, mapping) = Identifier::parse(string)?;
87 let (string, _) = tag("[")(string)?;
89 let (string, _) = Sanitizer::parse_whitespaces(string)?;
91 let (string, key) = Operand::parse(string)?;
93 let (string, _) = Sanitizer::parse_whitespaces(string)?;
95 let (string, _) = tag("]")(string)?;
97 let (string, _) = Sanitizer::parse_whitespaces(string)?;
99 let (string, _) = tag(";")(string)?;
101
102 Ok((string, Self { mapping, operands: [key] }))
103 }
104}
105
106impl<N: Network> FromStr for Remove<N> {
107 type Err = Error;
108
109 fn from_str(string: &str) -> Result<Self> {
111 match Self::parse(string) {
112 Ok((remainder, object)) => {
113 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
115 Ok(object)
117 }
118 Err(error) => bail!("Failed to parse string. {error}"),
119 }
120 }
121}
122
123impl<N: Network> Debug for Remove<N> {
124 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
126 Display::fmt(self, f)
127 }
128}
129
130impl<N: Network> Display for Remove<N> {
131 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
133 write!(f, "{} {}[{}];", Self::opcode(), self.mapping, self.key())
135 }
136}
137
138impl<N: Network> FromBytes for Remove<N> {
139 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
141 let mapping = Identifier::read_le(&mut reader)?;
143 let key = Operand::read_le(&mut reader)?;
145 Ok(Self { mapping, operands: [key] })
147 }
148}
149
150impl<N: Network> ToBytes for Remove<N> {
151 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
153 self.mapping.write_le(&mut writer)?;
155 self.key().write_le(&mut writer)
157 }
158}
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163 use console::{network::MainnetV0, program::Register};
164
165 type CurrentNetwork = MainnetV0;
166
167 #[test]
168 fn test_parse() {
169 let (string, remove) = Remove::<CurrentNetwork>::parse("remove account[r1];").unwrap();
170 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
171 assert_eq!(remove.mapping, Identifier::from_str("account").unwrap());
172 assert_eq!(remove.operands().len(), 1, "The number of operands is incorrect");
173 assert_eq!(remove.key(), &Operand::Register(Register::Locator(1)), "The first operand is incorrect");
174 }
175}