From 9fb0e6e88883adb81cdb14c6cfd99391d42b034f Mon Sep 17 00:00:00 2001 From: Huan-Cheng Chang Date: Thu, 28 Nov 2024 10:26:28 +0000 Subject: [PATCH] feat(octez): implement deserialize for protocol --- crates/octez/src/async/protocol.rs | 68 ++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 3 deletions(-) diff --git a/crates/octez/src/async/protocol.rs b/crates/octez/src/async/protocol.rs index d8420d7cf..1d5dedfc1 100644 --- a/crates/octez/src/async/protocol.rs +++ b/crates/octez/src/async/protocol.rs @@ -5,10 +5,11 @@ use super::bootstrap::{BootstrapAccounts, BootstrapContracts, BootstrapSmartRoll use rust_embed::Embed; use serde_json::Value; -use serde_with::SerializeDisplay; +use serde_with::{DeserializeFromStr, SerializeDisplay}; use std::fmt::Display; use std::io::{Read, Seek, Write}; use std::path::{Path, PathBuf}; +use std::str::FromStr; use std::sync::Arc; pub trait ReadWritable: Read + Write { @@ -21,11 +22,22 @@ impl ReadWritable for tempfile::NamedTempFile { } } -#[derive(PartialEq, Eq, Debug)] +#[derive(PartialEq, Eq, Debug, DeserializeFromStr)] pub enum ProtocolConstants { Sandbox, } +impl FromStr for ProtocolConstants { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + match s { + "sandbox" => Ok(ProtocolConstants::Sandbox), + _ => Err(anyhow::anyhow!("unknown protocol constants '{}'", s)), + } + } +} + impl Default for ProtocolConstants { fn default() -> Self { Self::Sandbox @@ -40,13 +52,28 @@ impl Display for ProtocolConstants { } } -#[derive(PartialEq, Eq, Debug, Clone, SerializeDisplay)] +#[derive(PartialEq, Eq, Debug, Clone, SerializeDisplay, DeserializeFromStr)] pub enum Protocol { Alpha, ParisC, Quebec, } +impl FromStr for Protocol { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + match s { + "alpha" => Ok(Protocol::Alpha), + "ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK" => Ok(Protocol::Alpha), + "parisC" => Ok(Protocol::ParisC), + "PsParisCZo7KAh1Z1smVd9ZMZ1HHn5gkzbM94V3PLCpknFWhUAi" => Ok(Protocol::ParisC), + "PsQubecQubecQubecQubecQubecQubecQubecQubecQubec" => Ok(Protocol::Quebec), + _ => Err(anyhow::anyhow!("unknown protocol '{}'", s)), + } + } +} + impl Default for Protocol { fn default() -> Self { Self::Alpha @@ -717,4 +744,39 @@ mod tests { "\"ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK\"" ) } + + #[test] + fn deserialize_protocol_constants() { + assert_eq!( + serde_json::from_str::("\"sandbox\"").unwrap(), + ProtocolConstants::Sandbox + ); + assert!(serde_json::from_str::("\"foobar\"") + .unwrap_err() + .to_string() + .contains("unknown protocol constants 'foobar'")); + } + + #[test] + fn deserialize_protocol() { + assert_eq!( + serde_json::from_str::( + "\"ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK\"" + ) + .unwrap(), + Protocol::Alpha + ); + assert_eq!( + serde_json::from_str::("\"alpha\"").unwrap(), + Protocol::Alpha + ); + assert_eq!( + serde_json::from_str::("\"parisC\"").unwrap(), + Protocol::ParisC + ); + assert!(serde_json::from_str::("\"foobar\"") + .unwrap_err() + .to_string() + .contains("unknown protocol 'foobar'")); + } }