use crate::error::{FasError, Result};
use crate::fas::bytes;
use crate::fas::header::Header;
pub const SECTION_ENTRY_SIZE: usize = 4;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SectionName {
pub index: u32,
pub name_offset: u32,
pub name: String,
}
pub fn parse_sections(header: &Header, data: &[u8]) -> Result<Option<Vec<SectionName>>> {
let Some(raw) = header.section_names(data)? else {
return Ok(None);
};
if raw.len() % SECTION_ENTRY_SIZE != 0 {
return Err(FasError::Geometry("section names not a multiple of 4"));
}
raw.chunks_exact(SECTION_ENTRY_SIZE)
.enumerate()
.map(|(index, entry)| {
let name_offset = bytes::u32_at(entry, 0)?;
Ok(SectionName {
index: u32::try_from(index + 1)
.map_err(|_| FasError::Geometry("too many sections"))?,
name_offset,
name: header.string_at(data, name_offset)?.to_owned(),
})
})
.collect::<Result<Vec<_>>>()
.map(Some)
}