emojifinder_core/
error.rs1use failure::{Context, Fail};
2use std::fmt;
3
4#[derive(Debug, Fail)]
5pub struct Error {
6 ctx: Context<ErrorKind>,
7}
8
9impl Error {
10 pub fn parse<T: AsRef<str>>(msg: T) -> Error {
11 Error::from(ErrorKind::Parse(msg.as_ref().to_string()))
12 }
13
14 pub fn clipboard<T: AsRef<str>>(msg: T) -> Error {
15 Error::from(ErrorKind::Clipboard(msg.as_ref().to_string()))
16 }
17}
18
19impl fmt::Display for Error {
20 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21 self.ctx.fmt(f)
22 }
23}
24
25#[derive(Clone, Debug, Eq, PartialEq)]
26pub enum ErrorKind {
27 Parse(String),
28 Clipboard(String),
29}
30
31impl fmt::Display for ErrorKind {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 match *self {
34 ErrorKind::Parse(ref msg) => write!(f, "Parse error: {}", msg),
35 ErrorKind::Clipboard(ref msg) => write!(f, "Clipboard error: {}", msg),
36 }
37 }
38}
39
40impl From<ErrorKind> for Error {
41 fn from(kind: ErrorKind) -> Error {
42 Error::from(Context::new(kind))
43 }
44}
45
46impl From<Context<ErrorKind>> for Error {
47 fn from(ctx: Context<ErrorKind>) -> Error {
48 Error { ctx }
49 }
50}