1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
From 3ae4de4abc172f922d7024b96053fea0b8b5a299 Mon Sep 17 00:00:00 2001
From: Konstantin Senkevich <konstsen@gmail.com>
Date: Mon, 24 Aug 2020 16:30:06 +0300
Subject: [PATCH 07/19] Replace failure dependency.
---
Cargo.toml | 2 +-
src/errors.rs | 16 ++++++++++++++--
src/page.rs | 11 +++++------
src/view.rs | 28 ++++++++++++++++------------
4 files changed, 36 insertions(+), 21 deletions(-)
--- a/src/errors.rs
+++ b/src/errors.rs
@@ -1,3 +1,15 @@
-use failure;
+use thiserror;
-pub type Result<T> = ::std::result::Result<T, failure::Error>;
+#[derive(thiserror::Error, Debug)]
+pub enum Error {
+ #[error("Cannot save page to svg!")]
+ FailedToSave(#[from] std::io::Error),
+ #[error("Invalid {name:?} range: {lower} <= {upper}. Please specify the {name:?} range manually.")]
+ InvalidRange {
+ name: String,
+ lower: f64,
+ upper: f64
+ },
+}
+
+pub type Result<T> = ::std::result::Result<T, Error>;
--- a/src/page.rs
+++ b/src/page.rs
@@ -2,6 +2,7 @@
The `page` module provides structures for laying out and rendering multiple views.
*/
+use crate::errors;
use std::ffi::OsStr;
use std::path::Path;
@@ -12,8 +13,6 @@
use crate::errors::Result;
use crate::view::View;
-use failure::ResultExt;
-
/**
A single page page laying out the views in a grid
*/
@@ -95,7 +94,6 @@
The type of file will be based on the file extension.
*/
-
pub fn save<P>(&self, path: P) -> Result<()>
where
P: AsRef<Path>,
@@ -109,9 +107,10 @@
}
}
- svg::save(&path, &self.to_svg()?)
- .context("saving svg")
- .map_err(From::from)
+ match svg::save(&path, &self.to_svg()?) {
+ Ok(ok) => Ok(ok),
+ Err(error) => Err(errors::Error::FailedToSave(error))
+ }
},
_ => Ok(()),
}
--- a/src/view.rs
+++ b/src/view.rs
@@ -6,10 +6,10 @@
extent to plot and information about the axes. It knows how to render itself.
*/
+use crate::errors;
use std;
use std::f64;
-use failure::format_err;
use svg::Node;
use crate::axis;
@@ -129,21 +129,21 @@
let default_x_range = self.default_x_range();
let x_range = self.x_range.as_ref().unwrap_or(&default_x_range);
if !x_range.is_valid() {
- return Err(format_err!(
- "Invalid x_range: {} >= {}. Please specify the x_range manually.",
- x_range.lower,
- x_range.upper
- ));
+ return Err(errors::Error::InvalidRange {
+ name: String::from("x"),
+ lower: x_range.lower,
+ upper: x_range.upper,
+ });
}
let default_y_range = self.default_y_range();
let y_range = self.y_range.as_ref().unwrap_or(&default_y_range);
if !y_range.is_valid() {
- return Err(format_err!(
- "Invalid y_range: {} >= {}. Please specify the y_range manually.",
- y_range.lower,
- y_range.upper
- ));
+ return Err(errors::Error::InvalidRange {
+ name: String::from("y"),
+ lower: x_range.lower,
+ upper: x_range.upper,
+ });
}
let x_label: String = self.x_label.clone().unwrap_or_else(|| "".to_string());
@@ -358,7 +358,11 @@
let y_range = self.y_range.as_ref().unwrap_or(&default_y_range);
if !y_range.is_valid() {
- return Err(format_err!("invalid y_range: {:?}", y_range));
+ return Err(errors::Error::InvalidRange {
+ name: String::from("y"),
+ lower: y_range.lower,
+ upper: y_range.upper,
+ });
}
let default_x_label = "".to_string();
|