use crate::{FinalizeOperation, FinalizeStoreTrait, Opcode, Operand, RegistersTrait, StackTrait};
use console::{
network::prelude::*,
program::{Identifier, Value},
};
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Set<N: Network> {
mapping: Identifier<N>,
operands: [Operand<N>; 2],
}
impl<N: Network> Set<N> {
#[inline]
pub const fn opcode() -> Opcode {
Opcode::Command("set")
}
#[inline]
pub fn operands(&self) -> &[Operand<N>] {
&self.operands
}
#[inline]
pub const fn mapping_name(&self) -> &Identifier<N> {
&self.mapping
}
#[inline]
pub const fn key(&self) -> &Operand<N> {
&self.operands[0]
}
#[inline]
pub const fn value(&self) -> &Operand<N> {
&self.operands[1]
}
#[inline]
pub fn contains_external_struct(&self) -> bool {
false
}
}
impl<N: Network> Set<N> {
pub fn finalize(
&self,
stack: &impl StackTrait<N>,
store: &impl FinalizeStoreTrait<N>,
registers: &mut impl RegistersTrait<N>,
) -> Result<FinalizeOperation<N>> {
if !store.contains_mapping_speculative(stack.program_id(), &self.mapping)? {
bail!("Mapping '{}/{}' does not exist", stack.program_id(), self.mapping);
}
let key = registers.load_plaintext(stack, self.key())?;
let value = Value::Plaintext(registers.load_plaintext(stack, self.value())?);
store.update_key_value(*stack.program_id(), self.mapping, key, value)
}
}
impl<N: Network> Parser for Set<N> {
fn parse(string: &str) -> ParserResult<Self> {
let (string, _) = Sanitizer::parse(string)?;
let (string, _) = tag(*Self::opcode())(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, value) = Operand::parse(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, _) = tag("into")(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, mapping) = Identifier::parse(string)?;
let (string, _) = tag("[")(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, key) = Operand::parse(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, _) = tag("]")(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, _) = tag(";")(string)?;
Ok((string, Self { mapping, operands: [key, value] }))
}
}
impl<N: Network> FromStr for Set<N> {
type Err = Error;
#[inline]
fn from_str(string: &str) -> Result<Self> {
match Self::parse(string) {
Ok((remainder, object)) => {
ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
Ok(object)
}
Err(error) => bail!("Failed to parse string. {error}"),
}
}
}
impl<N: Network> Debug for Set<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for Set<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{} ", Self::opcode())?;
write!(f, "{} into ", self.value())?;
write!(f, "{}[{}];", self.mapping, self.key())
}
}
impl<N: Network> FromBytes for Set<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let mapping = Identifier::read_le(&mut reader)?;
let key = Operand::read_le(&mut reader)?;
let value = Operand::read_le(&mut reader)?;
Ok(Self { mapping, operands: [key, value] })
}
}
impl<N: Network> ToBytes for Set<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
self.mapping.write_le(&mut writer)?;
self.key().write_le(&mut writer)?;
self.value().write_le(&mut writer)
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::{network::MainnetV0, program::Register};
type CurrentNetwork = MainnetV0;
#[test]
fn test_parse() {
let (string, set) = Set::<CurrentNetwork>::parse("set r0 into account[r1];").unwrap();
assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
assert_eq!(set.mapping, Identifier::from_str("account").unwrap());
assert_eq!(set.operands().len(), 2, "The number of operands is incorrect");
assert_eq!(set.value(), &Operand::Register(Register::Locator(0)), "The first operand is incorrect");
assert_eq!(set.key(), &Operand::Register(Register::Locator(1)), "The second operand is incorrect");
}
}