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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! Helper structures for runtime packing visualization.


use internal_prelude::v1::*;

#[cfg(any(feature="alloc", feature="std"))]
pub trait PackedStructDebug {
    fn fmt_fields(&self, fmt: &mut Formatter) -> Result<(), FmtError>;
    fn packed_struct_display_header() -> &'static str;
}

pub struct DebugBinaryByteSlice<'a> {
    pub bits: &'a Range<usize>,
    pub slice: &'a [u8]
}

impl<'a> fmt::Binary for DebugBinaryByteSlice<'a> {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        for i in self.bits.start..(self.bits.end + 1) {
            let byte = i / 8;
            let bit = i % 8;
            let bit = 7 - bit;

            let src_byte = self.slice[byte];
            let src_bit = (src_byte & (1 << bit)) == (1 << bit);

            let s = if src_bit { "1" } else { "0" };
            try!(fmt.write_str(s));
        }

        Ok(())
    }
}

pub struct DebugBitField<'a> { 
	pub name: Cow<'a, str>,
	pub bits: Range<usize>,
	pub display_value: Cow<'a, str>
}


pub fn packable_fmt_fields(f: &mut Formatter, packed_bytes: &[u8], fields: &[DebugBitField]) -> fmt::Result {
    if fields.len() == 0 {
		return Ok(());
	}

    let max_field_length_name = fields.iter().map(|x| x.name.len()).max().unwrap();
	let max_bit_width = fields.iter().map(|x| x.bits.len()).max().unwrap();

    if max_bit_width > 32 {
        for field in fields {
            try!(write!(f, "{name:>0$} | {base_value:?}\r\n",
                            max_field_length_name + 1,
                            base_value = field.display_value,
                            name = field.name
                            ));
        }
    } else {    
        for field in fields {

            let debug_binary = DebugBinaryByteSlice {
                bits: &field.bits,
                slice: packed_bytes
            };

            try!(write!(f, "{name:>0$} | bits {bits_start:>3}:{bits_end:<3} | 0b{binary_value:>0width_bits$b}{dummy:>0spaces$} | {base_value:?}\r\n",
                            max_field_length_name + 1,
                            base_value = field.display_value,
                            binary_value = debug_binary,
                            dummy = "",
                            bits_start = field.bits.start,
                            bits_end = field.bits.end,
                            width_bits = field.bits.len(),
                            spaces = (max_bit_width - field.bits.len()) as usize,
                            name = field.name
                            ));
        }
    }

    Ok(())
}

pub struct PackedStructDisplay<'a, P: 'a, B: 'a> {
    pub packed_struct: &'a P,
    pub packed_struct_packed: PhantomData<B>,
    pub header: bool,
    pub raw_decimal: bool,
    pub raw_hex: bool,
    pub raw_binary: bool,
    pub fields: bool
}

impl<'a, P, B> PackedStructDisplay<'a, P, B> {
    pub fn new(packed_struct: &'a P) -> Self {
        PackedStructDisplay {
            packed_struct: packed_struct,
            packed_struct_packed: Default::default(),
            
            header: true,
            raw_decimal: true,
            raw_hex: true,
            raw_binary: true,
            fields: true
        }
    }
}

use packing::{PackedStruct, PackedStructSlice};

impl<'a, P, B> fmt::Display for PackedStructDisplay<'a, P, B> where P: PackedStruct<B> + PackedStructSlice + PackedStructDebug {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self.packed_struct.pack_to_vec() {
            Ok(packed) => {
                let l = packed.len();

                if self.header {
                    try!(f.write_str(P::packed_struct_display_header()));
                    try!(f.write_str("\r\n"));
                    try!(f.write_str("\r\n"));
                }

                // decimal

                if self.raw_decimal {
                    try!(f.write_str("Decimal\r\n"));
                    try!(f.write_str("["));
                    for i in 0..l {
                        try!(write!(f, "{}", packed[i]));
                        if (i + 1) != l {
                            try!(f.write_str(", "));
                        }
                    }
                    try!(f.write_str("]"));

                    try!(f.write_str("\r\n"));
                    try!(f.write_str("\r\n"));
                }
                                
                // hex

                if self.raw_hex {
                    try!(f.write_str("Hex\r\n"));
                    try!(f.write_str("["));
                    for i in 0..l {
                        try!(write!(f, "0x{:X}", packed[i]));
                        if (i + 1) != l {
                            try!(f.write_str(", "));
                        }
                    }
                    try!(f.write_str("]"));
                    try!(f.write_str("\r\n"));
                    try!(f.write_str("\r\n"));
                }

                if self.raw_binary {
                    try!(f.write_str("Binary\r\n"));
                    try!(f.write_str("["));
                    for i in 0..l {
                        try!(write!(f, "0b{:08b}", packed[i]));
                        if (i + 1) != l {
                            try!(f.write_str(", "));
                        }
                    }
                    try!(f.write_str("]"));
                    try!(f.write_str("\r\n"));
                    try!(f.write_str("\r\n"));
                }

                if self.fields {
                    try!(self.packed_struct.fmt_fields(f));
                }                
            },
            Err(e) => {
                write!(f, "Error packing for display: {:?}", e)?;
            }
        }

        Ok(())
    }
}