Changed gradient builder to be more clear about what caused certain errors.
This commit is contained in:
parent
c4565759e4
commit
ab311c9375
1 changed files with 30 additions and 22 deletions
|
|
@ -103,7 +103,7 @@ pub struct Builder {
|
||||||
start: Point,
|
start: Point,
|
||||||
end: Point,
|
end: Point,
|
||||||
stops: Vec<ColorStop>,
|
stops: Vec<ColorStop>,
|
||||||
valid: bool,
|
error: Option<BuilderError>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Builder {
|
impl Builder {
|
||||||
|
|
@ -126,7 +126,7 @@ impl Builder {
|
||||||
start,
|
start,
|
||||||
end,
|
end,
|
||||||
stops: vec![],
|
stops: vec![],
|
||||||
valid: true,
|
error: None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -147,35 +147,43 @@ impl Builder {
|
||||||
stop.offset.partial_cmp(&offset).unwrap()
|
stop.offset.partial_cmp(&offset).unwrap()
|
||||||
}) {
|
}) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
//the offset already exists in the gradient
|
self.error = Some(BuilderError::DuplicateOffset(offset))
|
||||||
self.valid = false;
|
},
|
||||||
}
|
|
||||||
Err(index) => {
|
Err(index) => {
|
||||||
self.stops.insert(index, ColorStop { offset, color })
|
self.stops.insert(index, ColorStop { offset, color });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.valid = false;
|
self.error = Some(BuilderError::InvalidOffset(offset))
|
||||||
}
|
};
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds the linear [`Gradient`] of this [`Builder`].
|
/// Builds the linear [`Gradient`] of this [`Builder`].
|
||||||
///
|
///
|
||||||
/// Returns `Err` if no stops were added to the builder or
|
/// Returns `BuilderError` if gradient in invalid.
|
||||||
/// if stops not between 0.0 and 1.0 were added.
|
pub fn build(self) -> Result<Gradient, BuilderError> {
|
||||||
pub fn build(self) -> Result<Gradient, &'static str> {
|
if self.stops.is_empty() {
|
||||||
if self.stops.is_empty() || !self.valid {
|
Err(BuilderError::MissingColorStop)
|
||||||
return Err("Valid gradient conditions: \
|
} else if let Some(error) = self.error {
|
||||||
1) Must contain at least one color stop. \
|
Err(error)
|
||||||
2) Every color stop offset must be unique. \
|
} else {
|
||||||
3) Every color stop must be within the range of 0.0..=1.0");
|
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),
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue