changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/obj/src/err.rs

changeset 17: c7165d93a9eb
author: ellis <ellis@rwest.io>
date: Sun, 22 Oct 2023 23:03:15 -0400
permissions: -rw-r--r--
description: add obj and net src
1 //! obj errors
2 use std::{fmt, io};
3 
4 /// obj Result wrapper
5 pub type Result<T> = std::result::Result<T, Error>;
6 
7 /// obj Error type
8 #[derive(Debug)]
9 pub enum Error {
10  Message(String),
11  Ron(ron::error::Error),
12  Json(serde_json::error::Error),
13  Io(io::Error),
14  Bincode(bincode::Error),
15  Utf8(std::string::FromUtf8Error),
16  #[cfg(feature = "hg")]
17  Hg(hg_parser::ErrorKind),
18  Parse(std::string::ParseError),
19  // TODO [2021-08-25 Wed 21:58] : Git()
20 }
21 
22 impl serde::ser::Error for Error {
23  fn custom<T: fmt::Display>(msg: T) -> Self {
24  Error::Message(msg.to_string())
25  }
26 }
27 
28 impl serde::de::Error for Error {
29  fn custom<T: fmt::Display>(msg: T) -> Self {
30  Error::Message(msg.to_string())
31  }
32 }
33 
34 impl fmt::Display for Error {
35  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36  match self {
37  Error::Message(msg) => f.write_str(msg),
38  Error::Io(ref err) => write!(f, "obj IO error: {}", err),
39  Error::Ron(ref err) => write!(f, "obj Ron error: {}", err),
40  Error::Json(ref err) => write!(f, "obj Json error: {}", err),
41  Error::Bincode(ref err) => write!(f, "obj Bincode error: {}", err),
42  Error::Utf8(ref err) => write!(f, "obj Utf8 error: {}", err),
43  #[cfg(feature = "hg")]
44  Error::Hg(ref err) => write!(f, "obj MercurialRepo error: {}", err),
45  Error::Parse(ref err) => write!(f, "obj Parse error: {}", err),
46  }
47  }
48 }
49 
50 impl From<io::Error> for Error {
51  fn from(e: io::Error) -> Self {
52  Error::Io(e)
53  }
54 }
55 
56 impl From<std::string::ParseError> for Error {
57  fn from(e: std::string::ParseError) -> Self {
58  Error::Parse(e)
59  }
60 }
61 
62 impl From<std::string::FromUtf8Error> for Error {
63  fn from(err: std::string::FromUtf8Error) -> Self {
64  Error::Utf8(err)
65  }
66 }
67 
68 impl From<ron::Error> for Error {
69  fn from(e: ron::Error) -> Self {
70  Error::Ron(e)
71  }
72 }
73 
74 impl From<serde_json::Error> for Error {
75  fn from(e: serde_json::Error) -> Self {
76  Error::Json(e)
77  }
78 }
79 
80 impl From<bincode::Error> for Error {
81  fn from(e: bincode::Error) -> Self {
82  Error::Bincode(e)
83  }
84 }
85 
86 #[cfg(feature = "hg")]
87 impl From<hg_parser::ErrorKind> for Error {
88  fn from(e: hg_parser::ErrorKind) -> Self {
89  Error::Hg(e)
90  }
91 }
92 
93 impl std::error::Error for Error {}