~ajk/bsr

cec64dca8089139f702d81b101514fb29718fc11 — Andrew Kay 3 years ago 2765a1b
Replace tag constants with enum
1 files changed, 15 insertions(+), 12 deletions(-)

M src/bitstream.rs
M src/bitstream.rs => src/bitstream.rs +15 -12
@@ 10,14 10,17 @@ pub struct Header {
    pub bitstream_length: u32
}

pub fn read_header<R: Read>(reader: &mut R) -> io::Result<Header> {
    // See http://www.fpga-faq.com/FAQ_Pages/0026_Tell_me_about_bit_files.htm.
    const DESIGN_NAME_TAG: u8 = 0x61;
    const PART_NAME_TAG: u8 = 0x62;
    const DESIGN_DATE_TAG: u8 = 0x63;
    const DESIGN_TIME_TAG: u8 = 0x64;
    const BITSTREAM_TAG: u8 = 0x65;
// See http://www.fpga-faq.com/FAQ_Pages/0026_Tell_me_about_bit_files.htm.
#[repr(u8)]
enum Tag {
    DesignName = 0x61,
    PartName = 0x62,
    DesignDate = 0x63,
    DesignTime = 0x64,
    Bitstream = 0x65
}

pub fn read_header<R: Read>(reader: &mut R) -> io::Result<Header> {
    // Skip the first two fields.
    let length = read_u16(reader).or_else(|_| invalid_data("read length failed"))? as usize;



@@ 34,7 37,7 @@ pub fn read_header<R: Read>(reader: &mut R) -> io::Result<Header> {
    // The design name.
    let tag = read_u8(reader).or_else(|_| invalid_data("error reading design name tag"))?;

    if tag != DESIGN_NAME_TAG {
    if tag != Tag::DesignName as u8 {
        return invalid_data("expected design name tag");
    }



@@ 43,7 46,7 @@ pub fn read_header<R: Read>(reader: &mut R) -> io::Result<Header> {
    // The part name.
    let tag = read_u8(reader).or_else(|_| invalid_data("error reading part name tag"))?;

    if tag != PART_NAME_TAG {
    if tag != Tag::PartName as u8 {
        return invalid_data("expected part name tag");
    }



@@ 52,7 55,7 @@ pub fn read_header<R: Read>(reader: &mut R) -> io::Result<Header> {
    // The design date.
    let tag = read_u8(reader).or_else(|_| invalid_data("error reading design date tag"))?;

    if tag != DESIGN_DATE_TAG {
    if tag != Tag::DesignDate as u8 {
        return invalid_data("expected design date tag");
    }



@@ 61,7 64,7 @@ pub fn read_header<R: Read>(reader: &mut R) -> io::Result<Header> {
    // The design time.
    let tag = read_u8(reader).or_else(|_| invalid_data("error reading design time tag"))?;

    if tag != DESIGN_TIME_TAG {
    if tag != Tag::DesignTime as u8 {
        return invalid_data("expected design time tag");
    }



@@ 70,7 73,7 @@ pub fn read_header<R: Read>(reader: &mut R) -> io::Result<Header> {
    // The bitstream length.
    let tag = read_u8(reader).or_else(|_| invalid_data("error reading bitstream tag"))?;

    if tag != BITSTREAM_TAG {
    if tag != Tag::Bitstream as u8 {
        return invalid_data("expected bitstream tag");
    }