zipatch-rs 1.5.0

Parser for FFXIV ZiPatch patch files
Documentation
use binrw::BinRead;

use super::util::read_null_trimmed_utf8;

/// `DELD` chunk: remove a directory under the game install root.
///
/// When applied, the patcher calls `remove_dir` for `<game_root>/<name>`.
/// Unlike `ADIR`, only an empty directory can be deleted this way; if the
/// directory is non-empty the OS returns an error. If
/// [`crate::ApplyContext::ignore_missing`] is set, a missing directory is
/// silently skipped instead of returning an error.
///
/// Like `ADIR`, `DELD` chunks are rare in modern patches. The reference
/// implementation logs failures rather than rethrowing in some paths; this
/// crate propagates them. See
/// `lib/FFXIVQuickLauncher/.../Chunk/DeleteDirectoryChunk.cs`.
///
/// # Wire format
///
/// ```text
/// [name_len: u32 BE] [name: name_len bytes, NUL-padded]
/// ```
///
/// Identical layout to [`crate::chunk::adir::AddDirectory`]. `name_len`
/// includes any trailing NUL padding bytes; the parsed field has them stripped.
///
/// # Errors
///
/// Parsing fails with [`crate::ZiPatchError::BinrwError`] if:
/// - the body is too short to contain the `name_len` field or the declared
///   number of name bytes (truncated input), or
/// - the name bytes are not valid UTF-8.
#[derive(BinRead, Debug, Clone, PartialEq, Eq)]
#[br(big)]
pub struct DeleteDirectory {
    /// Directory path relative to the game install root.
    ///
    /// Encoded as UTF-8 on the wire, length-prefixed by a `u32` big-endian
    /// byte count. Trailing NUL bytes used as alignment padding are stripped
    /// before this field is populated. Example: `"sqpack/ex3"`.
    #[br(parse_with = read_null_trimmed_utf8)]
    pub name: String,
}

pub(crate) fn parse(body: &[u8]) -> crate::Result<DeleteDirectory> {
    super::util::parse_be(body)
}

#[cfg(test)]
mod tests {
    use super::parse;

    #[test]
    fn parses_delete_directory() {
        let mut body = Vec::new();
        body.extend_from_slice(&4u32.to_be_bytes());
        body.extend_from_slice(b"data");
        assert_eq!(parse(&body).unwrap().name, "data");
    }
}