feat: add byte conversion to base header

This commit is contained in:
leonhard committed 2026-10-22 00:06:15 +02:00
1 parent 8225b3d113
commit 91819f19bf
1 file changed
+87 -12
+87 -12
View File
@@ -9,30 +9,82 @@ pub struct BaseHeader {
os: Os,
}
enum CompressionMethod {
impl BaseHeader {
const SIZE: usize = 10;
fn to_bytes(self) -> [u8; Self::SIZE] {
let mtime: [u8; 4] = self.last_modified.into();
[
ID1,
ID2,
self.compression_method.into(),
self.flags.0,
mtime[0],
mtime[1],
mtime[2],
mtime[3],
self.extra_flags.0,
self.os.into(),
]
}
}
impl From<BaseHeader> for [u8; 10] {
fn from(header: BaseHeader) -> Self {
header.to_bytes()
}
}
pub enum CompressionMethod {
Deflate,
}
struct Flags(u8);
impl Flags {
const FTEXT: u8 = 1 << 0;
const FHCRC: u8 = 1 << 1;
const FEXTRA: u8 = 1 << 2;
const FNAME: u8 = 1 << 3;
const FCOMMENT: u8 = 1 << 4;
impl From<CompressionMethod> for u8 {
fn from(method: CompressionMethod) -> Self {
match method {
CompressionMethod::Deflate => 8,
}
}
}
enum ModificationTime {
pub struct Flags(u8);
impl Flags {
pub const FTEXT: u8 = 1 << 0;
pub const FHCRC: u8 = 1 << 1;
pub const FEXTRA: u8 = 1 << 2;
pub const FNAME: u8 = 1 << 3;
pub const FCOMMENT: u8 = 1 << 4;
pub fn contains(&self, flag: u8) -> bool {
self.0 & flag != 0
}
pub fn with(self, flag: u8) -> Self {
Flags(self.0 | flag)
}
}
pub enum ModificationTime {
UnixTimestamp(u32),
NoTimestampAvailable,
}
impl From<ModificationTime> for [u8; 4] {
fn from(mtime: ModificationTime) -> Self {
use ModificationTime::*;
match mtime {
NoTimestampAvailable => [0; 4],
UnixTimestamp(ts) => ts.to_le_bytes(),
}
}
}
// The meaning of these depends on the compression method
struct ExtraFlags(u8);
enum Os {
FaaFilesystem,
pub enum Os {
FatFilesystem,
Amiga,
Vms,
Unix,
@@ -48,3 +100,26 @@ enum Os {
AcornRiscos,
Unknown,
}
impl From<Os> for u8 {
fn from(os: Os) -> Self {
use Os::*;
match os {
FatFilesystem => 0,
Amiga => 1,
Vms => 2,
Unix => 3,
VmCms => 4,
AtariTos => 5,
HpfsFilesystem => 6,
Macintosh => 7,
ZSystem => 8,
CpM => 9,
Tops20 => 10,
NtfsFilesystem => 11,
Qdos => 12,
AcornRiscos => 13,
Unknown => 255,
}
}
}