Building a Config cannot fail

This commit is contained in:
Logan Darklock 2021-01-22 16:45:11 -08:00
parent a51872fb82
commit 3a91ef772d
No known key found for this signature in database
GPG key ID: B8C37CEDE1AC60EA

View file

@ -27,12 +27,13 @@ use glutin::dpi::LogicalSize;
/// ///
/// let config = ConfigBuilder::default() /// let config = ConfigBuilder::default()
/// .invert_y(false) /// .invert_y(false)
/// .build().unwrap(); /// .build();
/// ``` /// ```
/// ///
/// If there's a config option you want to see or think is missing, please open an issue! /// If there's a config option you want to see or think is missing, please open an issue!
#[non_exhaustive] #[non_exhaustive]
#[builder(default)] #[builder(default)]
#[builder(build_fn(skip))]
#[derive(Clone, PartialEq, Debug, Builder)] #[derive(Clone, PartialEq, Debug, Builder)]
pub struct Config { pub struct Config {
/// Sets the pixel dimensions of the buffer. The buffer will automatically stretch to fill the /// 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 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 { impl Default for Config {
fn default() -> Self { fn default() -> Self {
Config { Config {