Skip to content

Commit ce03482

Browse files
authored
chore: add serde deser to configs (#8)
* chore: add serde deser to configs * chore: version * lint: clippy
1 parent 1204404 commit ce03482

File tree

6 files changed

+31
-7
lines changed

6 files changed

+31
-7
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name = "init4-bin-base"
44
description = "Internal utilities for binaries produced by the init4 team"
55
keywords = ["init4", "bin", "base"]
66

7-
version = "0.2.1"
7+
version = "0.2.2"
88
edition = "2021"
99
rust-version = "1.81"
1010
authors = ["init4", "James Prestwich"]
@@ -36,6 +36,7 @@ chrono = "0.4.40"
3636
# Other
3737
thiserror = "2.0.11"
3838
alloy = { version = "0.12.6", optional = true, default-features = false, features = ["std"] }
39+
serde = { version = "1", features = ["derive"] }
3940

4041
[dev-dependencies]
4142
ajj = "0.3.1"

src/perms/builders.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
from_env::{EnvItemInfo, FromEnv, FromEnvErr, FromEnvVar},
1414
},
1515
};
16+
use serde::{Deserialize, Deserializer};
1617

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

5960
/// An individual builder.
60-
#[derive(Clone, Debug)]
61+
#[derive(Clone, Debug, serde::Deserialize)]
62+
#[serde(from = "String")]
6163
pub struct Builder {
6264
/// The sub of the builder.
6365
pub sub: String,
6466
}
6567

68+
impl From<String> for Builder {
69+
fn from(sub: String) -> Self {
70+
Self { sub }
71+
}
72+
}
73+
6674
impl Builder {
6775
/// Create a new builder.
6876
pub fn new(sub: impl AsRef<str>) -> Self {
@@ -78,19 +86,32 @@ impl Builder {
7886
}
7987

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

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

103+
fn deser_builders<'de, D>(deser: D) -> Result<Vec<Builder>, D::Error>
104+
where
105+
D: Deserializer<'de>,
106+
{
107+
let s = String::deserialize(deser)?;
108+
Ok(split_builders(&s))
109+
}
110+
111+
fn split_builders(s: &str) -> Vec<Builder> {
112+
s.split(',').map(Builder::new).collect()
113+
}
114+
94115
impl Builders {
95116
/// Create a new Builders struct.
96117
pub const fn new(builders: Vec<Builder>, config: SlotAuthzConfig) -> Self {
@@ -188,7 +209,7 @@ impl FromEnv for Builders {
188209
fn from_env() -> Result<Self, FromEnvErr<Self::Error>> {
189210
let s = String::from_env_var(BUILDERS)
190211
.map_err(FromEnvErr::infallible_into::<BuilderConfigError>)?;
191-
let builders = s.split(',').map(Builder::new).collect();
212+
let builders = split_builders(&s);
192213

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

src/perms/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub enum SlotAuthzConfigError {
2525
/// Configuration object that describes the slot time settings for a chain.
2626
///
2727
/// This struct is used to configure the slot authorization system
28-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28+
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
2929
pub struct SlotAuthzConfig {
3030
/// A [`SlotCalculator`] instance that can be used to calculate the slot
3131
/// number for a given timestamp.

src/utils/calc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub enum SlotCalcEnvError {
2424

2525
/// A slot calculator, which can calculate the slot number for a given
2626
/// timestamp.
27-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
27+
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize)]
2828
pub struct SlotCalculator {
2929
/// The start timestamp.
3030
start_timestamp: u64,

src/utils/metrics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ const METRICS_PORT: &str = "METRICS_PORT";
1111
/// Uses the following environment variables:
1212
/// - `METRICS_PORT` - optional. Defaults to 9000 if missing or unparseable.
1313
/// The port to bind the metrics server to.
14-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14+
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
1515
#[non_exhaustive]
16+
#[serde(from = "Option<u16>")]
1617
pub struct MetricsConfig {
1718
/// `METRICS_PORT` - The port on which to bind the metrics server. Defaults
1819
/// to `9000` if missing or unparseable.

src/utils/otlp.rs

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub struct OtelConfig {
100100

101101
/// Defaults to DEBUG.
102102
pub level: tracing::Level,
103+
103104
/// Defaults to 1 second. Specified in Milliseconds.
104105
pub timeout: Duration,
105106

0 commit comments

Comments
 (0)