snarkvm_console_program/data/value/
bytes.rsuse super::*;
impl<N: Network> FromBytes for Value<N> {
fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
let index = u8::read_le(&mut reader)?;
let entry = match index {
0 => Self::Plaintext(Plaintext::read_le(&mut reader)?),
1 => Self::Record(Record::read_le(&mut reader)?),
2 => Self::Future(Future::read_le(&mut reader)?),
3.. => return Err(error(format!("Failed to decode value variant {index}"))),
};
Ok(entry)
}
}
impl<N: Network> ToBytes for Value<N> {
fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
match self {
Self::Plaintext(plaintext) => {
0u8.write_le(&mut writer)?;
plaintext.write_le(&mut writer)
}
Self::Record(record) => {
1u8.write_le(&mut writer)?;
record.write_le(&mut writer)
}
Self::Future(future) => {
2u8.write_le(&mut writer)?;
future.write_le(&mut writer)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::MainnetV0;
type CurrentNetwork = MainnetV0;
#[test]
fn test_value_plaintext_bytes() -> Result<()> {
let expected = Value::Plaintext(Plaintext::<CurrentNetwork>::from_str(
"{ owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah, token_amount: 100u64 }",
)?);
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Value::read_le(&expected_bytes[..])?);
Ok(())
}
#[test]
fn test_value_record_bytes() -> Result<()> {
let expected = Value::Record(Record::<CurrentNetwork, Plaintext<CurrentNetwork>>::from_str(
"{ owner: aleo1d5hg2z3ma00382pngntdp68e74zv54jdxy249qhaujhks9c72yrs33ddah.private, token_amount: 100u64.private, _nonce: 0group.public }",
)?);
let expected_bytes = expected.to_bytes_le()?;
assert_eq!(expected, Value::read_le(&expected_bytes[..])?);
Ok(())
}
}