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 #[inline]
65 pub fn contains_external_struct(&self) -> bool {
66 false
67 }
68}
69
70impl<N: Network> Set<N> {
71 pub fn finalize(
73 &self,
74 stack: &impl StackTrait<N>,
75 store: &impl FinalizeStoreTrait<N>,
76 registers: &mut impl RegistersTrait<N>,
77 ) -> Result<FinalizeOperation<N>> {
78 if !store.contains_mapping_speculative(stack.program_id(), &self.mapping)? {
80 bail!("Mapping '{}/{}' does not exist", stack.program_id(), self.mapping);
81 }
82
83 let key = registers.load_plaintext(stack, self.key())?;
85 let value = Value::Plaintext(registers.load_plaintext(stack, self.value())?);
87
88 store.update_key_value(*stack.program_id(), self.mapping, key, value)
90 }
91}
92
93impl<N: Network> Parser for Set<N> {
94 fn parse(string: &str) -> ParserResult<Self> {
96 let (string, _) = Sanitizer::parse(string)?;
98 let (string, _) = tag(*Self::opcode())(string)?;
100 let (string, _) = Sanitizer::parse_whitespaces(string)?;
102
103 let (string, value) = Operand::parse(string)?;
105 let (string, _) = Sanitizer::parse_whitespaces(string)?;
107
108 let (string, _) = tag("into")(string)?;
110 let (string, _) = Sanitizer::parse_whitespaces(string)?;
112
113 let (string, mapping) = Identifier::parse(string)?;
115 let (string, _) = tag("[")(string)?;
117 let (string, _) = Sanitizer::parse_whitespaces(string)?;
119 let (string, key) = Operand::parse(string)?;
121 let (string, _) = Sanitizer::parse_whitespaces(string)?;
123 let (string, _) = tag("]")(string)?;
125 let (string, _) = Sanitizer::parse_whitespaces(string)?;
127 let (string, _) = tag(";")(string)?;
129
130 Ok((string, Self { mapping, operands: [key, value] }))
131 }
132}
133
134impl<N: Network> FromStr for Set<N> {
135 type Err = Error;
136
137 #[inline]
139 fn from_str(string: &str) -> Result<Self> {
140 match Self::parse(string) {
141 Ok((remainder, object)) => {
142 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
144 Ok(object)
146 }
147 Err(error) => bail!("Failed to parse string. {error}"),
148 }
149 }
150}
151
152impl<N: Network> Debug for Set<N> {
153 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
155 Display::fmt(self, f)
156 }
157}
158
159impl<N: Network> Display for Set<N> {
160 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
162 write!(f, "{} ", Self::opcode())?;
164 write!(f, "{} into ", self.value())?;
166 write!(f, "{}[{}];", self.mapping, self.key())
168 }
169}
170
171impl<N: Network> FromBytes for Set<N> {
172 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
174 let mapping = Identifier::read_le(&mut reader)?;
176 let key = Operand::read_le(&mut reader)?;
178 let value = Operand::read_le(&mut reader)?;
180 Ok(Self { mapping, operands: [key, value] })
182 }
183}
184
185impl<N: Network> ToBytes for Set<N> {
186 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
188 self.mapping.write_le(&mut writer)?;
190 self.key().write_le(&mut writer)?;
192 self.value().write_le(&mut writer)
194 }
195}
196
197#[cfg(test)]
198mod tests {
199 use super::*;
200 use console::{network::MainnetV0, program::Register};
201
202 type CurrentNetwork = MainnetV0;
203
204 #[test]
205 fn test_parse() {
206 let (string, set) = Set::<CurrentNetwork>::parse("set r0 into account[r1];").unwrap();
207 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
208 assert_eq!(set.mapping, Identifier::from_str("account").unwrap());
209 assert_eq!(set.operands().len(), 2, "The number of operands is incorrect");
210 assert_eq!(set.value(), &Operand::Register(Register::Locator(0)), "The first operand is incorrect");
211 assert_eq!(set.key(), &Operand::Register(Register::Locator(1)), "The second operand is incorrect");
212 }
213}