Skip to content

Commit

Permalink
feat(octez): serialise octez client config
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Nov 20, 2024
1 parent bf1af2d commit 87a0da1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
47 changes: 46 additions & 1 deletion crates/octez/src/async/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Directory>,
octez_node_endpoint: Endpoint,
disable_unsafe_disclaimer: bool,
}

fn skip_serializing_base_dir(d: &Arc<Directory>) -> bool {
matches!(d.as_ref(), &Directory::TempDir(_))
}

impl OctezClientConfig {
pub fn base_dir(&self) -> &Directory {
self.base_dir.as_ref()
Expand Down Expand Up @@ -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" })
);
}
}
31 changes: 31 additions & 0 deletions crates/octez/src/async/directory.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use anyhow::{bail, Result};
use serde::Serialize;
use tempfile::TempDir;

#[derive(Debug)]
Expand All @@ -9,6 +10,19 @@ pub enum Directory {
Path(PathBuf),
}

impl Serialize for Directory {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
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())
Expand Down Expand Up @@ -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())
);
}
}

0 comments on commit 87a0da1

Please sign in to comment.