From e7ccdd485e136f5d16b3f46aa7e7472920c1a1d5 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 9 Sep 2021 12:14:14 -0400 Subject: [PATCH] refine serde, make it default Signed-off-by: Christine Dodrill --- crates/logtail-poster/src/config.rs | 16 ++++++++++------ crates/logtail/Cargo.toml | 9 +-------- crates/logtail/src/lib.rs | 1 - crates/logtail/src/serde.rs | 20 +++++++++++++++++++- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/crates/logtail-poster/src/config.rs b/crates/logtail-poster/src/config.rs index 1f0a00b..ea33ca9 100644 --- a/crates/logtail-poster/src/config.rs +++ b/crates/logtail-poster/src/config.rs @@ -23,9 +23,8 @@ pub(crate) fn state_file(name: &str) -> Option { /// IDs, however in practice the most read value is the private ID. #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)] pub struct Config { - // TODO(Xe): make this use a custom serde format, see https://serde.rs/custom-date-format.html - private_id: String, - public_id: String, + private_id: logtail::PrivateID, + public_id: logtail::PublicID, } impl Config { @@ -35,14 +34,19 @@ impl Config { let private_id = logtail::PrivateID::new(); Self { - private_id: private_id.as_hex(), - public_id: private_id.as_public().as_hex(), + public_id: private_id.as_public(), + private_id, } } /// Returns the logtail private ID for this Config. pub fn private_id(self) -> logtail::PrivateID { - logtail::PrivateID::from_hex(self.private_id).unwrap() + self.private_id + } + + /// Returns the logtail public ID for this Config. + pub fn public_id(self) -> logtail::PublicID { + self.public_id } /// Loads the relevant configuration file off disk, and creates a new one if it diff --git a/crates/logtail/Cargo.toml b/crates/logtail/Cargo.toml index 8a29751..286ff70 100644 --- a/crates/logtail/Cargo.toml +++ b/crates/logtail/Cargo.toml @@ -6,15 +6,8 @@ edition = "2018" # See more keys and their definitions at # https://doc.rust-lang.org/cargo/reference/manifest.html -[features] -default = [ "serde-ser" ] -serde-ser = [ "serde" ] - [dependencies] hex = "0.4" rand = "0" +serde = "1" sha2 = "0.9" - -[dependencies.serde] -version = "1" -optional = true diff --git a/crates/logtail/src/lib.rs b/crates/logtail/src/lib.rs index b85f4ec..316d4a7 100644 --- a/crates/logtail/src/lib.rs +++ b/crates/logtail/src/lib.rs @@ -10,7 +10,6 @@ use std::fmt; #[derive(Clone, PartialEq, Eq)] pub struct PrivateID([u8; SHA256_SIZE]); -#[cfg(feature = "serde-ser")] mod serde; impl PrivateID { diff --git a/crates/logtail/src/serde.rs b/crates/logtail/src/serde.rs index d4c5bcd..8b07220 100644 --- a/crates/logtail/src/serde.rs +++ b/crates/logtail/src/serde.rs @@ -1,5 +1,5 @@ use super::{PrivateID, PublicID}; -use serde::{de, Serialize, Serializer}; +use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::fmt; impl Serialize for PrivateID { @@ -34,6 +34,15 @@ impl<'de> de::Visitor<'de> for PrivateIDVisitor { } } +impl<'de> Deserialize<'de> for PrivateID { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + deserializer.deserialize_str(PrivateIDVisitor) + } +} + impl Serialize for PublicID { fn serialize(&self, serializer: S) -> Result where @@ -65,3 +74,12 @@ impl<'de> de::Visitor<'de> for PublicIDVisitor { } } } + +impl<'de> Deserialize<'de> for PublicID { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + deserializer.deserialize_str(PublicIDVisitor) + } +}