Skip to content

Commit 5c0f3a5

Browse files
committed
Switch to concrete error type
1 parent a9a477c commit 5c0f3a5

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

examples/layout.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
99
root.fill(&full_palette::WHITE)?;
1010

1111
let mut chart = ChartLayout::new(&root);
12-
chart.set_title_text("Chart Title").unwrap();
13-
chart.draw().unwrap();
12+
chart.set_title_text("Chart Title")?.draw()?;
1413

1514
Ok(())
1615
}

src/chart/layout/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::coord::Shift;
22

3-
use crate::drawing::DrawingArea;
3+
use crate::drawing::{DrawingArea, DrawingAreaErrorKind};
44
use crate::style::IntoFont;
5-
use crate::style::{colors, TextStyle};
5+
use crate::style::TextStyle;
66

77
use plotters_backend::DrawingBackend;
88

@@ -32,7 +32,7 @@ impl<'a, 'b, DB: DrawingBackend> ChartLayout<'a, 'b, DB> {
3232
}
3333
}
3434

35-
pub fn draw(&mut self) -> Result<&mut Self, Box<dyn std::error::Error + 'a>> {
35+
pub fn draw(&mut self) -> Result<&mut Self, DrawingAreaErrorKind<DB::ErrorType>> {
3636
let (x_range, y_range) = self.root_area.get_pixel_range();
3737
let (x_top, y_top) = (x_range.start, y_range.start);
3838
let (w, h) = (x_range.end - x_top, y_range.end - y_top);
@@ -54,11 +54,12 @@ impl<'a, 'b, DB: DrawingBackend> ChartLayout<'a, 'b, DB> {
5454
pub fn set_title_text<S: AsRef<str>>(
5555
&mut self,
5656
title: S,
57-
) -> Result<&mut Self, Box<dyn std::error::Error + 'a>> {
57+
) -> Result<&mut Self, DrawingAreaErrorKind<DB::ErrorType>> {
5858
self.title_text = Some(title.as_ref().to_string());
5959
let (w, h) = self
6060
.root_area
61-
.estimate_text_size(&self.title_text.as_ref().unwrap(), &self.title_style)?;
61+
.estimate_text_size(&self.title_text.as_ref().unwrap(), &self.title_style)
62+
.unwrap();
6263
self.nodes.set_chart_title_size(w as i32, h as i32)?;
6364
Ok(self)
6465
}
@@ -87,4 +88,4 @@ mod test {
8788
let mut chart = ChartLayout::new(&drawing_area);
8889
}
8990
}
90-
*/
91+
*/

src/chart/layout/nodes.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ macro_rules! impl_set_size {
6666
&mut self,
6767
w: i32,
6868
h: i32,
69-
) -> Result<(), Box<dyn std::error::Error>> {
69+
) -> Result<(), stretch::Error> {
7070
self.stretch_context.set_measure(
7171
self.$name,
7272
Some(new_measure_func_with_min_sizes(w as f32, h as f32)),
@@ -83,7 +83,7 @@ macro_rules! impl_set_size {
8383
&mut self,
8484
w: i32,
8585
h: i32,
86-
) -> Result<(), Box<dyn std::error::Error>> {
86+
) -> Result<(), stretch::Error> {
8787
self.stretch_context.set_measure(
8888
self.$name.$sub_part,
8989
Some(new_measure_func_with_min_sizes(w as f32, h as f32)),
@@ -106,7 +106,7 @@ pub(crate) struct CenteredLabelLayout {
106106
impl CenteredLabelLayout {
107107
/// Create an inner node that is `justify-content: center` with respect
108108
/// to its outer node.
109-
fn new(stretch_context: &mut Stretch) -> Result<Self, Box<dyn std::error::Error>> {
109+
fn new(stretch_context: &mut Stretch) -> Result<Self, stretch::Error> {
110110
let inner = stretch_context.new_leaf(
111111
Default::default(),
112112
Box::new(|constraint| {
@@ -127,7 +127,7 @@ impl CenteredLabelLayout {
127127
Ok(Self { inner, outer })
128128
}
129129
/// Create an inner node that is horizontally centered in its 100% width parent.
130-
fn new_row_layout(stretch_context: &mut Stretch) -> Result<Self, Box<dyn std::error::Error>> {
130+
fn new_row_layout(stretch_context: &mut Stretch) -> Result<Self, stretch::Error> {
131131
let layout = Self::new(stretch_context)?;
132132
// If the layout is placed in a row, the outer should have 100% width.
133133
let outer_style = *stretch_context.style(layout.outer)?;
@@ -142,7 +142,7 @@ impl CenteredLabelLayout {
142142
Ok(layout)
143143
}
144144
/// Create an inner node that is vertically centered in its 100% height parent.
145-
fn new_col_layout(stretch_context: &mut Stretch) -> Result<Self, Box<dyn std::error::Error>> {
145+
fn new_col_layout(stretch_context: &mut Stretch) -> Result<Self, stretch::Error> {
146146
let layout = Self::new(stretch_context)?;
147147
// If the layout is placed in a row, the outer should have 100% width.
148148
let outer_style = *stretch_context.style(layout.outer)?;
@@ -232,7 +232,7 @@ pub(crate) struct ChartLayoutNodes {
232232
impl ChartLayoutNodes {
233233
/// Create a new `ChartLayoutNodes`. All margins/padding/sizes are set to 0
234234
/// and should be overridden as needed.
235-
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
235+
pub fn new() -> Result<Self, stretch::Error> {
236236
// Set up the layout engine
237237
let mut stretch_context = Stretch::new();
238238

@@ -312,7 +312,7 @@ impl ChartLayoutNodes {
312312
}
313313
/// Compute the layout of all items to fill a container of width
314314
/// `w` and height `h`.
315-
pub fn layout(&mut self, w: u32, h: u32) -> Result<(), Box<dyn std::error::Error>> {
315+
pub fn layout(&mut self, w: u32, h: u32) -> Result<(), stretch::Error> {
316316
// Compute the initial layout
317317
self.stretch_context.compute_layout(
318318
self.outer_container,
@@ -381,7 +381,7 @@ impl ChartLayoutNodes {
381381
&mut self,
382382
w: i32,
383383
h: i32,
384-
) -> Result<(), Box<dyn std::error::Error>> {
384+
) -> Result<(), stretch::Error> {
385385
self.stretch_context.set_measure(
386386
self.chart_area,
387387
Some(new_measure_func_with_min_sizes(w as f32, h as f32)),
@@ -420,7 +420,7 @@ impl ChartLayoutNodes {
420420
fn packed_title_label_area(
421421
stretch_context: &mut Stretch,
422422
flex_direction: FlexDirection,
423-
) -> Result<(Node, CenteredLabelLayout, Node), Box<dyn std::error::Error>> {
423+
) -> Result<(Node, CenteredLabelLayout, Node), stretch::Error> {
424424
let title = match flex_direction {
425425
FlexDirection::Row | FlexDirection::RowReverse => {
426426
// If the title and the label are packed in a row, the title should be centered in a *column*.

src/drawing/area.rs

+9
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ pub enum DrawingAreaErrorKind<E: Error + Send + Sync> {
141141
SharingError,
142142
/// The error caused by invalid layout
143143
LayoutError,
144+
/// Layout error coming from the Stretch library
145+
StretchError(stretch::Error),
144146
}
145147

146148
impl<E: Error + Send + Sync> std::fmt::Display for DrawingAreaErrorKind<E> {
@@ -151,12 +153,19 @@ impl<E: Error + Send + Sync> std::fmt::Display for DrawingAreaErrorKind<E> {
151153
write!(fmt, "Multiple backend operation in progress")
152154
}
153155
DrawingAreaErrorKind::LayoutError => write!(fmt, "Bad layout"),
156+
DrawingAreaErrorKind::StretchError(e) => e.fmt(fmt),
154157
}
155158
}
156159
}
157160

158161
impl<E: Error + Send + Sync> Error for DrawingAreaErrorKind<E> {}
159162

163+
impl<E: Error + Send + Sync> From<stretch::Error> for DrawingAreaErrorKind<E> {
164+
fn from(err: stretch::Error) -> Self {
165+
DrawingAreaErrorKind::StretchError(err)
166+
}
167+
}
168+
160169
#[allow(type_alias_bounds)]
161170
type DrawingAreaError<T: DrawingBackend> = DrawingAreaErrorKind<T::ErrorType>;
162171

0 commit comments

Comments
 (0)