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.

lib.rs 2.9KB

5 years ago
Fix clippy warnings (#744) Clippy is returning some warnings. Let's fix or explicitly ignore them. In particular: - In `components/imageproc/src/lib.rs`, we implement `Hash` explicitly but derive `PartialEq`. We need to maintain the property that two keys being equal implies the hashes of those two keys are equal. Our `Hash` implementations preserve this, so we'll explicitly ignore the warnings. - In `components/site/src/lib.rs`, we were calling `.into()` on some values that are already of the correct type. - In `components/site/src/lib.rs`, we were using `.map(|x| *x)` in iterator chains to remove a level of indirection; we can instead say `.copied()` (introduced in Rust v1.36) or `.cloned()`. Using `.copied` here is better from a type-checking point of view, but we'll use `.cloned` for now as Rust v1.36 was only recently released. - In `components/templates/src/filters.rs` and `components/utils/src/site.rs`, we were taking `HashMap`s as function arguments but not generically accepting alternate `Hasher` implementations. - In `src/cmd/check.rs`, we use `env::current_dir()` as a default value, but our use of `unwrap_or` meant that we would always retrieve the current directory even when not needed. - In `components/errors/src/lib.rs`, we can use `if let` rather than `match`. - In `components/library/src/content/page.rs`, we can collapse a nested conditional into `else if let ...`. - In `components/library/src/sorting.rs`, a function takes `&&Page` arguments. Clippy warns about this for efficiency reasons, but we're doing it here to match a particular sorting API, so we'll explicitly ignore the warning.
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }
  59. impl From<&str> for Error {
  60. fn from(e: &str) -> Self {
  61. Self::msg(e)
  62. }
  63. }
  64. impl From<String> for Error {
  65. fn from(e: String) -> Self {
  66. Self::msg(e)
  67. }
  68. }
  69. impl From<toml::de::Error> for Error {
  70. fn from(e: toml::de::Error) -> Self {
  71. Self { kind: ErrorKind::Toml(e), source: None }
  72. }
  73. }
  74. impl From<syntect::LoadingError> for Error {
  75. fn from(e: syntect::LoadingError) -> Self {
  76. Self { kind: ErrorKind::Syntect(e), source: None }
  77. }
  78. }
  79. impl From<tera::Error> for Error {
  80. fn from(e: tera::Error) -> Self {
  81. Self { kind: ErrorKind::Tera(e), source: None }
  82. }
  83. }
  84. impl From<::std::io::Error> for Error {
  85. fn from(e: ::std::io::Error) -> Self {
  86. Self { kind: ErrorKind::Io(e), source: None }
  87. }
  88. }
  89. impl From<image::ImageError> for Error {
  90. fn from(e: image::ImageError) -> Self {
  91. Self { kind: ErrorKind::Image(e), source: None }
  92. }
  93. }
  94. /// Convenient wrapper around std::Result.
  95. pub type Result<T> = ::std::result::Result<T, Error>;
  96. // So we can use bail! in all other crates
  97. #[macro_export]
  98. macro_rules! bail {
  99. ($e:expr) => {
  100. return Err($e.into());
  101. };
  102. ($fmt:expr, $($arg:tt)+) => {
  103. return Err(format!($fmt, $($arg)+).into());
  104. };
  105. }