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.

133 lines
2.7 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::net::AddrParseError> for Error {
  41. fn from(error: std::net::AddrParseError) -> Self {
  42. format!("{error}").into()
  43. }
  44. }
  45. impl From<std::string::FromUtf8Error> for Error {
  46. fn from(error: std::string::FromUtf8Error) -> Self {
  47. format!("UTF-8 error: {error}").into()
  48. }
  49. }
  50. impl From<std::sync::mpsc::RecvError> for Error {
  51. fn from(error: std::sync::mpsc::RecvError) -> Self {
  52. format!("MSPC receiver error: {error}").into()
  53. }
  54. }
  55. impl From<std::time::SystemTimeError> for Error {
  56. fn from(error: std::time::SystemTimeError) -> Self {
  57. format!("SystemTimeError difference: {:?}", error.duration()).into()
  58. }
  59. }
  60. impl From<base64::DecodeError> for Error {
  61. fn from(error: base64::DecodeError) -> Self {
  62. format!("base 64 decode error: {error}").into()
  63. }
  64. }
  65. impl From<syslog::Error> for Error {
  66. fn from(error: syslog::Error) -> Self {
  67. format!("syslog error: {error}").into()
  68. }
  69. }
  70. impl From<toml::de::Error> for Error {
  71. fn from(error: toml::de::Error) -> Self {
  72. format!("IO error: {error}").into()
  73. }
  74. }
  75. impl From<serde_json::error::Error> for Error {
  76. fn from(error: serde_json::error::Error) -> Self {
  77. format!("IO error: {error}").into()
  78. }
  79. }
  80. impl From<attohttpc::Error> for Error {
  81. fn from(error: attohttpc::Error) -> Self {
  82. format!("HTTP error: {error}").into()
  83. }
  84. }
  85. impl From<glob::PatternError> for Error {
  86. fn from(error: glob::PatternError) -> Self {
  87. format!("pattern error: {error}").into()
  88. }
  89. }
  90. impl From<tinytemplate::error::Error> for Error {
  91. fn from(error: tinytemplate::error::Error) -> Self {
  92. format!("template error: {error}").into()
  93. }
  94. }
  95. #[cfg(feature = "crypto_openssl")]
  96. impl From<native_tls::Error> for Error {
  97. fn from(error: native_tls::Error) -> Self {
  98. format!("{error}").into()
  99. }
  100. }
  101. #[cfg(feature = "crypto_openssl")]
  102. impl From<openssl::error::ErrorStack> for Error {
  103. fn from(error: openssl::error::ErrorStack) -> Self {
  104. format!("{error}").into()
  105. }
  106. }
  107. #[cfg(unix)]
  108. impl From<nix::Error> for Error {
  109. fn from(error: nix::Error) -> Self {
  110. format!("{error}").into()
  111. }
  112. }