Skip to content

Commit

Permalink
feat(octez): serialise baker config
Browse files Browse the repository at this point in the history
  • Loading branch information
huancheng-trili committed Nov 15, 2024
1 parent eaa9af1 commit f15e8c6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
33 changes: 32 additions & 1 deletion crates/octez/src/async/baker.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Result};
use serde::Serialize;
use std::{fmt::Display, path::PathBuf};
use tokio::process::{Child, Command};

Expand All @@ -10,6 +11,15 @@ pub enum BakerBinaryPath {
Custom(PathBuf), // The binary is at the given path
}

impl Serialize for BakerBinaryPath {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
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 {
Expand All @@ -25,7 +35,7 @@ impl Display for BakerBinaryPath {
}
}

#[derive(Clone)]
#[derive(Clone, Serialize)]
pub struct OctezBakerConfig {
binary_path: BakerBinaryPath,
octez_client_base_dir: PathBuf,
Expand Down Expand Up @@ -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"
})
)
}
}
18 changes: 18 additions & 0 deletions crates/octez/src/async/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -46,6 +47,15 @@ pub enum Protocol {
Quebec,
}

impl Serialize for Protocol {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.hash())
}
}

impl Default for Protocol {
fn default() -> Self {
Self::Alpha
Expand Down Expand Up @@ -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\""
)
}
}

0 comments on commit f15e8c6

Please sign in to comment.