fn from_buf_descriptor(buf: &[u8]) -> Option <Sense> {
    if buf.len() < 8 {
      return None;
    }

    let response_code = buf[0] & 0x7F;
    if !(0x72..=0x73_u8).contains(&response_code) {
      return None;
    }

    let mut rv: Sense = Sense {
      deferred: response_code == 0x73,
      sense_key: SenseKey::from_u8(buf[1] & 0x0F),
      additional_sense_code: buf[2],
      additional_sense_code_qualifier: buf[3],
      ..Default::default()
    };

    let additional_sense_length = buf[7] as usize;
    let mut index: usize = 8;
    while let Some(dtype) = buf.get(index) {
      if index - 8 >= additional_sense_length {
        break;
      }

      // Matching keys are based on SPC-3 Table 15
      match dtype {
        0x00 => {
          if let Some(info) = buf.get(index..(index + 12)) {
            rv.information.copy_from_slice(&info[4..12]);
          }
          index += 12;
        },
        0x01 => {
          if let Some(cmd_info) = buf.get(index..(index + 12)) {
            rv.command_specific_information.copy_from_slice(&cmd_info[4..12]);
          }
          index += 12;
        },
        0x02 => {
          if let Some(skp) = buf.get(index..(index + 8)) {
            rv.sense_key_specific = SenseKeySpecific::from_buf(&rv.sense_key, &skp[4..7]);
          }
          index += 8;
        },
        0x03 => {
          if let Some(fru) = buf.get(index..(index + 4)) {
            rv.field_replaceable_unit_code = fru[3];
          }
          index += 4;
        },
        0x04 => {
          //TODO For now, just stop parsing the buffer
          break;
        },
        0x05 => {
          break;
        },
        0x06 => {
          break;
        },
        0x07 => {
          break;
        },
        0x08 => {
          break;
        },
        0x09 => {
          break;
        },
        0x80..=0xFF => {
          if let Some(vend) = buf.get(index..(index + 2)) {
            if let Some(spec) = buf.get((index + 2)..(index + 2 + (vend[1] as usize))) {
              rv.vendor_specific_sense_data = spec.to_vec();
            }
            index += vend[1] as usize;
          }
          index += 2;
        },
        _ => {
          //TODO If it's unknown, what do we do? Append it as additional_sense_bytes? Have an unknown vec?
          break;
        }
      }
    }

    Some(rv)
  }
/// Descriptor format described in SPC-3 Section 4.5.2
Descriptor format described in SPC-3 Section 4.5.2
Descriptor format described in SPC - 3 Section 4 . 5 . 2
fn from_buf_descriptor(buf: &[u8]) -> Option <Sense> {
    if buf.len() < 8 {
      return None;
    }
    let response_code = buf[0] & 0x7F;
    if !(0x72..=0x73_u8).contains(&response_code) {
      return None;
    }
    let mut rv: Sense = Sense {
      deferred: response_code == 0x73,
      sense_key: SenseKey::from_u8(buf[1] & 0x0F),
      additional_sense_code: buf[2],
      additional_sense_code_qualifier: buf[3],
      ..Default::default()
    };
    let additional_sense_length = buf[7] as usize;
    let mut index: usize = 8;
    while let Some(dtype) = buf.get(index) {
      if index - 8 >= additional_sense_length {
        break;
      }
      // Matching keys are based on SPC-3 Table 15
      match dtype {
        0x00 => {
          if let Some(info) = buf.get(index..(index + 12)) {
            rv.information.copy_from_slice(&info[4..12]);
          }
          index += 12;
        },
        0x01 => {
          if let Some(cmd_info) = buf.get(index..(index + 12)) {
            rv.command_specific_information.copy_from_slice(&cmd_info[4..12]);
          }
          index += 12;
        },
        0x02 => {
          if let Some(skp) = buf.get(index..(index + 8)) {
            rv.sense_key_specific = SenseKeySpecific::from_buf(&rv.sense_key, &skp[4..7]);
          }
          index += 8;
        },
        0x03 => {
          if let Some(fru) = buf.get(index..(index + 4)) {
            rv.field_replaceable_unit_code = fru[3];
          }
          index += 4;
        },
        0x04 => {
          //TODO For now, just stop parsing the buffer
          break;
        },
        0x05 => {
          break;
        },
        0x06 => {
          break;
        },
        0x07 => {
          break;
        },
        0x08 => {
          break;
        },
        0x09 => {
          break;
        },
        0x80..=0xFF => {
          if let Some(vend) = buf.get(index..(index + 2)) {
            if let Some(spec) = buf.get((index + 2)..(index + 2 + (vend[1] as usize))) {
              rv.vendor_specific_sense_data = spec.to_vec();
            }
            index += vend[1] as usize;
          }
          index += 2;
        },
        _ => {
          //TODO If it's unknown, what do we do? Append it as additional_sense_bytes? Have an unknown vec?
          break;
        }
      }
    }
    Some(rv)
  }
fn from_buf_descriptor ( buf : & [ u8 ] ) -> Option < Sense > { if buf . len ( ) < 8 { return None ; } let response_code = buf [ 0 ] & 0x7F ; if ! ( 0x72 ..= 0x73_u8 ) . contains ( & response_code ) { return None ; } let mut rv : Sense = Sense { deferred : response_code == 0x73 , sense_key : SenseKey :: from_u8 ( buf [ 1 ] & 0x0F ) , additional_sense_code : buf [ 2 ] , additional_sense_code_qualifier : buf [ 3 ] , .. Default :: default ( ) } ; let additional_sense_length = buf [ 7 ] as usize ; let mut index : usize = 8 ; while let Some ( dtype ) = buf . get ( index ) { if index - 8 >= additional_sense_length { break ; } // Matching keys are based on SPC-3 Table 15 match dtype { 0x00 => { if let Some ( info ) = buf . get ( index .. ( index + 12 ) ) { rv . information . copy_from_slice ( & info [ 4 .. 12 ] ) ; } index += 12 ; } , 0x01 => { if let Some ( cmd_info ) = buf . get ( index .. ( index + 12 ) ) { rv . command_specific_information . copy_from_slice ( & cmd_info [ 4 .. 12 ] ) ; } index += 12 ; } , 0x02 => { if let Some ( skp ) = buf . get ( index .. ( index + 8 ) ) { rv . sense_key_specific = SenseKeySpecific :: from_buf ( & rv . sense_key , & skp [ 4 .. 7 ] ) ; } index += 8 ; } , 0x03 => { if let Some ( fru ) = buf . get ( index .. ( index + 4 ) ) { rv . field_replaceable_unit_code = fru [ 3 ] ; } index += 4 ; } , 0x04 => { //TODO For now, just stop parsing the buffer break ; } , 0x05 => { break ; } , 0x06 => { break ; } , 0x07 => { break ; } , 0x08 => { break ; } , 0x09 => { break ; } , 0x80 ..= 0xFF => { if let Some ( vend ) = buf . get ( index .. ( index + 2 ) ) { if let Some ( spec ) = buf . get ( ( index + 2 ) .. ( index + 2 + ( vend [ 1 ] as usize ) ) ) { rv . vendor_specific_sense_data = spec . to_vec ( ) ; } index += vend [ 1 ] as usize ; } index += 2 ; } , _ => { //TODO If it's unknown, what do we do? Append it as additional_sense_bytes? Have an unknown vec? break ; } } } Some ( rv ) }
Descriptor format described in SPC-3 Section 4.5.2
Descriptor format described in SPC - 3 Section 4 . 5 . 2
buf
&[u8]