From 3a91ef772d3ff10e0aa2e116c03d1b9ba2692942 Mon Sep 17 00:00:00 2001 From: Logan Darklock Date: Fri, 22 Jan 2021 16:45:11 -0800 Subject: [PATCH] Building a Config cannot fail --- src/config.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index e9e11f1..603ba85 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,12 +27,13 @@ use glutin::dpi::LogicalSize; /// /// let config = ConfigBuilder::default() /// .invert_y(false) -/// .build().unwrap(); +/// .build(); /// ``` /// /// If there's a config option you want to see or think is missing, please open an issue! #[non_exhaustive] #[builder(default)] +#[builder(build_fn(skip))] #[derive(Clone, PartialEq, Debug, Builder)] pub struct Config { /// Sets the pixel dimensions of the buffer. The buffer will automatically stretch to fill the @@ -54,6 +55,28 @@ pub struct Config { pub invert_y: bool } +impl ConfigBuilder { + /// Builds a new [`Config`]. + pub fn build(&self) -> Config { + let mut config = Config::default(); + + macro_rules! fields { + ($($n:ident),+) => { + $( + if let Some($n) = &self.$n { + config.$n = $n.clone(); + } + )+ + } + } + + // I guess this is better than implementing the entire builder by hand + fields!(buffer_size, resizable, window_title, window_size, invert_y); + + config + } +} + impl Default for Config { fn default() -> Self { Config {