#![doc = include_str!("../README.md")]
use binrw::{binread, BinRead, BinReaderExt};
use bitflags::bitflags;
pub use chrono;
pub use fourcc_rs::FourCC;
pub mod type_code {
use fourcc_rs::{fourcc, FourCC};
pub const APPLICATION: FourCC = fourcc!("APPL");
}
#[derive(BinRead, Debug, Copy, Clone)]
#[br(big)]
pub struct Point {
pub x: i16,
pub y: i16,
}
#[binread]
pub struct PascalString {
#[br(temp)]
len: u8,
#[br(count(len), map(decode_string))]
contents: String,
}
impl PascalString {
pub fn contents(&self) -> String {
self.contents.to_string()
}
}
impl From<PascalString> for String {
fn from(val: PascalString) -> Self {
val.contents
}
}
pub fn string(text: PascalString) -> String {
text.contents
}
pub fn decode_string(bytes: Vec<u8>) -> String {
encoding_rs::MACINTOSH.decode(&bytes).0.to_string()
}
pub fn decode_string_from_slice(bytes: &[u8]) -> String {
encoding_rs::MACINTOSH.decode(bytes).0.to_string()
}
pub fn date(seconds_since_1904: u32) -> chrono::DateTime<chrono::Utc> {
const SECONDS_FROM_1904_TO_1970: i64 = 2082844800;
chrono::DateTime::from_timestamp(seconds_since_1904 as i64 - SECONDS_FROM_1904_TO_1970, 0)
.unwrap()
}
#[derive(Debug, Copy, Clone)]
pub enum Fork {
Resource,
Data,
}
impl Fork {
pub fn is_resource(&self) -> bool {
matches!(self, Fork::Resource)
}
pub fn is_data(&self) -> bool {
matches!(self, Fork::Data)
}
}
bitflags! {
#[derive(Debug, Default, Clone, Copy)]
pub struct FinderFlags: u16 {
const ALIAS = (1 << 15);
const INVISIBLE = (1 << 14);
const BUNDLE = (1 << 13);
const NAMELOCKED = (1 << 12);
const STATIONERY = (1 << 11);
const CUSTOMICON = (1 << 10);
const RESERVED = (1 << 9);
const INITED = (1 << 8);
const NOINIT = (1 << 7);
const SHARED = (1 << 6);
const SWITCH_LAUNCH = (1<<5);
const COLOR_RESERVED = (1<<4);
const COLORBIT2 = (1 << 3);
const COLORBIT1 = (1 << 2);
const COLORBIT0 = (1 << 1);
const ON_DESKTOP = (1 << 0);
}
}
impl BinRead for FinderFlags {
type Args<'a> = ();
fn read_options<R: std::io::Read + std::io::Seek>(
reader: &mut R,
_endian: binrw::Endian,
_args: Self::Args<'_>,
) -> binrw::BinResult<Self> {
let flags: u16 = reader.read_be()?;
if let Some(flags) = FinderFlags::from_bits(flags) {
return Ok(flags);
}
Ok(Default::default())
}
}