snarkvm_synthesizer_program/logic/command/
set.rs1use crate::{FinalizeOperation, FinalizeStoreTrait, Opcode, Operand, RegistersTrait, StackTrait};
17use console::{
18 network::prelude::*,
19 program::{Identifier, Value},
20};
21
22#[derive(Clone, PartialEq, Eq, Hash)]
25pub struct Set<N: Network> {
26 mapping: Identifier<N>,
28 operands: [Operand<N>; 2],
30}
31
32impl<N: Network> Set<N> {
33 #[inline]
35 pub const fn opcode() -> Opcode {
36 Opcode::Command("set")
37 }
38
39 #[inline]
41 pub fn operands(&self) -> &[Operand<N>] {
42 &self.operands
43 }
44
45 #[inline]
47 pub const fn mapping_name(&self) -> &Identifier<N> {
48 &self.mapping
49 }
50
51 #[inline]
53 pub const fn key(&self) -> &Operand<N> {
54 &self.operands[0]
55 }
56
57 #[inline]
59 pub const fn value(&self) -> &Operand<N> {
60 &self.operands[1]
61 }
62}
63
64impl<N: Network> Set<N> {
65 pub fn finalize(
67 &self,
68 stack: &impl StackTrait<N>,
69 store: &impl FinalizeStoreTrait<N>,
70 registers: &mut impl RegistersTrait<N>,
71 ) -> Result<FinalizeOperation<N>> {
72 if !store.contains_mapping_speculative(stack.program_id(), &self.mapping)? {
74 bail!("Mapping '{}/{}' does not exist", stack.program_id(), self.mapping);
75 }
76
77 let key = registers.load_plaintext(stack, self.key())?;
79 let value = Value::Plaintext(registers.load_plaintext(stack, self.value())?);
81
82 store.update_key_value(*stack.program_id(), self.mapping, key, value)
84 }
85}
86
87impl<N: Network> Parser for Set<N> {
88 fn parse(string: &str) -> ParserResult<Self> {
90 let (string, _) = Sanitizer::parse(string)?;
92 let (string, _) = tag(*Self::opcode())(string)?;
94 let (string, _) = Sanitizer::parse_whitespaces(string)?;
96
97 let (string, value) = Operand::parse(string)?;
99 let (string, _) = Sanitizer::parse_whitespaces(string)?;
101
102 let (string, _) = tag("into")(string)?;
104 let (string, _) = Sanitizer::parse_whitespaces(string)?;
106
107 let (string, mapping) = Identifier::parse(string)?;
109 let (string, _) = tag("[")(string)?;
111 let (string, _) = Sanitizer::parse_whitespaces(string)?;
113 let (string, key) = Operand::parse(string)?;
115 let (string, _) = Sanitizer::parse_whitespaces(string)?;
117 let (string, _) = tag("]")(string)?;
119 let (string, _) = Sanitizer::parse_whitespaces(string)?;
121 let (string, _) = tag(";")(string)?;
123
124 Ok((string, Self { mapping, operands: [key, value] }))
125 }
126}
127
128impl<N: Network> FromStr for Set<N> {
129 type Err = Error;
130
131 #[inline]
133 fn from_str(string: &str) -> Result<Self> {
134 match Self::parse(string) {
135 Ok((remainder, object)) => {
136 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
138 Ok(object)
140 }
141 Err(error) => bail!("Failed to parse string. {error}"),
142 }
143 }
144}
145
146impl<N: Network> Debug for Set<N> {
147 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
149 Display::fmt(self, f)
150 }
151}
152
153impl<N: Network> Display for Set<N> {
154 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
156 write!(f, "{} ", Self::opcode())?;
158 write!(f, "{} into ", self.value())?;
160 write!(f, "{}[{}];", self.mapping, self.key())
162 }
163}
164
165impl<N: Network> FromBytes for Set<N> {
166 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
168 let mapping = Identifier::read_le(&mut reader)?;
170 let key = Operand::read_le(&mut reader)?;
172 let value = Operand::read_le(&mut reader)?;
174 Ok(Self { mapping, operands: [key, value] })
176 }
177}
178
179impl<N: Network> ToBytes for Set<N> {
180 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
182 self.mapping.write_le(&mut writer)?;
184 self.key().write_le(&mut writer)?;
186 self.value().write_le(&mut writer)
188 }
189}
190
191#[cfg(test)]
192mod tests {
193 use super::*;
194 use console::{network::MainnetV0, program::Register};
195
196 type CurrentNetwork = MainnetV0;
197
198 #[test]
199 fn test_parse() {
200 let (string, set) = Set::<CurrentNetwork>::parse("set r0 into account[r1];").unwrap();
201 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
202 assert_eq!(set.mapping, Identifier::from_str("account").unwrap());
203 assert_eq!(set.operands().len(), 2, "The number of operands is incorrect");
204 assert_eq!(set.value(), &Operand::Register(Register::Locator(0)), "The first operand is incorrect");
205 assert_eq!(set.key(), &Operand::Register(Register::Locator(1)), "The second operand is incorrect");
206 }
207}