recursive_size.rs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-FileCopyrightText: 2024 Christina Sørensen
  2. // SPDX-License-Identifier: EUPL-1.2
  3. //
  4. // SPDX-FileCopyrightText: 2023-2024 Christina Sørensen, eza contributors
  5. // SPDX-FileCopyrightText: 2014 Benjamin Sago
  6. // SPDX-License-Identifier: MIT
  7. /// Used to represent a the size of a recursive directory traversal. `None`
  8. /// should be used when the file does not represent a directory or the recursive
  9. /// size should not be calculated.
  10. #[derive(Copy, Clone, Debug)]
  11. pub enum RecursiveSize {
  12. /// Size should not be computed
  13. None,
  14. /// Size should be computed but has not been computed yet
  15. Unknown,
  16. /// Size has been computed. First field is size in bytes and second field
  17. /// is size in blocks
  18. #[cfg_attr(target_family = "windows", allow(dead_code))]
  19. Some(u64, u64),
  20. }
  21. impl RecursiveSize {
  22. /// Returns `true` if `None`
  23. ///
  24. /// # Examples
  25. ///
  26. /// ```
  27. /// use eza::fs::recursive_size::RecursiveSize;
  28. ///
  29. /// let x = RecursiveSize::None;
  30. /// assert_eq!(x.is_none(), true);
  31. ///
  32. /// let x = RecursiveSize::Unknown;
  33. /// assert_eq!(x.is_none(), false);
  34. ///
  35. /// let x = RecursiveSize::Some(0, 0);
  36. /// assert_eq!(x.is_none(), false);
  37. /// ```
  38. #[inline]
  39. #[must_use] pub const fn is_none(&self) -> bool {
  40. matches!(*self, Self::None)
  41. }
  42. /// Returns the contained [`Some`] value or a provided default.
  43. ///
  44. /// # Examples
  45. ///
  46. /// ```
  47. /// use eza::fs::recursive_size::RecursiveSize;
  48. ///
  49. /// assert_eq!(RecursiveSize::None.unwrap_bytes_or(1), 1);
  50. /// assert_eq!(RecursiveSize::Unknown.unwrap_bytes_or(1), 1);
  51. /// assert_eq!(RecursiveSize::Some(2, 3).unwrap_bytes_or(1), 2);
  52. /// ```
  53. #[inline]
  54. #[must_use] pub const fn unwrap_bytes_or(self, default: u64) -> u64 {
  55. match self {
  56. Self::Some(bytes, _blocks) => bytes,
  57. _ => default,
  58. }
  59. }
  60. /// Returns the provided default result (if None or Unknown),
  61. /// or applies a function to the contained value (if Some).
  62. ///
  63. /// # Examples
  64. ///
  65. /// ```
  66. /// use eza::fs::recursive_size::RecursiveSize;
  67. ///
  68. /// assert_eq!(RecursiveSize::None.map_or(None, |s, _| Some(s * 2)), None);
  69. /// assert_eq!(RecursiveSize::Unknown.map_or(None, |s, _| Some(s * 2)), None);
  70. /// assert_eq!(RecursiveSize::Some(2, 3).map_or(None, |s, _| Some(s * 2)), Some(4));
  71. /// ```
  72. #[inline]
  73. #[cfg_attr(target_family = "windows", allow(dead_code))]
  74. pub fn map_or<U, F>(self, default: U, f: F) -> U
  75. where
  76. F: FnOnce(u64, u64) -> U,
  77. {
  78. match self {
  79. RecursiveSize::Some(bytes, blocks) => f(bytes, blocks),
  80. _ => default,
  81. }
  82. }
  83. }