Skip to main content

embedded_sdmmc/filesystem/
cluster.rs

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