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.

128 lines
3.4KB

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