embedded_sdmmc/filesystem/
cluster.rs

1/// Identifies a cluster on disk.
2///
3/// A cluster is a consecutive group of blocks. Each cluster has a a numeric ID.
4/// Some numeric IDs are reserved for special purposes.
5#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
6#[derive(Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
7pub struct ClusterId(pub(crate) u32);
8
9impl ClusterId {
10    /// Magic value indicating an invalid cluster value.
11    pub const INVALID: ClusterId = ClusterId(0xFFFF_FFF6);
12    /// Magic value indicating a bad cluster.
13    pub const BAD: ClusterId = ClusterId(0xFFFF_FFF7);
14    /// Magic value indicating a empty cluster.
15    pub const EMPTY: ClusterId = ClusterId(0x0000_0000);
16    /// Magic value indicating the cluster holding the root directory (which
17    /// doesn't have a number in FAT16 as there's a reserved region).
18    pub const ROOT_DIR: ClusterId = ClusterId(0xFFFF_FFFC);
19    /// Magic value indicating that the cluster is allocated and is the final cluster for the file
20    pub const END_OF_FILE: ClusterId = ClusterId(0xFFFF_FFFF);
21}
22
23impl core::ops::Add<u32> for ClusterId {
24    type Output = ClusterId;
25    fn add(self, rhs: u32) -> ClusterId {
26        ClusterId(self.0 + rhs)
27    }
28}
29
30impl core::ops::AddAssign<u32> for ClusterId {
31    fn add_assign(&mut self, rhs: u32) {
32        self.0 += rhs;
33    }
34}
35
36impl core::fmt::Debug for ClusterId {
37    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38        write!(f, "ClusterId(")?;
39        match *self {
40            Self::INVALID => {
41                write!(f, "{:08}", "INVALID")?;
42            }
43            Self::BAD => {
44                write!(f, "{:08}", "BAD")?;
45            }
46            Self::EMPTY => {
47                write!(f, "{:08}", "EMPTY")?;
48            }
49            Self::ROOT_DIR => {
50                write!(f, "{:08}", "ROOT")?;
51            }
52            Self::END_OF_FILE => {
53                write!(f, "{:08}", "EOF")?;
54            }
55            ClusterId(value) => {
56                write!(f, "{:08x}", value)?;
57            }
58        }
59        write!(f, ")")?;
60        Ok(())
61    }
62}
63
64// ****************************************************************************
65//
66// End Of File
67//
68// ****************************************************************************