use anyhow::{anyhow, bail, Result};
use std::{ffi::OsStr, path::Path};
pub fn check_address_overflow(address: u16, length: u16) -> Result<()> {
if length > 0 && u16::checked_add(address, length - 1).is_none() {
bail!(
"Address {:#06x} + length {:#06x} overflows address space",
address,
length
)
} else {
Ok(())
}
}
pub fn get_extension<P: AsRef<Path>>(path: P) -> Option<String> {
path.as_ref()
.extension()
.and_then(OsStr::to_str)
.map(|s| s.to_lowercase())
}
pub fn extract_load_address(data: &[u8]) -> Result<u16> {
data.get(..2)
.ok_or_else(|| anyhow!("at least two bytes required to detect load address"))
.map(|b| b.try_into().unwrap()) .map(u16::from_le_bytes) }