embedded_fat/
structure.rs

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
//! Useful macros for parsing SD/MMC structures.

macro_rules! define_field {
    ($name:ident, bool, $offset:expr, $bit:expr) => {
        /// Get the value from the $name field
        pub fn $name(&self) -> bool {
            access_field!(self, $offset, $bit, 1)
        }
    };
    ($name:ident, u8, $offset:expr, $start_bit:expr, $num_bits:expr) => {
        /// Get the value from the $name field
        pub fn $name(&self) -> u8 {
            access_field!(self, $offset, $start_bit, $num_bits)
        }
    };
    ($name:ident, $type:ty, [ $( ( $offset:expr, $start_bit:expr, $num_bits:expr ) ),+ ]) => {
        /// Gets the value from the $name field
        pub fn $name(&self) -> $type {
            let mut result = 0;
            $(
                    result <<= $num_bits;
                    let part = access_field!(self, $offset, $start_bit, $num_bits) as $type;
                    result |=  part;
            )+
            result
        }
    };

    ($name:ident, u8, $offset:expr) => {
        /// Get the value from the $name field
        pub fn $name(&self) -> u8 {
            self.data[$offset]
        }
    };

    ($name:ident, u16, $offset:expr) => {
        /// Get the value from the $name field
        pub fn $name(&self) -> u16 {
            LittleEndian::read_u16(&self.data[$offset..$offset+2])
        }
    };

    ($name:ident, u32, $offset:expr) => {
        /// Get the $name field
        pub fn $name(&self) -> u32 {
            LittleEndian::read_u32(&self.data[$offset..$offset+4])
        }
    };
}