Skip to content

Commit

Permalink
allow generic settings
Browse files Browse the repository at this point in the history
  • Loading branch information
TroyKomodo committed Apr 29, 2024
1 parent 67e04f1 commit d34f1a5
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 21 deletions.
4 changes: 4 additions & 0 deletions foundations/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ path = "src/simple.rs"
name = "http"
path = "src/http.rs"

[[example]]
name = "generics"
path = "src/generics.rs"

[dependencies]
scuffle-foundations = { path = "../" }
tracing = "0.1"
Expand Down
20 changes: 20 additions & 0 deletions foundations/examples/src/generics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use scuffle_foundations::settings::{auto_settings, Settings};

#[auto_settings]
pub struct BaseSettings<S: Settings + Default> {
#[serde(flatten)]
/// The internal settings.
external: S,
}

#[auto_settings]
pub struct ExtraSettings {
/// An extra setting.
pub extra: bool,
/// Another extra setting.
pub another: bool,
}

fn main() {
println!("{}", BaseSettings::<ExtraSettings>::default().to_yaml_string().unwrap());
}
4 changes: 2 additions & 2 deletions foundations/examples/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use scuffle_foundations::runtime::spawn;
use scuffle_foundations::settings::cli::Matches;
use scuffle_foundations::telementry::opentelemetry::OpenTelemetrySpanExt;
use scuffle_foundations::telementry::settings::TelementrySettings;
use scuffle_foundations::{settings::settings, wrapped, BootstrapResult};
use scuffle_foundations::{settings::auto_settings, wrapped, BootstrapResult};
use std::convert::Infallible;
use std::net::SocketAddr;
use std::net::TcpListener as StdTcpListener;
use tokio::net::{TcpListener, TcpStream};

type Body = http_body_util::Full<Bytes>;

#[settings]
#[auto_settings]
#[serde(default)]
struct Config {
telemetry: TelementrySettings,
Expand Down
4 changes: 2 additions & 2 deletions foundations/examples/src/simple.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use scuffle_foundations::{
bootstrap::{bootstrap, Bootstrap, RuntimeSettings},
settings::{cli::Matches, settings},
settings::{cli::Matches, auto_settings},
telementry::{metrics::metrics, settings::TelementrySettings},
};

Expand All @@ -24,7 +24,7 @@ mod http_server {
pub fn requests_failed_total(endpoint_name: &Arc<String>, status_code: u16) -> Counter;
}

#[settings]
#[auto_settings]
pub struct HttpServerSettings {
/// Telementry Settings
telementry: TelementrySettings,
Expand Down
4 changes: 3 additions & 1 deletion foundations/macros/src/settings/types/field_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use syn::{LitStr, Meta};
use crate::helpers::parse_docs;

use super::{
serde::{parse_default_fn, Name, RenameAll},
serde::{parse_default_fn, serde_flatten, Name, RenameAll},
Args,
};

Expand All @@ -14,6 +14,7 @@ pub struct Field {
pub default_fn: Option<proc_macro2::TokenStream>,
pub args: FieldArgs,
pub item: syn::Field,
pub flatten: bool,
}

impl Field {
Expand All @@ -27,6 +28,7 @@ impl Field {
docs: parse_docs(&item.attrs),
default_fn: parse_default_fn(&item.attrs)?,
args: FieldArgs::parse(&item.attrs)?,
flatten: serde_flatten(&item.attrs)?,
item,
})
}
Expand Down
7 changes: 7 additions & 0 deletions foundations/macros/src/settings/types/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ pub fn parse_default_fn(attrs: &[syn::Attribute]) -> syn::Result<Option<proc_mac
})
}

pub fn serde_flatten(attrs: &[syn::Attribute]) -> syn::Result<bool> {
parse_serde_attrs(attrs, false, |state, meta| match meta {
Meta::Path(meta) if meta.is_ident("flatten") => *state = true,
_ => {}
})
}

fn parse_serde_attrs<T>(
attr: &[syn::Attribute],
state: T,
Expand Down
12 changes: 10 additions & 2 deletions foundations/macros/src/settings/types/struct_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl Struct {
})
.collect::<Vec<_>>();

let insert = if docs.is_empty() {
let insert = if docs.is_empty() || field.flatten {
quote::quote! {}
} else {
quote::quote! {
Expand All @@ -111,10 +111,18 @@ impl Struct {
let name = &name.serialize;
let ident = field.item.ident.as_ref().unwrap();

let name_push = if field.flatten {
quote::quote! {}
} else {
quote::quote! {
parent_key.push(::std::borrow::Cow::Borrowed(#name));
}
};

quote::quote! {
{
let mut parent_key = parent_key.to_vec();
parent_key.push(::std::borrow::Cow::Borrowed(#name));
#name_push
(&&&&#crate_path::settings::Wrapped(&self.#ident)).add_docs(&parent_key, docs);
#insert
}
Expand Down
4 changes: 2 additions & 2 deletions foundations/macros/src/settings/types/variant_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ impl Variant {
})
.collect::<Vec<_>>();

let insert = if docs.is_empty() {
let insert = if docs.is_empty() || field.flatten {
quote::quote! {}
} else {
quote::quote! {
docs.insert(parent_key, ::std::borrow::Cow::Borrowed(&[#(#docs),*]));
}
};

let name_push = if self.fields.len() == 1 && field.name.is_none() {
let name_push = if (self.fields.len() == 1 && field.name.is_none()) || field.flatten {
quote::quote! {}
} else {
let name = field
Expand Down
2 changes: 1 addition & 1 deletion foundations/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde_yaml::Value;
pub mod cli;

#[cfg(feature = "macros")]
pub use scuffle_foundations_macros::{auto_settings as settings, Settings};
pub use scuffle_foundations_macros::{auto_settings, Settings};

#[derive(Debug, Clone)]
pub struct SettingsParser<S> {
Expand Down
2 changes: 2 additions & 0 deletions foundations/src/settings/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,5 @@ where
(**self.0).add_docs(parent_key, docs);
}
}

impl Settings for () {}
22 changes: 11 additions & 11 deletions foundations/src/telementry/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use opentelemetry_sdk::Resource;
#[cfg(feature = "logging")]
use tracing_subscriber::fmt::time::{ChronoLocal, ChronoUtc};

#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
pub struct TelementrySettings {
/// Settings for metric exporting.
pub metrics: MetricsSettings,
Expand All @@ -30,7 +30,7 @@ pub struct TelementrySettings {
}

#[cfg(feature = "metrics")]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
pub struct MetricsSettings {
/// Whether to enable metrics.
#[settings(default = true)]
Expand All @@ -40,7 +40,7 @@ pub struct MetricsSettings {
}

#[cfg(feature = "opentelemetry")]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
#[serde(default)]
pub struct OpentelemetrySettings {
/// Whether to enable opentelemetry span exporting.
Expand Down Expand Up @@ -98,7 +98,7 @@ pub struct OpentelemetrySettings {
}

#[cfg(feature = "opentelemetry")]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
#[serde(default)]
pub struct OpentelemetrySettingsLogging {
#[settings(default = OpentelemetrySettingsLoggingLevel::Warn)]
Expand All @@ -110,7 +110,7 @@ pub struct OpentelemetrySettingsLogging {
}

#[cfg(feature = "opentelemetry")]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
#[serde(rename_all = "lowercase")]
pub enum OpentelemetrySettingsLoggingLevel {
/// Error level logging.
Expand All @@ -129,7 +129,7 @@ pub enum OpentelemetrySettingsLoggingLevel {
}

#[cfg(feature = "opentelemetry")]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
#[serde(rename_all = "lowercase")]
pub enum OpentelemetrySettingsExportMethod {
#[settings(default)]
Expand All @@ -140,7 +140,7 @@ pub enum OpentelemetrySettingsExportMethod {
}

#[cfg(feature = "opentelemetry")]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
#[serde(rename_all = "lowercase")]
pub enum OpentelemetrySettingsSampler {
/// Always sample all spans.
Expand Down Expand Up @@ -174,7 +174,7 @@ fn default_true() -> bool {
}

#[cfg(feature = "logging")]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
#[serde(default)]
pub struct LoggingSettings {
/// Whether to enable logging.
Expand Down Expand Up @@ -209,7 +209,7 @@ pub struct LoggingSettings {
}

#[cfg(feature = "logging")]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
#[serde(rename_all = "lowercase")]
pub enum LoggingSettingsTimestamps {
/// Show timestamps in logs in the local timezone.
Expand All @@ -222,7 +222,7 @@ pub enum LoggingSettingsTimestamps {
}

#[cfg(feature = "logging")]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
#[serde(rename_all = "lowercase")]
pub enum LoggingSettingsFormat {
#[settings(default)]
Expand All @@ -240,7 +240,7 @@ pub enum LoggingSettingsFormat {
any(feature = "pprof-cpu", feature = "pprof-heap", feature = "metrics",),
feature = "telemetry-server"
))]
#[crate::settings::settings(crate_path = "crate")]
#[crate::settings::auto_settings(crate_path = "crate")]
#[serde(default)]
pub struct ServerSettings {
/// Whether to enable the server.
Expand Down

0 comments on commit d34f1a5

Please sign in to comment.