Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
Removed some unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
gnieto committed Jun 3, 2017
1 parent 558d6c1 commit 270ba1c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
51 changes: 24 additions & 27 deletions src/model/owned/table_type/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,27 @@ impl SimpleEntry {
self.value_data
}

pub fn to_vec(&self) -> Vec<u8> {
pub fn to_vec(&self) -> Result<Vec<u8>> {
let mut out = Vec::new();

// Header size
out.write_u16::<LittleEndian>(8).unwrap();
out.write_u16::<LittleEndian>(8)?;

// Flags => Simple entry
out.write_u16::<LittleEndian>(0).unwrap();
out.write_u16::<LittleEndian>(0)?;

// Key index
out.write_u32::<LittleEndian>(self.get_key() as u32)
.unwrap();
out.write_u32::<LittleEndian>(self.get_key() as u32)?;

// Value type
out.write_u16::<LittleEndian>(8).unwrap();
out.write_u8(0).unwrap();
out.write_u8(self.get_type()).unwrap();
out.write_u16::<LittleEndian>(8)?;
out.write_u8(0)?;
out.write_u8(self.get_type())?;

// Value
out.write_u32::<LittleEndian>(self.get_value() as u32)
.unwrap();
out.write_u32::<LittleEndian>(self.get_value() as u32)?;

out
Ok(out)
}
}

Expand Down Expand Up @@ -128,45 +126,44 @@ impl ComplexEntry {
&self.entries
}

pub fn to_vec(&self) -> Vec<u8> {
pub fn to_vec(&self) -> Result<Vec<u8>> {
let mut out = Vec::new();

// Header size
out.write_u16::<LittleEndian>(16).unwrap();
out.write_u16::<LittleEndian>(16)?;

// Flags => Complex entry
out.write_u16::<LittleEndian>(1).unwrap();
out.write_u16::<LittleEndian>(1)?;

// Key index
out.write_u32::<LittleEndian>(self.key_index).unwrap();
out.write_u32::<LittleEndian>(self.key_index)?;

// Parent entry
out.write_u32::<LittleEndian>(self.parent_entry_id).unwrap();
out.write_u32::<LittleEndian>(self.parent_entry_id)?;

// Children entry amount
let children_amount = self.entries.len() as u32;
if children_amount == 0 {
out.write_u32::<LittleEndian>(0xFFFFFFFF).unwrap();
out.write_u32::<LittleEndian>(0xFFFFFFFF)?;
} else {
out.write_u32::<LittleEndian>(self.entries.len() as u32)
.unwrap();
out.write_u32::<LittleEndian>(self.entries.len() as u32)?;
}

for e in &self.entries {
// TODO: Unify this with simple entry without header
// Key index
out.write_u32::<LittleEndian>(e.get_id()).unwrap();
out.write_u32::<LittleEndian>(e.get_id())?;

// Value type
out.write_u16::<LittleEndian>(8).unwrap();
out.write_u8(0).unwrap();
out.write_u8(e.get_type()).unwrap();
out.write_u16::<LittleEndian>(8)?;
out.write_u8(0)?;
out.write_u8(e.get_type())?;

// Value
out.write_u32::<LittleEndian>(e.get_value() as u32).unwrap();
out.write_u32::<LittleEndian>(e.get_value() as u32)?;
}

out
Ok(out)
}
}

Expand Down Expand Up @@ -215,11 +212,11 @@ impl Entry {
}
}

pub fn to_vec(&self) -> Vec<u8> {
pub fn to_vec(&self) -> Result<Vec<u8>> {
match *self {
Entry::Complex(ref complex) => complex.to_vec(),
Entry::Simple(ref simple) => simple.to_vec(),
Entry::Empty(_, _) => Vec::new(),
Entry::Empty(_, _) => Ok(Vec::new()),
}
}
}
2 changes: 1 addition & 1 deletion src/model/owned/table_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl OwnedBuf for TableTypeBuf {
let mut entries_body = Vec::new();

for e in &self.entries {
let current_entry = e.to_vec();
let current_entry = e.to_vec()?;

if e.is_empty() {
out.write_u32::<LittleEndian>(0xFFFFFFFF)?;
Expand Down

0 comments on commit 270ba1c

Please sign in to comment.