You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.2 KiB

  1. use std::fmt;
  2. #[derive(Clone, Debug)]
  3. pub struct Error {
  4. pub message: String,
  5. }
  6. impl Error {
  7. pub fn prefix(&self, prefix: &str) -> Self {
  8. Error {
  9. message: format!("{}: {}", prefix, &self.message),
  10. }
  11. }
  12. }
  13. impl fmt::Display for Error {
  14. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  15. write!(f, "{}", self.message)
  16. }
  17. }
  18. impl From<&str> for Error {
  19. fn from(error: &str) -> Self {
  20. Error {
  21. message: error.to_string(),
  22. }
  23. }
  24. }
  25. impl From<String> for Error {
  26. fn from(error: String) -> Self {
  27. error.as_str().into()
  28. }
  29. }
  30. impl From<&String> for Error {
  31. fn from(error: &String) -> Self {
  32. error.as_str().into()
  33. }
  34. }
  35. impl From<std::io::Error> for Error {
  36. fn from(error: std::io::Error) -> Self {
  37. format!("IO error: {}", error).into()
  38. }
  39. }
  40. impl From<std::string::FromUtf8Error> for Error {
  41. fn from(error: std::string::FromUtf8Error) -> Self {
  42. format!("UTF-8 error: {}", error).into()
  43. }
  44. }
  45. impl From<std::sync::mpsc::RecvError> for Error {
  46. fn from(error: std::sync::mpsc::RecvError) -> Self {
  47. format!("MSPC receiver error: {}", error).into()
  48. }
  49. }
  50. impl From<syslog::Error> for Error {
  51. fn from(error: syslog::Error) -> Self {
  52. format!("syslog error: {}", error).into()
  53. }
  54. }
  55. impl From<toml::de::Error> for Error {
  56. fn from(error: toml::de::Error) -> Self {
  57. format!("IO error: {}", error).into()
  58. }
  59. }
  60. impl From<serde_json::error::Error> for Error {
  61. fn from(error: serde_json::error::Error) -> Self {
  62. format!("IO error: {}", error).into()
  63. }
  64. }
  65. impl From<handlebars::TemplateRenderError> for Error {
  66. fn from(error: handlebars::TemplateRenderError) -> Self {
  67. format!("Template error: {}", error).into()
  68. }
  69. }
  70. impl From<openssl::error::ErrorStack> for Error {
  71. fn from(error: openssl::error::ErrorStack) -> Self {
  72. format!("{}", error).into()
  73. }
  74. }
  75. impl From<http_req::error::Error> for Error {
  76. fn from(error: http_req::error::Error) -> Self {
  77. format!("HTTP error: {}", error).into()
  78. }
  79. }
  80. #[cfg(unix)]
  81. impl From<nix::Error> for Error {
  82. fn from(error: nix::Error) -> Self {
  83. format!("{}", error).into()
  84. }
  85. }