From ecd83031603ab1bc1ac121b892679163ceea4e73 Mon Sep 17 00:00:00 2001 From: Huan-Cheng Chang Date: Wed, 20 Nov 2024 09:46:36 +0000 Subject: [PATCH] feat(octez): serialise baker config --- crates/octez/src/async/baker.rs | 50 ++++++++++++++++++++++++++++-- crates/octez/src/async/protocol.rs | 17 +++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/crates/octez/src/async/baker.rs b/crates/octez/src/async/baker.rs index 65c86de40..5cac571c9 100644 --- a/crates/octez/src/async/baker.rs +++ b/crates/octez/src/async/baker.rs @@ -1,10 +1,12 @@ use anyhow::{anyhow, Result}; +use serde::Serialize; +use serde_with::SerializeDisplay; use std::{fmt::Display, path::PathBuf}; use tokio::process::{Child, Command}; use super::{endpoint::Endpoint, protocol::Protocol}; -#[derive(PartialEq, Debug, Clone)] +#[derive(PartialEq, Debug, Clone, SerializeDisplay)] pub enum BakerBinaryPath { Env(Protocol), // The binary exists in $PATH Custom(PathBuf), // The binary is at the given path @@ -25,7 +27,7 @@ impl Display for BakerBinaryPath { } } -#[derive(Clone)] +#[derive(Clone, Serialize)] pub struct OctezBakerConfig { binary_path: BakerBinaryPath, octez_client_base_dir: PathBuf, @@ -94,6 +96,8 @@ impl OctezBaker { #[cfg(test)] mod test { + use std::str::FromStr; + use super::*; use crate::r#async::endpoint::Endpoint; use http::Uri; @@ -126,4 +130,46 @@ mod test { .build(); assert!(config.is_err_and(|e| e.to_string().contains("binary path not set"))); } + + #[test] + fn serialize_baker_path() { + assert_eq!( + serde_json::to_string(&BakerBinaryPath::Env(Protocol::Alpha)).unwrap(), + "\"octez-baker-alpha\"" + ); + + assert_eq!( + serde_json::to_string(&BakerBinaryPath::Env(Protocol::ParisC)).unwrap(), + "\"octez-baker-PsParisC\"" + ); + + assert_eq!( + serde_json::to_string(&BakerBinaryPath::Custom( + PathBuf::from_str("/foo/bar").unwrap() + )) + .unwrap(), + "\"/foo/bar\"" + ); + } + + #[test] + fn serialize_config() { + let base_dir = TempDir::new().unwrap(); + let endpoint = + Endpoint::try_from(Uri::from_static("http://localhost:8732")).unwrap(); + let config = OctezBakerConfigBuilder::new() + .set_binary_path(BakerBinaryPath::Env(Protocol::Alpha)) + .set_octez_client_base_dir(base_dir.path().to_str().unwrap()) + .set_octez_node_endpoint(&endpoint) + .build() + .unwrap(); + assert_eq!( + serde_json::to_value(&config).unwrap(), + serde_json::json!({ + "octez_client_base_dir": base_dir.path().to_string_lossy(), + "octez_node_endpoint": "http://localhost:8732", + "binary_path": "octez-baker-alpha" + }) + ) + } } diff --git a/crates/octez/src/async/protocol.rs b/crates/octez/src/async/protocol.rs index 3449acb6a..d8420d7cf 100644 --- a/crates/octez/src/async/protocol.rs +++ b/crates/octez/src/async/protocol.rs @@ -5,6 +5,7 @@ use super::bootstrap::{BootstrapAccounts, BootstrapContracts, BootstrapSmartRoll use rust_embed::Embed; use serde_json::Value; +use serde_with::SerializeDisplay; use std::fmt::Display; use std::io::{Read, Seek, Write}; use std::path::{Path, PathBuf}; @@ -39,7 +40,7 @@ impl Display for ProtocolConstants { } } -#[derive(PartialEq, Eq, Debug, Clone)] +#[derive(PartialEq, Eq, Debug, Clone, SerializeDisplay)] pub enum Protocol { Alpha, ParisC, @@ -52,6 +53,12 @@ impl Default for Protocol { } } +impl Display for Protocol { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.hash()) + } +} + impl Protocol { pub fn hash(&self) -> &'static str { match self { @@ -702,4 +709,12 @@ mod tests { assert_eq!(rollups.first().unwrap(), &first_rollup); assert_eq!(rollups.last().unwrap(), &second_rollup); } + + #[test] + fn serialize_protocol() { + assert_eq!( + serde_json::to_string(&Protocol::Alpha).unwrap(), + "\"ProtoALphaALphaALphaALphaALphaALphaALphaALphaDdp3zK\"" + ) + } }