libgm 0.8.1

A tool for unpacking and modding GameMaker games
Documentation
// SPDX-License-Identifier: GPL-3.0-only
use std::ops::RangeBounds;

use crate::prelude::*;

pub fn format_bytes(bytes: usize) -> String {
    const UNITS: [&str; 6] = ["B", "KB", "MB", "GB", "TB", "PB"];

    #[allow(clippy::cast_precision_loss)]
    let mut size = bytes as f64;
    let mut unit_idx = 0;

    while size >= 1000.0 && unit_idx < UNITS.len() - 1 {
        size /= 1000.0;
        unit_idx += 1;
    }

    let unit = UNITS[unit_idx];
    format!("{size:.2} {unit}")
}

pub fn hexdump(raw_data: &[u8]) -> String {
    if raw_data.is_empty() {
        return String::new();
    }

    let mut buffer = String::with_capacity(raw_data.len() * 3);
    for byte in raw_data {
        use std::fmt::Write;
        let _ = write!(&mut buffer, "{byte:02X} ");
    }
    buffer.pop(); // pop last space

    buffer
}

pub fn hexdump_range(raw_data: &[u8], range: impl RangeBounds<usize>) -> Result<String> {
    use std::ops::Bound::Excluded;
    use std::ops::Bound::Included;
    use std::ops::Bound::Unbounded;

    let len = raw_data.len();
    let start = match range.start_bound() {
        Included(&n) => n,
        Excluded(&n) => n + 1,
        Unbounded => 0,
    };
    let end = match range.end_bound() {
        Included(&n) => n + 1,
        Excluded(&n) => n,
        Unbounded => len,
    };

    if start > len || end > len {
        bail!("Range out of bounds: {start}..{end} for length {len}");
    }
    if start > end {
        bail!("Invalid range: start {start} > end {end}");
    }

    let slice: &[u8] = &raw_data[start..end];
    Ok(hexdump(slice))
}

/// This function should only ever be called when an error has already occurred.
///
/// It basically just gets the typename in a slightly more readable manner.
pub fn typename<T>() -> &'static str {
    // HACK: When Rust adds support for handling typenames at compile time, use that.
    let ty = std::any::type_name::<T>();
    let ty = trim_prefixes(ty, &[
        "libgm::wad::elem::",
        "libgm::wad::reference::",
        "libgm::wad::",
        "libgm::",
    ]);
    remove_module_prefix(ty).unwrap_or(ty)
}

#[inline]
fn remove_module_prefix(ty: &str) -> Option<&str> {
    let (module, ty) = ty.split_once("::")?;
    let (module_first, module_rest) = module.as_bytes().split_at_checked(1)?;
    let (ty_first, ty_rest) = ty.as_bytes().split_at_checked(1)?;
    if module_first.eq_ignore_ascii_case(ty_first) && module_rest == ty_rest {
        Some(ty)
    } else {
        None
    }
}

#[inline]
fn trim_prefixes<'a>(string: &'a str, prefixes: &[&str]) -> &'a str {
    for prefix in prefixes {
        if let Some(s) = string.strip_prefix(prefix) {
            return s;
        }
    }
    string
}