From 8fcc94c6a249b7b2b1ce490d85c64ef9b062b288 Mon Sep 17 00:00:00 2001 From: Sarthak Singh Date: Sat, 25 Jul 2020 10:39:40 -0700 Subject: [PATCH 1/2] ConfigBuilder::build can now consume config Fixed the error returned by build --- src/config/runtime.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/config/runtime.rs b/src/config/runtime.rs index 6b80019f..afb540bd 100644 --- a/src/config/runtime.rs +++ b/src/config/runtime.rs @@ -14,6 +14,12 @@ pub struct Config { loggers: Vec, } +/// Any structs that impl this trait can be consumed by ConfigBuilder to make +/// build a config +pub trait ConfigBuildable { + fn builder_unwrap(self) -> (Option, Root); +} + impl Config { /// Creates a new `ConfigBuilder`. pub fn builder() -> ConfigBuilder { @@ -53,6 +59,15 @@ impl Config { } } +impl ConfigBuildable for Config { + fn builder_unwrap(self) -> (Option, Root) { + ( + Some(ConfigBuilder{ appenders: self.appenders, loggers: self.loggers }), + self.root + ) + } +} + /// A builder for `Config`s. #[derive(Debug, Default)] pub struct ConfigBuilder { @@ -91,6 +106,11 @@ impl ConfigBuilder { self } + /// Consumes two `ConfigBuilders` and combines them into one + pub fn combine(self, ConfigBuilder { appenders, loggers }: ConfigBuilder) -> ConfigBuilder { + self.appenders(appenders).loggers(loggers) + } + /// Consumes the `ConfigBuilder`, returning the `Config`. /// /// Unlike `build`, this method will always return a `Config` by stripping @@ -156,8 +176,13 @@ impl ConfigBuilder { } /// Consumes the `ConfigBuilder`, returning the `Config`. - pub fn build(self, root: Root) -> Result { - let (config, errors) = self.build_lossy(root); + pub fn build(self, buildable: impl ConfigBuildable) -> Result { + let (config_builder, root) = buildable.builder_unwrap(); + + let (config, errors) = if let Some(config_builder) = config_builder { + self.combine(config_builder) + } else { self }.build_lossy(root); + if errors.is_empty() { Ok(config) } else { @@ -195,6 +220,12 @@ impl Root { } } +impl ConfigBuildable for Root { + fn builder_unwrap(self) -> (Option, Root) { + (None, self) + } +} + /// A builder for `Root`s. #[derive(Clone, Eq, PartialEq, Hash, Debug)] pub struct RootBuilder { From 151f638c2f3f84cc9c96f171f0a6f117425f5ac5 Mon Sep 17 00:00:00 2001 From: Sarthak Singh Date: Sat, 25 Jul 2020 11:06:34 -0700 Subject: [PATCH 2/2] Fixed fmt --- src/config/runtime.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/config/runtime.rs b/src/config/runtime.rs index afb540bd..5fe969ff 100644 --- a/src/config/runtime.rs +++ b/src/config/runtime.rs @@ -17,6 +17,7 @@ pub struct Config { /// Any structs that impl this trait can be consumed by ConfigBuilder to make /// build a config pub trait ConfigBuildable { + /// Unwraps the stuct into structs the builder can build from fn builder_unwrap(self) -> (Option, Root); } @@ -60,11 +61,14 @@ impl Config { } impl ConfigBuildable for Config { - fn builder_unwrap(self) -> (Option, Root) { + fn builder_unwrap(self) -> (Option, Root) { ( - Some(ConfigBuilder{ appenders: self.appenders, loggers: self.loggers }), - self.root - ) + Some(ConfigBuilder { + appenders: self.appenders, + loggers: self.loggers, + }), + self.root, + ) } } @@ -181,7 +185,10 @@ impl ConfigBuilder { let (config, errors) = if let Some(config_builder) = config_builder { self.combine(config_builder) - } else { self }.build_lossy(root); + } else { + self + } + .build_lossy(root); if errors.is_empty() { Ok(config) @@ -221,7 +228,7 @@ impl Root { } impl ConfigBuildable for Root { - fn builder_unwrap(self) -> (Option, Root) { + fn builder_unwrap(self) -> (Option, Root) { (None, self) } }