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 #[inline]
56 pub fn contains_external_struct(&self) -> bool {
57 false
58 }
59}
60
61impl<N: Network> Remove<N> {
62 pub fn finalize(
64 &self,
65 stack: &impl StackTrait<N>,
66 store: &impl FinalizeStoreTrait<N>,
67 registers: &mut impl RegistersTrait<N>,
68 ) -> Result<Option<FinalizeOperation<N>>> {
69 if !store.contains_mapping_speculative(stack.program_id(), &self.mapping)? {
71 bail!("Mapping '{}/{}' does not exist", stack.program_id(), self.mapping);
72 }
73
74 let key = registers.load_plaintext(stack, self.key())?;
76 store.remove_key_value(*stack.program_id(), self.mapping, &key)
78 }
79}
80
81impl<N: Network> Parser for Remove<N> {
82 fn parse(string: &str) -> ParserResult<Self> {
84 let (string, _) = Sanitizer::parse(string)?;
86 let (string, _) = tag(*Self::opcode())(string)?;
88 let (string, _) = Sanitizer::parse_whitespaces(string)?;
90
91 let (string, mapping) = Identifier::parse(string)?;
93 let (string, _) = tag("[")(string)?;
95 let (string, _) = Sanitizer::parse_whitespaces(string)?;
97 let (string, key) = Operand::parse(string)?;
99 let (string, _) = Sanitizer::parse_whitespaces(string)?;
101 let (string, _) = tag("]")(string)?;
103 let (string, _) = Sanitizer::parse_whitespaces(string)?;
105 let (string, _) = tag(";")(string)?;
107
108 Ok((string, Self { mapping, operands: [key] }))
109 }
110}
111
112impl<N: Network> FromStr for Remove<N> {
113 type Err = Error;
114
115 fn from_str(string: &str) -> Result<Self> {
117 match Self::parse(string) {
118 Ok((remainder, object)) => {
119 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
121 Ok(object)
123 }
124 Err(error) => bail!("Failed to parse string. {error}"),
125 }
126 }
127}
128
129impl<N: Network> Debug for Remove<N> {
130 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
132 Display::fmt(self, f)
133 }
134}
135
136impl<N: Network> Display for Remove<N> {
137 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
139 write!(f, "{} {}[{}];", Self::opcode(), self.mapping, self.key())
141 }
142}
143
144impl<N: Network> FromBytes for Remove<N> {
145 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
147 let mapping = Identifier::read_le(&mut reader)?;
149 let key = Operand::read_le(&mut reader)?;
151 Ok(Self { mapping, operands: [key] })
153 }
154}
155
156impl<N: Network> ToBytes for Remove<N> {
157 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
159 self.mapping.write_le(&mut writer)?;
161 self.key().write_le(&mut writer)
163 }
164}
165
166#[cfg(test)]
167mod tests {
168 use super::*;
169 use console::{network::MainnetV0, program::Register};
170
171 type CurrentNetwork = MainnetV0;
172
173 #[test]
174 fn test_parse() {
175 let (string, remove) = Remove::<CurrentNetwork>::parse("remove account[r1];").unwrap();
176 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
177 assert_eq!(remove.mapping, Identifier::from_str("account").unwrap());
178 assert_eq!(remove.operands().len(), 1, "The number of operands is incorrect");
179 assert_eq!(remove.key(), &Operand::Register(Register::Locator(1)), "The first operand is incorrect");
180 }
181}