Skip to main content

uhttp/
result.rs

1#[cfg(feature = "anyhow")]
2pub type Result<T> = anyhow::Result<T>;
3
4#[cfg(not(feature = "anyhow"))]
5pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Debug)]
8pub enum Error {
9  IO(std::io::Error),
10  Generic(String),
11  HTTP(http::Error),
12}
13
14impl Error {
15  #[cfg(not(feature = "anyhow"))]
16  pub fn generic(message: impl AsRef<str>) -> Self {
17    Self::Generic(message.as_ref().to_string())
18  }
19
20  #[cfg(feature = "anyhow")]
21  pub fn generic(message: impl AsRef<str>) -> anyhow::Error {
22    anyhow::anyhow!("{}", message.as_ref())
23  }
24
25  #[cfg(not(feature = "anyhow"))]
26  pub fn generic_err(message: impl AsRef<str>) -> Result<()> {
27    Err(Self::Generic(message.as_ref().to_string()))
28  }
29
30  #[cfg(feature = "anyhow")]
31  pub fn generic_err(message: impl AsRef<str>) -> Result<()> {
32    return Err(anyhow::anyhow!("{}", message.as_ref()));
33  }
34}
35
36impl std::fmt::Display for Error {
37  fn fmt(
38    &self,
39    f: &mut std::fmt::Formatter<'_>,
40  ) -> std::fmt::Result {
41    write!(f, "{:?}", self)
42  }
43}
44
45impl std::error::Error for Error {}
46
47impl From<std::io::Error> for Error {
48  fn from(value: std::io::Error) -> Self {
49    Error::IO(value)
50  }
51}
52
53impl From<http::Error> for Error {
54  fn from(value: http::Error) -> Self {
55    Error::HTTP(value)
56  }
57}