diff options
author | 2022-10-18 17:45:47 -0700 | |
---|---|---|
committer | 2022-10-18 17:45:47 -0700 | |
commit | ab311c9375999d568a599b31424a12beb3f02ad2 (patch) | |
tree | db46922e75092f8068549cdbe829cd8981f09e7f /graphics | |
parent | c4565759e4294540f54a81e4d91ddea7a769d3d4 (diff) | |
download | iced-ab311c9375999d568a599b31424a12beb3f02ad2.tar.gz iced-ab311c9375999d568a599b31424a12beb3f02ad2.tar.bz2 iced-ab311c9375999d568a599b31424a12beb3f02ad2.zip |
Changed gradient builder to be more clear about what caused certain errors.
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/src/gradient/linear.rs | 52 |
1 files changed, 30 insertions, 22 deletions
diff --git a/graphics/src/gradient/linear.rs b/graphics/src/gradient/linear.rs index 020c65db..03dd04a2 100644 --- a/graphics/src/gradient/linear.rs +++ b/graphics/src/gradient/linear.rs @@ -103,7 +103,7 @@ pub struct Builder { start: Point, end: Point, stops: Vec<ColorStop>, - valid: bool, + error: Option<BuilderError> } impl Builder { @@ -126,7 +126,7 @@ impl Builder { start, end, stops: vec![], - valid: true, + error: None } } @@ -147,35 +147,43 @@ impl Builder { stop.offset.partial_cmp(&offset).unwrap() }) { Ok(_) => { - //the offset already exists in the gradient - self.valid = false; - } + self.error = Some(BuilderError::DuplicateOffset(offset)) + }, Err(index) => { - self.stops.insert(index, ColorStop { offset, color }) + self.stops.insert(index, ColorStop { offset, color }); } } } else { - self.valid = false; - } + self.error = Some(BuilderError::InvalidOffset(offset)) + }; + self } /// Builds the linear [`Gradient`] of this [`Builder`]. /// - /// Returns `Err` if no stops were added to the builder or - /// if stops not between 0.0 and 1.0 were added. - pub fn build(self) -> Result<Gradient, &'static str> { - if self.stops.is_empty() || !self.valid { - return Err("Valid gradient conditions: \ - 1) Must contain at least one color stop. \ - 2) Every color stop offset must be unique. \ - 3) Every color stop must be within the range of 0.0..=1.0"); + /// Returns `BuilderError` if gradient in invalid. + pub fn build(self) -> Result<Gradient, BuilderError> { + if self.stops.is_empty() { + Err(BuilderError::MissingColorStop) + } else if let Some(error) = self.error { + Err(error) + } else { + Ok(Gradient::Linear(Linear { + start: self.start, + end: self.end, + color_stops: self.stops, + })) } - - Ok(Gradient::Linear(Linear { - start: self.start, - end: self.end, - color_stops: self.stops, - })) } } + +#[derive(Debug, thiserror::Error)] +pub enum BuilderError { + #[error("Gradients must contain at least one color stop.")] + MissingColorStop, + #[error("Offset {0} must be a unique, finite number.")] + DuplicateOffset(f32), + #[error("Offset {0} must be within 0.0..=1.0.")] + InvalidOffset(f32), +} |