#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
pub struct Attributes(pub(crate) u8);
impl Attributes {
pub const READ_ONLY: u8 = 0x01;
pub const HIDDEN: u8 = 0x02;
pub const SYSTEM: u8 = 0x04;
pub const VOLUME: u8 = 0x08;
pub const DIRECTORY: u8 = 0x10;
pub const ARCHIVE: u8 = 0x20;
pub const LFN: u8 = Self::READ_ONLY | Self::HIDDEN | Self::SYSTEM | Self::VOLUME;
pub(crate) fn create_from_fat(value: u8) -> Attributes {
Attributes(value)
}
pub(crate) fn set_archive(&mut self, flag: bool) {
let archive = if flag { 0x20 } else { 0x00 };
self.0 |= archive;
}
pub fn is_read_only(self) -> bool {
(self.0 & Self::READ_ONLY) == Self::READ_ONLY
}
pub fn is_hidden(self) -> bool {
(self.0 & Self::HIDDEN) == Self::HIDDEN
}
pub fn is_system(self) -> bool {
(self.0 & Self::SYSTEM) == Self::SYSTEM
}
pub fn is_volume(self) -> bool {
(self.0 & Self::VOLUME) == Self::VOLUME
}
pub fn is_directory(self) -> bool {
(self.0 & Self::DIRECTORY) == Self::DIRECTORY
}
pub fn is_archive(self) -> bool {
(self.0 & Self::ARCHIVE) == Self::ARCHIVE
}
pub fn is_lfn(self) -> bool {
(self.0 & Self::LFN) == Self::LFN
}
}
impl core::fmt::Debug for Attributes {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let mut output = heapless::String::<7>::new();
if self.is_lfn() {
output.push_str("LFN").unwrap();
} else {
if self.is_directory() {
output.push_str("D").unwrap();
} else {
output.push_str("F").unwrap();
}
if self.is_read_only() {
output.push_str("R").unwrap();
}
if self.is_hidden() {
output.push_str("H").unwrap();
}
if self.is_system() {
output.push_str("S").unwrap();
}
if self.is_volume() {
output.push_str("V").unwrap();
}
if self.is_archive() {
output.push_str("A").unwrap();
}
}
f.pad(&output)
}
}