Documentation
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

use crate::utils::windows_1252::windows_1252_to_utf8;

/// Gives a representation of the bytes in native String.
/// It will try to:
/// * parse the bytes as an utf8 string
/// * if this fails it will try to decode it according to iso_8859_1
/// * if it fail it will try to decode it as utf8 replacing the invalid characters.
pub(crate) fn parse(bytes: Vec<u8>) -> String {
    match String::from_utf8(bytes) {
        Ok(s) => s,
        Err(e) => windows_1252_to_utf8(e.as_bytes()),
    }
}

#[cfg(test)]
mod test {
    use crate::conversions::parse;

    #[test]
    fn parse_utf8() {
        let value = "615µs";
        let result = parse(value.as_bytes().to_vec());
        assert_eq!("615µs".to_string(), result)
    }

    #[test]
    fn parse_iso_8859_1() {
        let value: Vec<u8> = vec![0x36, 0x31, 0x35, 0xb5, 0x73];
        let result = parse(value);
        assert_eq!("615µs".to_string(), result)
    }
}