Skip to content

chore: add serde deser to configs #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "init4-bin-base"
description = "Internal utilities for binaries produced by the init4 team"
keywords = ["init4", "bin", "base"]

version = "0.2.1"
version = "0.2.2"
edition = "2021"
rust-version = "1.81"
authors = ["init4", "James Prestwich"]
Expand Down Expand Up @@ -36,6 +36,7 @@ chrono = "0.4.40"
# Other
thiserror = "2.0.11"
alloy = { version = "0.12.6", optional = true, default-features = false, features = ["std"] }
serde = { version = "1", features = ["derive"] }

[dev-dependencies]
ajj = "0.3.1"
Expand Down
27 changes: 24 additions & 3 deletions src/perms/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{
from_env::{EnvItemInfo, FromEnv, FromEnvErr, FromEnvVar},
},
};
use serde::{Deserialize, Deserializer};

/// The builder list env var.
const BUILDERS: &str = "PERMISSIONED_BUILDERS";
Expand Down Expand Up @@ -57,12 +58,19 @@ pub enum BuilderConfigError {
}

/// An individual builder.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, serde::Deserialize)]
#[serde(from = "String")]
pub struct Builder {
/// The sub of the builder.
pub sub: String,
}

impl From<String> for Builder {
fn from(sub: String) -> Self {
Self { sub }
}
}

impl Builder {
/// Create a new builder.
pub fn new(sub: impl AsRef<str>) -> Self {
Expand All @@ -78,19 +86,32 @@ impl Builder {
}

/// Builders struct to keep track of the builders that are allowed to perform actions.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, serde::Deserialize)]
pub struct Builders {
/// The list of builders.
///
/// This is configured in the environment variable `PERMISSIONED_BUILDERS`,
/// as a list of comma-separated UUIDs.
#[serde(deserialize_with = "deser_builders")]
pub builders: Vec<Builder>,

/// The slot authorization configuration. See [`SlotAuthzConfig`] for more
/// information and env vars
config: SlotAuthzConfig,
}

fn deser_builders<'de, D>(deser: D) -> Result<Vec<Builder>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deser)?;
Ok(split_builders(&s))
}

fn split_builders(s: &str) -> Vec<Builder> {
s.split(',').map(Builder::new).collect()
}

impl Builders {
/// Create a new Builders struct.
pub const fn new(builders: Vec<Builder>, config: SlotAuthzConfig) -> Self {
Expand Down Expand Up @@ -188,7 +209,7 @@ impl FromEnv for Builders {
fn from_env() -> Result<Self, FromEnvErr<Self::Error>> {
let s = String::from_env_var(BUILDERS)
.map_err(FromEnvErr::infallible_into::<BuilderConfigError>)?;
let builders = s.split(',').map(Builder::new).collect();
let builders = split_builders(&s);

let config = SlotAuthzConfig::from_env().map_err(FromEnvErr::from)?;

Expand Down
2 changes: 1 addition & 1 deletion src/perms/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub enum SlotAuthzConfigError {
/// Configuration object that describes the slot time settings for a chain.
///
/// This struct is used to configure the slot authorization system
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
pub struct SlotAuthzConfig {
/// A [`SlotCalculator`] instance that can be used to calculate the slot
/// number for a given timestamp.
Expand Down
2 changes: 1 addition & 1 deletion src/utils/calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub enum SlotCalcEnvError {

/// A slot calculator, which can calculate the slot number for a given
/// timestamp.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize)]
pub struct SlotCalculator {
/// The start timestamp.
start_timestamp: u64,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ const METRICS_PORT: &str = "METRICS_PORT";
/// Uses the following environment variables:
/// - `METRICS_PORT` - optional. Defaults to 9000 if missing or unparseable.
/// The port to bind the metrics server to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
#[non_exhaustive]
#[serde(from = "Option<u16>")]
pub struct MetricsConfig {
/// `METRICS_PORT` - The port on which to bind the metrics server. Defaults
/// to `9000` if missing or unparseable.
Expand Down
1 change: 1 addition & 0 deletions src/utils/otlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct OtelConfig {

/// Defaults to DEBUG.
pub level: tracing::Level,

/// Defaults to 1 second. Specified in Milliseconds.
pub timeout: Duration,

Expand Down