Replace panic! with Option (#25)

This commit is contained in:
Nick Condron 2023-01-21 01:27:37 -05:00 committed by GitHub
parent ec062bf5ca
commit 6afc535fad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,12 +18,12 @@ use crate::obj::{
ObjSymbolFlagSet, ObjSymbolFlags, ObjSymbolFlagSet, ObjSymbolFlags,
}; };
fn to_obj_section_kind(kind: SectionKind) -> ObjSectionKind { fn to_obj_section_kind(kind: SectionKind) -> Option<ObjSectionKind> {
match kind { match kind {
SectionKind::Text => ObjSectionKind::Code, SectionKind::Text => Some(ObjSectionKind::Code),
SectionKind::Data | SectionKind::ReadOnlyData => ObjSectionKind::Data, SectionKind::Data | SectionKind::ReadOnlyData => Some(ObjSectionKind::Data),
SectionKind::UninitializedData => ObjSectionKind::Bss, SectionKind::UninitializedData => Some(ObjSectionKind::Bss),
_ => panic!("Unhandled section kind {kind:?}"), _ => None,
} }
} }
@ -74,18 +74,14 @@ fn filter_sections(obj_file: &File<'_>) -> Result<Vec<ObjSection>> {
if section.size() == 0 { if section.size() == 0 {
continue; continue;
} }
if section.kind() != SectionKind::Text let Some(kind) = to_obj_section_kind(section.kind()) else {
&& section.kind() != SectionKind::Data
&& section.kind() != SectionKind::ReadOnlyData
&& section.kind() != SectionKind::UninitializedData
{
continue; continue;
} };
let name = section.name().context("Failed to process section name")?; let name = section.name().context("Failed to process section name")?;
let data = section.uncompressed_data().context("Failed to read section data")?; let data = section.uncompressed_data().context("Failed to read section data")?;
result.push(ObjSection { result.push(ObjSection {
name: name.to_string(), name: name.to_string(),
kind: to_obj_section_kind(section.kind()), kind,
address: section.address(), address: section.address(),
size: section.size(), size: section.size(),
data: data.to_vec(), data: data.to_vec(),