resource-fork-types 0.1.1

Support for reading common resource fork types in rust
Documentation
use binrw::{binread, BinRead};
use macintosh_utils::{decode_string, PascalString};
use resource_fork::Resource;

#[binread]
/// Common string variant where one-byte denotes the length of the string followed by that many mac-roman encoded characters
struct TotalLengthPrefixedString {
    #[br(temp)]
    len: u8,

    #[br(temp, calc(if len == 0 { 0 } else {len-1}))]
    str_len: u8,

    #[br(count(str_len), map(decode_string))]
    pub contents: String,
}

#[binread]
#[derive(Debug, Resource)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[resource(code = "STR#")]
#[br(big)]
pub struct Strings {
    #[br(temp)]
    count: u16,

    #[br(count(count), map(|s: Vec<PascalString>| s.into_iter().map(macintosh_utils::string).collect()))]
    pub strings: Vec<std::string::String>,
}

#[allow(unused)]
#[derive(Resource, Debug, BinRead)]
#[resource(code = "STR ")]
pub struct SingleString {
    #[br(map(|l: TotalLengthPrefixedString| l.contents))]
    content: String,
}
impl AsRef<[u8]> for SingleString {
    fn as_ref(&self) -> &[u8] {
        self.content.as_ref()
    }
}