Skip to content

Commit

Permalink
gdk: Simplify RGBABuilder code
Browse files Browse the repository at this point in the history
  • Loading branch information
SeaDve authored and bilelmoussaoui committed Sep 3, 2023
1 parent 3c9e035 commit 567d31e
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions gdk4/src/rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ use crate::RGBA;
use glib::{translate::*, IntoGStr};
use std::{fmt, str::FromStr};

#[derive(Debug, Default)]
#[derive(Debug)]
// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`RGBA`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct RGBABuilder {
red: Option<f32>,
green: Option<f32>,
blue: Option<f32>,
alpha: Option<f32>,
pub struct RGBABuilder(RGBA);

impl Default for RGBABuilder {
fn default() -> Self {
Self(RGBA::WHITE)
}
}

impl RGBABuilder {
Expand All @@ -25,43 +26,30 @@ impl RGBABuilder {
}

pub fn blue(mut self, blue: f32) -> Self {
self.blue = Some(blue);
self.0.set_blue(blue);
self
}

pub fn green(mut self, green: f32) -> Self {
self.green = Some(green);
self.0.set_green(green);
self
}

pub fn red(mut self, red: f32) -> Self {
self.red = Some(red);
self.0.set_red(red);
self
}

pub fn alpha(mut self, alpha: f32) -> Self {
self.alpha = Some(alpha);
self.0.set_alpha(alpha);
self
}

// rustdoc-stripper-ignore-next
/// Build the [`RGBA`].
#[must_use = "The RGBA returned by this builder should probably be used"]
pub fn build(self) -> RGBA {
let mut rgba = RGBA::WHITE;
if let Some(blue) = self.blue {
rgba.set_blue(blue);
}
if let Some(red) = self.red {
rgba.set_red(red);
}
if let Some(green) = self.green {
rgba.set_green(green);
}
if let Some(alpha) = self.alpha {
rgba.set_alpha(alpha);
}
rgba
self.0
}
}

Expand Down

0 comments on commit 567d31e

Please sign in to comment.