refine serde, make it default

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-09-09 12:14:14 -04:00
parent 916afc8852
commit e7ccdd485e
4 changed files with 30 additions and 16 deletions

View File

@ -23,9 +23,8 @@ pub(crate) fn state_file(name: &str) -> Option<PathBuf> {
/// 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

View File

@ -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

View File

@ -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 {

View File

@ -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<D>(deserializer: D) -> Result<PrivateID, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(PrivateIDVisitor)
}
}
impl Serialize for PublicID {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@ -65,3 +74,12 @@ impl<'de> de::Visitor<'de> for PublicIDVisitor {
}
}
}
impl<'de> Deserialize<'de> for PublicID {
fn deserialize<D>(deserializer: D) -> Result<PublicID, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_str(PublicIDVisitor)
}
}