Skip to content

Commit

Permalink
feat: config files accept hex strings as seeds
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentElement committed Sep 25, 2024
1 parent 1778a01 commit ca3f38e
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ use serde::{Deserialize, Serialize};

use crate::generators::Standardization;

/// Represents a seed for serde RNGs in the configuration file. Mostly here because we want
/// to ser/de to/from a hex string.
#[warn(missing_docs)]
#[derive(Serialize, Deserialize, Debug)]
pub struct ConfigSeed(Option<[u8; 32]>);
use crate::utils::{decode_hex, encode_hex};

/// `Config` stores the global configuration of the program.
#[warn(missing_docs)]
Expand Down Expand Up @@ -134,17 +130,6 @@ pub struct FontanaGen {
pub n_max_free_vars: u32,
}

impl ConfigSeed {
/// Get the seed item
pub fn get(&self) -> [u8; 32] {
self.0.unwrap_or(thread_rng().gen())
}

pub fn new(seed: [u8; 32]) -> Self {
ConfigSeed(Some(seed))
}
}

impl Reactor {
/// Produce a new `ReactorConfig` struct with default values.
pub fn new() -> Self {
Expand Down Expand Up @@ -235,3 +220,60 @@ impl Config {
self.verbose_logging = logging;
}
}

/// Represents a seed for serde RNGs in the configuration file. Mostly here because we want
/// to ser/de to/from a hex string.
#[warn(missing_docs)]
#[derive(Debug)]
pub struct ConfigSeed(Option<[u8; 32]>);

impl ConfigSeed {
/// Get the seed item
pub fn get(&self) -> [u8; 32] {
self.0.unwrap_or(thread_rng().gen())
}

pub fn seed(&self) -> Option<[u8; 32]> {
self.0
}

pub fn new(seed: [u8; 32]) -> Self {
ConfigSeed(Some(seed))
}

pub fn blank() -> Self {
ConfigSeed(None)
}
}

/// Manually serialize [u8; 32] to a hex string
impl Serialize for ConfigSeed {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if self.seed().is_some() {
serializer.serialize_str(&encode_hex(&self.get()))
} else {
serializer.serialize_none()
}
}
}

/// Manually deserialize a hex string to [u8; 32]
///
/// SAFETY: `panic!`s when hex string is malformed or of odd length
impl<'de> Deserialize<'de> for ConfigSeed {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let seed_string: Option<&str> = serde::de::Deserialize::deserialize(deserializer)?;
Ok(if let Some(s) = seed_string {
let hexvec = decode_hex(s).unwrap();
ConfigSeed::new(hexvec.try_into().unwrap())
} else {
ConfigSeed::blank()
})
}
}

0 comments on commit ca3f38e

Please sign in to comment.