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.

123 lines
3.3KB

  1. use std::convert::Into;
  2. use std::error::Error as StdError;
  3. use std::fmt;
  4. #[derive(Debug)]
  5. pub enum ErrorKind {
  6. Msg(String),
  7. Tera(tera::Error),
  8. Io(::std::io::Error),
  9. Toml(toml::de::Error),
  10. Image(image::ImageError),
  11. Syntect(syntect::LoadingError),
  12. }
  13. /// The Error type
  14. #[derive(Debug)]
  15. pub struct Error {
  16. /// Kind of error
  17. pub kind: ErrorKind,
  18. pub source: Option<Box<dyn StdError>>,
  19. }
  20. unsafe impl Sync for Error {}
  21. unsafe impl Send for Error {}
  22. impl StdError for Error {
  23. fn source(&self) -> Option<&(dyn StdError + 'static)> {
  24. let mut source = self.source.as_ref().map(|c| &**c);
  25. if source.is_none() {
  26. if let ErrorKind::Tera(ref err) = self.kind {
  27. source = err.source();
  28. }
  29. }
  30. source
  31. }
  32. }
  33. impl fmt::Display for Error {
  34. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  35. match self.kind {
  36. ErrorKind::Msg(ref message) => write!(f, "{}", message),
  37. ErrorKind::Tera(ref e) => write!(f, "{}", e),
  38. ErrorKind::Io(ref e) => write!(f, "{}", e),
  39. ErrorKind::Toml(ref e) => write!(f, "{}", e),
  40. ErrorKind::Image(ref e) => write!(f, "{}", e),
  41. ErrorKind::Syntect(ref e) => write!(f, "{}", e),
  42. }
  43. }
  44. }
  45. impl Error {
  46. /// Creates generic error
  47. pub fn msg(value: impl ToString) -> Self {
  48. Self { kind: ErrorKind::Msg(value.to_string()), source: None }
  49. }
  50. /// Creates generic error with a cause
  51. pub fn chain(value: impl ToString, source: impl Into<Box<dyn StdError>>) -> Self {
  52. Self { kind: ErrorKind::Msg(value.to_string()), source: Some(source.into()) }
  53. }
  54. /// Create an error from a list of path collisions, formatting the output
  55. pub fn from_collisions(collisions: Vec<(&str, Vec<String>)>) -> Self {
  56. let mut msg = String::from("Found path collisions:\n");
  57. for (path, filepaths) in collisions {
  58. let row = format!("- `{}` from files {:?}\n", path, filepaths);
  59. msg.push_str(&row);
  60. }
  61. Self { kind: ErrorKind::Msg(msg), source: None }
  62. }
  63. }
  64. impl From<&str> for Error {
  65. fn from(e: &str) -> Self {
  66. Self::msg(e)
  67. }
  68. }
  69. impl From<String> for Error {
  70. fn from(e: String) -> Self {
  71. Self::msg(e)
  72. }
  73. }
  74. impl From<toml::de::Error> for Error {
  75. fn from(e: toml::de::Error) -> Self {
  76. Self { kind: ErrorKind::Toml(e), source: None }
  77. }
  78. }
  79. impl From<syntect::LoadingError> for Error {
  80. fn from(e: syntect::LoadingError) -> Self {
  81. Self { kind: ErrorKind::Syntect(e), source: None }
  82. }
  83. }
  84. impl From<tera::Error> for Error {
  85. fn from(e: tera::Error) -> Self {
  86. Self { kind: ErrorKind::Tera(e), source: None }
  87. }
  88. }
  89. impl From<::std::io::Error> for Error {
  90. fn from(e: ::std::io::Error) -> Self {
  91. Self { kind: ErrorKind::Io(e), source: None }
  92. }
  93. }
  94. impl From<image::ImageError> for Error {
  95. fn from(e: image::ImageError) -> Self {
  96. Self { kind: ErrorKind::Image(e), source: None }
  97. }
  98. }
  99. /// Convenient wrapper around std::Result.
  100. pub type Result<T> = ::std::result::Result<T, Error>;
  101. // So we can use bail! in all other crates
  102. #[macro_export]
  103. macro_rules! bail {
  104. ($e:expr) => {
  105. return Err($e.into());
  106. };
  107. ($fmt:expr, $($arg:tt)+) => {
  108. return Err(format!($fmt, $($arg)+).into());
  109. };
  110. }