From f15e8c665f021aa2c54f0630d9f21abf05b3c8c9 Mon Sep 17 00:00:00 2001 From: Huan-Cheng Chang Date: Fri, 15 Nov 2024 15:43:12 +0000 Subject: [PATCH] feat(octez): serialise baker config --- crates/octez/src/async/baker.rs | 33 +++++++++++++++++++++++++++++- crates/octez/src/async/protocol.rs | 18 ++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/crates/octez/src/async/baker.rs b/crates/octez/src/async/baker.rs index 65c86de40..3e92f8cf5 100644 --- a/crates/octez/src/async/baker.rs +++ b/crates/octez/src/async/baker.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, Result}; +use serde::Serialize; use std::{fmt::Display, path::PathBuf}; use tokio::process::{Child, Command}; @@ -10,6 +11,15 @@ pub enum BakerBinaryPath { Custom(PathBuf), // The binary is at the given path } +impl Serialize for BakerBinaryPath { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} + impl Display for BakerBinaryPath { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { @@ -25,7 +35,7 @@ impl Display for BakerBinaryPath { } } -#[derive(Clone)] +#[derive(Clone, Serialize)] pub struct OctezBakerConfig { binary_path: BakerBinaryPath, octez_client_base_dir: PathBuf, @@ -126,4 +136,25 @@ mod test { .build(); assert!(config.is_err_and(|e| e.to_string().contains("binary path not set"))); } + + #[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..586ad84b6 100644 --- a/crates/octez/src/async/protocol.rs +++ b/crates/octez/src/async/protocol.rs @@ -4,6 +4,7 @@ pub use super::bootstrap::{ use super::bootstrap::{BootstrapAccounts, BootstrapContracts, BootstrapSmartRollups}; use rust_embed::Embed; +use serde::Serialize; use serde_json::Value; use std::fmt::Display; use std::io::{Read, Seek, Write}; @@ -46,6 +47,15 @@ pub enum Protocol { Quebec, } +impl Serialize for Protocol { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(self.hash()) + } +} + impl Default for Protocol { fn default() -> Self { Self::Alpha @@ -702,4 +712,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\"" + ) + } }