use super::*;
use snarkvm_utilities::DeserializeExt;
impl<N: Network> Serialize for InputID<N> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match serializer.is_human_readable() {
true => match self {
Self::Constant(id) => {
let mut input = serializer.serialize_struct("InputID", 2)?;
input.serialize_field("type", "constant")?;
input.serialize_field("id", &id)?;
input.end()
}
Self::Public(id) => {
let mut input = serializer.serialize_struct("InputID", 2)?;
input.serialize_field("type", "public")?;
input.serialize_field("id", &id)?;
input.end()
}
Self::Private(id) => {
let mut input = serializer.serialize_struct("InputID", 2)?;
input.serialize_field("type", "private")?;
input.serialize_field("id", &id)?;
input.end()
}
Self::Record(commitment, gamma, serial_number, tag) => {
let mut input = serializer.serialize_struct("InputID", 5)?;
input.serialize_field("type", "record")?;
input.serialize_field("commitment", &commitment)?;
input.serialize_field("gamma", &gamma)?;
input.serialize_field("serial_number", &serial_number)?;
input.serialize_field("tag", &tag)?;
input.end()
}
Self::ExternalRecord(id) => {
let mut input = serializer.serialize_struct("InputID", 2)?;
input.serialize_field("type", "external_record")?;
input.serialize_field("id", &id)?;
input.end()
}
},
false => ToBytesSerializer::serialize_with_size_encoding(self, serializer),
}
}
}
impl<'de, N: Network> Deserialize<'de> for InputID<N> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
match deserializer.is_human_readable() {
true => {
let mut input = serde_json::Value::deserialize(deserializer)?;
let input_id = match input.get("type").and_then(|t| t.as_str()) {
Some("constant") => InputID::Constant(DeserializeExt::take_from_value::<D>(&mut input, "id")?),
Some("public") => InputID::Public(DeserializeExt::take_from_value::<D>(&mut input, "id")?),
Some("private") => InputID::Private(DeserializeExt::take_from_value::<D>(&mut input, "id")?),
Some("record") => InputID::Record(
DeserializeExt::take_from_value::<D>(&mut input, "commitment")?,
DeserializeExt::take_from_value::<D>(&mut input, "gamma")?,
DeserializeExt::take_from_value::<D>(&mut input, "serial_number")?,
DeserializeExt::take_from_value::<D>(&mut input, "tag")?,
),
Some("external_record") => {
InputID::ExternalRecord(DeserializeExt::take_from_value::<D>(&mut input, "id")?)
}
_ => return Err(de::Error::custom("Invalid input type")),
};
Ok(input_id)
}
false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "input ID"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use snarkvm_console_network::Testnet3;
type CurrentNetwork = Testnet3;
const TEST_CASES: &[&str] = &[
"{\"type\":\"constant\",\"id\":\"5field\"}",
"{\"type\":\"public\",\"id\":\"0field\"}",
"{\"type\":\"private\",\"id\":\"123field\"}",
"{\"type\":\"record\",\"commitment\":\"123123field\",\"tag\":\"0field\",\"serial_number\":\"123456789field\",\"gamma\":\"0group\"}",
"{\"type\":\"external_record\",\"id\":\"123456789field\"}",
];
fn check_serde_json<
T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
>(
expected: T,
) {
let expected_string = expected.to_string();
let candidate_string = serde_json::to_string(&expected).unwrap();
let candidate = serde_json::from_str::<T>(&candidate_string).unwrap();
assert_eq!(expected, candidate);
assert_eq!(expected_string, candidate_string);
assert_eq!(expected_string, candidate.to_string());
assert_eq!(expected, T::from_str(&expected_string).unwrap_or_else(|_| panic!("FromStr: {expected_string}")));
assert_eq!(expected, serde_json::from_str(&candidate_string).unwrap());
}
fn check_bincode<
T: Serialize + for<'a> Deserialize<'a> + Debug + Display + PartialEq + Eq + FromStr + ToBytes + FromBytes,
>(
expected: T,
) {
let expected_bytes = expected.to_bytes_le().unwrap();
let expected_bytes_with_size_encoding = bincode::serialize(&expected).unwrap();
assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
assert_eq!(expected, T::read_le(&expected_bytes[..]).unwrap());
assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..]).unwrap());
}
#[test]
fn test_serde_json() {
for case in TEST_CASES.iter() {
check_serde_json(InputID::<CurrentNetwork>::from_str(case).unwrap());
}
}
#[test]
fn test_bincode() {
for case in TEST_CASES.iter() {
check_bincode(InputID::<CurrentNetwork>::from_str(case).unwrap());
}
}
}