Trait bpx::Interface [−][src]
pub trait Interface {
fn find_section_by_type(&self, btype: u8) -> Option<SectionHandle>;
fn find_all_sections_of_type(&self, btype: u8) -> Vec<SectionHandle>;
fn find_section_by_index(&self, index: u32) -> Option<SectionHandle>;
fn get_section_header(&self, handle: SectionHandle) -> &SectionHeader;
fn get_section_index(&self, handle: SectionHandle) -> u32;
fn open_section(
&mut self,
handle: SectionHandle
) -> Result<&mut dyn SectionData>;
fn get_main_header(&self) -> &MainHeader;
}
Expand description
The interface implemented by both the BPX encoder and decoder.
Required methods
fn find_section_by_type(&self, btype: u8) -> Option<SectionHandle>
fn find_section_by_type(&self, btype: u8) -> Option<SectionHandle>
fn find_all_sections_of_type(&self, btype: u8) -> Vec<SectionHandle>
fn find_all_sections_of_type(&self, btype: u8) -> Vec<SectionHandle>
Searches for all sections of a given type. Returns None if no section could be found.
Arguments
btype
: section type byte.
returns: Vec<SectionHandle, Global>
Examples
use bpx::encoder::Encoder;
use bpx::Interface;
let file = Encoder::new(Vec::<u8>::new()).unwrap();
assert_eq!(file.find_all_sections_of_type(0).len(), 0);
fn find_section_by_index(&self, index: u32) -> Option<SectionHandle>
fn find_section_by_index(&self, index: u32) -> Option<SectionHandle>
Locates a section by its index in the file. Returns None if the section does not exist.
Arguments
index
: the section index to search for.
returns: Option
Examples
use bpx::encoder::Encoder;
use bpx::Interface;
let file = Encoder::new(Vec::<u8>::new()).unwrap();
assert!(file.find_section_by_index(0).is_none());
fn get_section_header(&self, handle: SectionHandle) -> &SectionHeader
fn get_section_header(&self, handle: SectionHandle) -> &SectionHeader
Returns the BPX section header of a section.
Arguments
handle
: a handle to the section.
returns: &SectionHeader
Panics
Panics if the given section handle is invalid.
Examples
use bpx::encoder::Encoder;
use bpx::Interface;
use bpx::builder::SectionHeaderBuilder;
let mut file = Encoder::new(Vec::<u8>::new()).unwrap();
let handle = file.create_section(SectionHeaderBuilder::new().with_type(1).build()).unwrap();
let header = file.get_section_header(handle);
assert_eq!(header.btype, 1);
fn get_section_index(&self, handle: SectionHandle) -> u32
fn get_section_index(&self, handle: SectionHandle) -> u32
Returns the section index from a section handle.
Arguments
handle
: a handle to the section.
returns: u32
Panics
Panics if the given section handle is invalid.
Examples
use bpx::encoder::Encoder;
use bpx::Interface;
use bpx::builder::SectionHeaderBuilder;
let mut file = Encoder::new(Vec::<u8>::new()).unwrap();
let handle = file.create_section(SectionHeaderBuilder::new().build()).unwrap();
assert_eq!(file.get_section_index(handle), 0);
fn open_section(
&mut self,
handle: SectionHandle
) -> Result<&mut dyn SectionData>
fn open_section(
&mut self,
handle: SectionHandle
) -> Result<&mut dyn SectionData>
Opens a section for read and/or write.
Arguments
handle
: a handle to the section.
returns: Result<&mut dyn SectionData, Error>
Errors
A BPX Error if an IO or any other file error occurs while reading the section from the file.
Panics
Panics if the given section handle is invalid.
Examples
use bpx::encoder::Encoder;
use bpx::Interface;
use bpx::builder::SectionHeaderBuilder;
let mut file = Encoder::new(Vec::<u8>::new()).unwrap();
let handle = file.create_section(SectionHeaderBuilder::new().build()).unwrap();
let section = file.open_section(handle).unwrap();
let data = section.load_in_memory().unwrap();
assert_eq!(data.len(), 0);
fn get_main_header(&self) -> &MainHeader
fn get_main_header(&self) -> &MainHeader
Returns a read-only reference to the BPX main header.
Examples
use bpx::encoder::Encoder;
use bpx::Interface;
use bpx::builder::SectionHeaderBuilder;
let mut file = Encoder::new(Vec::<u8>::new()).unwrap();
let header = file.get_main_header();
//Default BPX variant/type is 'P'
assert_eq!(header.btype, 'P' as u8);