embedded_fat/
structure.rs

1//! Useful macros for parsing SD/MMC structures.
2
3macro_rules! define_field {
4    ($name:ident, bool, $offset:expr, $bit:expr) => {
5        /// Get the value from the $name field
6        pub fn $name(&self) -> bool {
7            access_field!(self, $offset, $bit, 1)
8        }
9    };
10    ($name:ident, u8, $offset:expr, $start_bit:expr, $num_bits:expr) => {
11        /// Get the value from the $name field
12        pub fn $name(&self) -> u8 {
13            access_field!(self, $offset, $start_bit, $num_bits)
14        }
15    };
16    ($name:ident, $type:ty, [ $( ( $offset:expr, $start_bit:expr, $num_bits:expr ) ),+ ]) => {
17        /// Gets the value from the $name field
18        pub fn $name(&self) -> $type {
19            let mut result = 0;
20            $(
21                    result <<= $num_bits;
22                    let part = access_field!(self, $offset, $start_bit, $num_bits) as $type;
23                    result |=  part;
24            )+
25            result
26        }
27    };
28
29    ($name:ident, u8, $offset:expr) => {
30        /// Get the value from the $name field
31        pub fn $name(&self) -> u8 {
32            self.data[$offset]
33        }
34    };
35
36    ($name:ident, u16, $offset:expr) => {
37        /// Get the value from the $name field
38        pub fn $name(&self) -> u16 {
39            LittleEndian::read_u16(&self.data[$offset..$offset+2])
40        }
41    };
42
43    ($name:ident, u32, $offset:expr) => {
44        /// Get the $name field
45        pub fn $name(&self) -> u32 {
46            LittleEndian::read_u32(&self.data[$offset..$offset+4])
47        }
48    };
49}