From 7bed1ce54df7384399cfb2059a12a1ec09236c52 Mon Sep 17 00:00:00 2001 From: Huan-Cheng Chang Date: Fri, 15 Nov 2024 17:00:08 +0000 Subject: [PATCH] feat(octez): implement serialize for endpoint --- crates/octez/src/async/endpoint.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/crates/octez/src/async/endpoint.rs b/crates/octez/src/async/endpoint.rs index 1149361ae..bd18d6f1e 100644 --- a/crates/octez/src/async/endpoint.rs +++ b/crates/octez/src/async/endpoint.rs @@ -1,7 +1,6 @@ -#![allow(dead_code)] -use std::fmt::{self, Display}; - use http::{uri::Scheme, Uri}; +use serde::Serialize; +use std::fmt::{self, Display}; #[derive(Debug, Clone, PartialEq)] pub struct Endpoint { @@ -32,6 +31,15 @@ impl Endpoint { } } +impl Serialize for Endpoint { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} + impl Default for Endpoint { fn default() -> Self { Self::localhost(80) @@ -121,4 +129,13 @@ mod tests { let endpoint = Endpoint::localhost(8765); assert!(endpoint.to_string().contains("http://localhost:8765")); } + + #[test] + fn serialize() { + let endpoint = Endpoint::localhost(8765); + assert_eq!( + serde_json::to_string(&endpoint).unwrap(), + "\"http://localhost:8765\"" + ) + } }