use crate::{BinaryReader, Result};
use ::core::ops::Range;
#[derive(Clone)]
pub struct CustomSectionReader<'a> {
pub(crate) name: &'a str,
pub(crate) data_offset: usize,
pub(crate) data: &'a [u8],
pub(crate) range: Range<usize>,
}
impl<'a> CustomSectionReader<'a> {
pub fn new(data: &'a [u8], offset: usize) -> Result<CustomSectionReader<'a>> {
let mut reader = BinaryReader::new_with_offset(data, offset);
let name = reader.read_string()?;
let data_offset = reader.original_position();
let data = reader.remaining_buffer();
let range = reader.range();
Ok(CustomSectionReader {
name,
data_offset,
data,
range,
})
}
pub fn name(&self) -> &'a str {
self.name
}
pub fn data_offset(&self) -> usize {
self.data_offset
}
pub fn data(&self) -> &'a [u8] {
self.data
}
pub fn range(&self) -> Range<usize> {
self.range.clone()
}
}
impl<'a> ::core::fmt::Debug for CustomSectionReader<'a> {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.debug_struct("CustomSectionReader")
.field("name", &self.name)
.field("data_offset", &self.data_offset)
.field("data", &"...")
.field("range", &self.range)
.finish()
}
}