wii_disk 0.1.2

Gamecube file header library and utilities.
Documentation
// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR MPL-2.0
// SPDX-FileCopyrightText: 2024 Gabriel Marcano <gabemarcano@yahoo.com>

use crate::error::Error;

use std::string::String;

use encoding_rs::SHIFT_JIS;
use encoding_rs::WINDOWS_1252;

// FIXME I don't think this is enough to properly detect the encoding. I may need to request the
// region on Banner construction.
/// Re-encodes the data to either latin1 or SHIFT JIS, if possible.
///
/// # Errors
///
/// Returns [`Error::Parse`] if the data does not decode as valid latin1 or SHIFT JIS characters.
pub fn from_latin1_or_shift_jis(data: &[u8]) -> Result<String, Error> {
    let (result, _, error) = WINDOWS_1252.decode(data);
    Ok(if error {
        let (result, _, error) = SHIFT_JIS.decode(data);
        if error {
            return Err(Error::Parse(
                "unable to convert to latin1 or SHIFT JIS".into(),
            ));
        }
        result.to_string()
    } else {
        result.to_string()
    })
}

/// Trims null and whitespace characters (in that order) from the given string.
pub fn trim(string: &str) -> &str {
    string.trim_matches('\0').trim()
}