From 87a0da13f0b771e8e042d5d0b106449642899db1 Mon Sep 17 00:00:00 2001 From: Huan-Cheng Chang Date: Wed, 20 Nov 2024 09:33:52 +0000 Subject: [PATCH] feat(octez): serialise octez client config --- Cargo.toml | 2 +- crates/octez/src/async/client.rs | 47 ++++++++++++++++++++++++++++- crates/octez/src/async/directory.rs | 31 +++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dd639d820..914c7e7cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -83,7 +83,7 @@ reqwest = { version = "0.11.24", features = ["json"] } reqwest-eventsource = "0.5.0" rust-embed = { version = "8.5.0", features = ["interpolate-folder-path"] } rustyline = "14.0.0" -serde = { version = "1.0.196", features = ["derive"] } +serde = { version = "1.0.196", features = ["derive", "rc"] } serde-wasm-bindgen = "0.6.5" serde_json = "1.0.107" serde_with = { version = "3.6.1", features = ["macros"] } diff --git a/crates/octez/src/async/client.rs b/crates/octez/src/async/client.rs index f45dc4e16..a8bb566b9 100644 --- a/crates/octez/src/async/client.rs +++ b/crates/octez/src/async/client.rs @@ -3,6 +3,7 @@ use jstz_crypto::{ public_key::PublicKey, public_key_hash::PublicKeyHash, secret_key::SecretKey, }; use regex::Regex; +use serde::Serialize; use std::{ ffi::OsStr, fmt, @@ -19,14 +20,19 @@ const DEFAULT_BINARY_PATH: &str = "octez-client"; type StdOut = String; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Serialize)] pub struct OctezClientConfig { binary_path: PathBuf, + #[serde(skip_serializing_if = "skip_serializing_base_dir")] base_dir: Arc, octez_node_endpoint: Endpoint, disable_unsafe_disclaimer: bool, } +fn skip_serializing_base_dir(d: &Arc) -> bool { + matches!(d.as_ref(), &Directory::TempDir(_)) +} + impl OctezClientConfig { pub fn base_dir(&self) -> &Directory { self.base_dir.as_ref() @@ -958,4 +964,43 @@ mod test { "invalid checksum" ); } + + #[test] + fn serialize_config() { + let endpoint = Endpoint::localhost(8888); + let tmp_file = NamedTempFile::new().unwrap(); + let tmp_dir = TempDir::new().unwrap(); + let base_dir = tmp_dir.path().to_path_buf(); + let binary_path = tmp_file.path().to_path_buf(); + assert_eq!( + serde_json::to_value( + OctezClientConfigBuilder::new(endpoint) + .set_base_dir(base_dir.clone()) + .set_binary_path(binary_path.clone()) + .set_disable_unsafe_disclaimer(false) + .build() + .unwrap() + ) + .unwrap(), + serde_json::json!({ "base_dir": base_dir.to_string_lossy(), "binary_path": binary_path.to_string_lossy(), "disable_unsafe_disclaimer": false, "octez_node_endpoint": "http://localhost:8888" }) + ); + } + + #[test] + fn serialize_config_no_base_dir() { + let endpoint = Endpoint::localhost(8888); + let tmp_file = NamedTempFile::new().unwrap(); + let client_path = tmp_file.path().to_path_buf(); + assert_eq!( + serde_json::to_value( + OctezClientConfigBuilder::new(endpoint) + .set_binary_path(client_path.clone()) + .set_disable_unsafe_disclaimer(false) + .build() + .unwrap() + ) + .unwrap(), + serde_json::json!({ "binary_path": client_path.to_string_lossy(), "disable_unsafe_disclaimer": false, "octez_node_endpoint": "http://localhost:8888" }) + ); + } } diff --git a/crates/octez/src/async/directory.rs b/crates/octez/src/async/directory.rs index 79f40c3df..d6e3632f2 100644 --- a/crates/octez/src/async/directory.rs +++ b/crates/octez/src/async/directory.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; use anyhow::{bail, Result}; +use serde::Serialize; use tempfile::TempDir; #[derive(Debug)] @@ -9,6 +10,19 @@ pub enum Directory { Path(PathBuf), } +impl Serialize for Directory { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: serde::Serializer, + { + let s = match self { + Directory::Path(p) => p.to_string_lossy(), + Directory::TempDir(p) => p.path().to_string_lossy(), + }; + serializer.serialize_str(&s) + } +} + impl Default for Directory { fn default() -> Self { Self::TempDir(TempDir::new().unwrap()) @@ -121,4 +135,21 @@ mod test { let path_buf: PathBuf = (&directory).into(); assert_eq!(path_buf, dir_path); } + + #[test] + fn serialize() { + let temp_dir = TempDir::new().unwrap(); + let dir_path = temp_dir.path().to_path_buf(); + let directory = Directory::Path(dir_path.clone()); + assert_eq!( + serde_json::to_value(&directory).unwrap(), + serde_json::json!(dir_path.to_string_lossy()) + ); + + let directory = Directory::TempDir(temp_dir); + assert_eq!( + serde_json::to_value(&directory).unwrap(), + serde_json::json!(dir_path.to_string_lossy()) + ); + } }