1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//---------------------------------------------------------------------------------------------------- Use

use std::path::PathBuf;

use serde::{Serialize,Deserialize};


// TODO:
// Fix import resolution errors.
//#[cfg(feature = "bincode2")]
//use bincode2::{Encode,Decode};

//---------------------------------------------------------------------------------------------------- Metadata
//#[cfg_attr(feature = "bincode2", derive(::bincode2::Encode, ::bincode2::Decode))]
#[derive(Clone,Hash,Debug,Serialize,Deserialize,PartialEq,Eq,PartialOrd,Ord)]
/// Metadata collected about a file/directory.
///
/// This stores:
/// - [`u64`]: the amount of bytes (saved|removed) (to|from) disk.
/// - [`PathBuf`]: the PATH where the (file|directory) (is|was) (saved|removed).
///
/// ## Display
/// This implements a more human readable [`Display`].
///
/// `format!("{metadata}")` or `metadata.to_string()` looks like this:
/// ```txt
/// 12336 bytes @ /the/path/to/your/file
/// ```
pub struct Metadata {
	size: u64,
	path: PathBuf,
}

impl Metadata {
	/// Create a new [`Metadata`].
	pub(crate) const fn new(size: u64, path: PathBuf) -> Self {
		Self { size, path }
	}

	/// Create a new `0` byte size [`Metadata`].
	pub(crate) const fn zero(path: PathBuf) -> Self {
		Self { size: 0, path }
	}

	/// Returns the amount of bytes removed/saved to disk.
	pub const fn size(&self) -> u64 {
		self.size
	}

	/// Returns the [`PathBuf`] of the file/directory.
	pub fn path(self) -> PathBuf {
		self.path
	}

	/// Clone and returns the inner parts.
	pub fn to_parts(&self) -> (u64, PathBuf) {
		(self.size, self.path.clone())
	}

	/// Consume [`Metadata`] and returns the inner parts.
	pub fn into_parts(self) -> (u64, PathBuf) {
		(self.size, self.path)
	}
}


//---------------------------------------------------------------------------------------------------- Display
impl std::fmt::Display for Metadata {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		#[cfg(feature = "bytesize")]
		{
			write!(f, "{} @ {}", bytesize::ByteSize::b(self.size), self.path.display())
		}
		#[cfg(not(feature = "bytesize"))]
		{
			write!(f, "{} @ {}", self.size, self.path.display())
		}
	}
}

//---------------------------------------------------------------------------------------------------- TESTS
//#[cfg(test)]
//mod tests {
//}