log_result.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // imag - the personal information management suite for the commandline
  3. // Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; version
  8. // 2.1 of the License.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. //
  19. /// This macro is used to generate extensions for the `Result<T, U>` type which only have
  20. /// sideeffects.
  21. ///
  22. /// This macro is then used to generate debug/info/log/warning/etc extensions.
  23. ///
  24. /// It is exported, so other crates can use it to generate more specific extensions for
  25. /// `Result<T, U>` types
  26. ///
  27. /// # Parameters
  28. ///
  29. /// The documentation for the parameters of the macro follow.
  30. ///
  31. /// ## `$name`
  32. ///
  33. /// name of the trait to generate
  34. ///
  35. /// ## `$map_name`
  36. ///
  37. /// Name of the function which is generated to call the closure with.
  38. ///
  39. /// This function gets `&T` from `Result<T, E>` and can now build the argument for
  40. /// `$closure`. So, this function can, for example, `|e| format!("Look here: {:?}", e)`, the
  41. /// result gets fed to `$closure`.
  42. ///
  43. /// ## `$map_str_name`
  44. ///
  45. /// Name of the function which is generated to call the closure with.
  46. ///
  47. /// This function gets simply a `&str` which gets fed to the `$closure` later.
  48. /// So it can be used to `foo().$map_str_name("Something happened")`
  49. ///
  50. /// ## `$map_err_name`
  51. ///
  52. /// Same as `$map_name`, but gets `&E` from `Resul<T, E>`.
  53. ///
  54. /// ## `$map_err_str_name`
  55. ///
  56. /// Same as `$map_str_name`, but is called for error cases in `Result<T, E>` (though no
  57. /// argument is passed.
  58. ///
  59. /// ## `$closure`
  60. ///
  61. /// The closure which should be called when mapping.
  62. ///
  63. /// This closure can now do things, but the return value of the closure is discarded.
  64. /// So, this closure can be used for its sideeffects (logging for example) only.
  65. ///
  66. /// An example would be: `|element| debug!("Element: {:?}", element)`.
  67. ///
  68. #[macro_export]
  69. macro_rules! generate_result_logging_extension {
  70. {
  71. $name: ident,
  72. $map_name: ident,
  73. $map_str_name: ident,
  74. $map_err_name: ident,
  75. $map_err_str_name: ident,
  76. $closure: expr
  77. } => {
  78. pub trait $name<T, E> : Sized {
  79. fn $map_name<F: FnOnce(&T) -> String>(self, f: F) -> Self;
  80. fn $map_str_name(self, s: &str) -> Self {
  81. self.$map_name(|_| format!("{}", s))
  82. }
  83. fn $map_err_name<F: FnOnce(&E) -> String>(self, f: F) -> Self;
  84. fn $map_err_str_name(self, s: &str) -> Self {
  85. self.$map_err_name(|_| format!("{}", s))
  86. }
  87. }
  88. impl<T, E> $name<T, E> for Result<T, E> {
  89. fn $map_name<F: FnOnce(&T) -> String>(self, f: F) -> Self {
  90. self.map(|x| { $closure(f(&x)); x })
  91. }
  92. fn $map_err_name<F: FnOnce(&E) -> String>(self, f: F) -> Self {
  93. self.map_err(|e| { $closure(f(&e)); e })
  94. }
  95. }
  96. }
  97. }