use crate::error::Error; use std::{ convert::TryFrom, fmt::{Display, Formatter}, }; pub type SectionDataIterator<'data> = Box + 'data>; pub struct SectionInfo<'data> { pub size: u64, pub data: Option>, pub offset: u64, } pub enum Section<'data> { Text(SectionInfo<'data>), Data(SectionInfo<'data>), Bss(SectionInfo<'data>), } impl Section<'_> { pub fn size(&self) -> Result { match self { Section::Text(s) => Ok(s.size), Section::Data(s) => Ok(s.size), Section::Bss(s) => Ok(s.size), } } } impl<'data> TryFrom<(&str, SectionInfo<'data>)> for Section<'data> { type Error = Error; fn try_from(value: (&str, SectionInfo<'data>)) -> Result { if value.0.starts_with(".text") { Ok(Section::Text(value.1)) } else if value.0.starts_with(".rodata") || value.0.starts_with(".data") { Ok(Section::Data(value.1)) } else if value.0.starts_with(".bss") { Ok(Section::Bss(value.1)) } else { Err(Error::InvalidSectionName) } } } impl Display for SectionInfo<'_> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( f, "size: {}, data: {}, offset: {}", self.size, self.data.is_some(), self.offset, ) } } impl Display for Section<'_> { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Section::Text(s) => writeln!(f, "TEXT {}", s), Section::Data(s) => writeln!(f, "DATA {}", s), Section::Bss(s) => writeln!(f, "BSS {}", s), } } }