Polish a few things (#67)

* Improve some match statements using macros

* Use `thiserror` instead of manually impl'ing Error
This commit is contained in:
T0mstone
2021-04-10 16:27:44 +02:00
committed by Keavon Chambers
parent a56f50c60c
commit 21ffb3571e
7 changed files with 145 additions and 75 deletions
+1
View File
@@ -11,6 +11,7 @@ license = "Apache-2.0"
[dependencies]
log = "0.4"
bitflags = "1.2.1"
thiserror = "1.0.24"
[dependencies.document-core]
path = "../document"
+8 -4
View File
@@ -31,10 +31,14 @@ pub enum Response {
impl fmt::Display for Response {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self {
Response::UpdateCanvas { document: _ } => write!(formatter, "UpdateCanvas"),
Response::SetActiveTool { tool_name: _ } => write!(formatter, "SetActiveTool"),
}
use Response::*;
let name = match_variant_name!(match (self) {
UpdateCanvas,
SetActiveTool
});
formatter.write_str(name)
}
}
+7 -17
View File
@@ -1,32 +1,22 @@
use crate::events::Event;
use crate::Color;
use std::error::Error;
use std::fmt::{self, Display};
use thiserror::Error;
/// The error type used by the Graphite editor.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Error)]
pub enum EditorError {
#[error("Failed to execute operation: {0}")]
InvalidOperation(String),
#[error("Failed to dispatch event: {0}")]
InvalidEvent(String),
#[error("{0}")]
Misc(String),
#[error("Tried to construct an invalid color {0:?}")]
Color(String),
#[error("The requested tool does not exist")]
UnknownTool,
}
impl Display for EditorError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
EditorError::InvalidOperation(e) => write!(f, "Failed to execute operation: {}", e),
EditorError::InvalidEvent(e) => write!(f, "Failed to dispatch event: {}", e),
EditorError::Misc(e) => write!(f, "{}", e),
EditorError::Color(c) => write!(f, "Tried to construct an invalid color {:?}", c),
EditorError::UnknownTool => write!(f, "The requested tool does not exist"),
}
}
}
impl Error for EditorError {}
macro_rules! derive_from {
($type:ty, $kind:ident) => {
impl From<$type> for EditorError {
+37
View File
@@ -47,3 +47,40 @@ macro_rules! gen_tools_hash_map {
hash_map
}};
}
/// Creates a string representation of an enum value that exactly matches the given name of each enum variant
///
/// # Example
///
/// ```ignore
/// enum E {
/// A(u8),
/// B
/// }
///
/// // this line is important
/// use E::*;
///
/// let a = E::A(7);
/// let s = match_variant_name!(match (a) { A, B });
/// ```
///
/// expands to
///
/// ```ignore
/// // ...
///
/// let s = match a {
/// A { .. } => "A",
/// B { .. } => "B"
/// };
/// ```
macro_rules! match_variant_name {
(match ($e:expr) { $($v:ident),* $(,)? }) => {
match $e {
$(
$v { .. } => stringify!(v)
),*
}
};
}
+27 -23
View File
@@ -124,29 +124,33 @@ pub enum ToolType {
impl fmt::Display for ToolType {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self {
ToolType::Select => write!(formatter, "Select"),
ToolType::Crop => write!(formatter, "Crop"),
ToolType::Navigate => write!(formatter, "Navigate"),
ToolType::Sample => write!(formatter, "Sample"),
ToolType::Text => write!(formatter, "Text"),
ToolType::Fill => write!(formatter, "Fill"),
ToolType::Gradient => write!(formatter, "Gradient"),
ToolType::Brush => write!(formatter, "Brush"),
ToolType::Heal => write!(formatter, "Heal"),
ToolType::Clone => write!(formatter, "Clone"),
ToolType::Patch => write!(formatter, "Patch"),
ToolType::BlurSharpen => write!(formatter, "BlurSharpen"),
ToolType::Relight => write!(formatter, "Relight"),
ToolType::Path => write!(formatter, "Path"),
ToolType::Pen => write!(formatter, "Pen"),
ToolType::Freehand => write!(formatter, "Freehand"),
ToolType::Spline => write!(formatter, "Spline"),
ToolType::Line => write!(formatter, "Line"),
ToolType::Rectangle => write!(formatter, "Rectangle"),
ToolType::Ellipse => write!(formatter, "Ellipse"),
ToolType::Shape => write!(formatter, "Shape"),
}
use ToolType::*;
let name = match_variant_name!(match (self) {
Select,
Crop,
Navigate,
Sample,
Text,
Fill,
Gradient,
Brush,
Heal,
Clone,
Patch,
BlurSharpen,
Relight,
Path,
Pen,
Freehand,
Spline,
Line,
Rectangle,
Ellipse,
Shape
});
formatter.write_str(name)
}
}