use crate::header::{
MainHeader,
SectionHeader,
FLAG_CHECK_CRC32,
FLAG_CHECK_WEAK,
FLAG_COMPRESS_XZ,
FLAG_COMPRESS_ZLIB
};
const COMPRESSION_THRESHOLD: u32 = 65536;
pub enum CompressionMethod
{
Xz,
Zlib
}
pub enum Checksum
{
Weak,
Crc32
}
pub struct SectionHeaderBuilder
{
header: SectionHeader
}
impl SectionHeaderBuilder
{
pub fn new() -> SectionHeaderBuilder
{
return SectionHeaderBuilder {
header: SectionHeader::new()
};
}
pub fn with_size(mut self, size: u32) -> Self
{
self.header.size = size;
return self;
}
pub fn with_type(mut self, typeb: u8) -> Self
{
self.header.btype = typeb;
return self;
}
pub fn with_compression(mut self, method: CompressionMethod) -> Self
{
match method {
CompressionMethod::Xz => self.header.flags |= FLAG_COMPRESS_XZ,
CompressionMethod::Zlib => self.header.flags |= FLAG_COMPRESS_ZLIB
}
self.header.csize = COMPRESSION_THRESHOLD;
return self;
}
pub fn with_threshold(mut self, threshold: u32) -> Self
{
self.header.csize = threshold;
return self;
}
pub fn with_checksum(mut self, chksum: Checksum) -> Self
{
match chksum {
Checksum::Crc32 => self.header.flags |= FLAG_CHECK_CRC32,
Checksum::Weak => self.header.flags |= FLAG_CHECK_WEAK
}
return self;
}
pub fn build(self) -> SectionHeader
{
return self.header;
}
}
pub struct MainHeaderBuilder
{
header: MainHeader
}
impl MainHeaderBuilder
{
pub fn new() -> MainHeaderBuilder
{
return MainHeaderBuilder {
header: MainHeader::new()
};
}
pub fn with_type(mut self, typeb: u8) -> Self
{
self.header.btype = typeb;
return self;
}
pub fn with_type_ext(mut self, type_ext: [u8; 16]) -> Self
{
self.header.type_ext = type_ext;
return self;
}
pub fn with_version(mut self, version: u32) -> Self
{
self.header.version = version;
return self;
}
pub fn build(self) -> MainHeader
{
return self.header;
}
}