use crate::ffi::utils::*;
use crate::ffi::{___mb_cur_max, CODESET};
use crate::settings::locale::Category;
use crate::{Locale, LocaleResult};
#[derive(Debug, Clone, PartialEq)]
pub struct CodeSetFormat {
pub code_set: Option<String>,
pub multibyte_max_bytes: Option<u32>,
}
pub fn get_code_set_format() -> CodeSetFormat {
let mb_max_bytes = unsafe { ___mb_cur_max() as u32 };
CodeSetFormat {
code_set: get_nl_string(CODESET),
multibyte_max_bytes: Some(mb_max_bytes),
}
}
pub fn get_code_set_format_for_locale(
locale: Locale,
inherit_current: bool,
) -> LocaleResult<CodeSetFormat> {
get_format_for_locale(
Category::CharacterTypes,
locale,
&get_code_set_format,
inherit_current,
)
}
#[cfg(test)]
mod tests {
use crate::settings::codeset::{get_code_set_format, get_code_set_format_for_locale};
use crate::settings::locale::{set_locale, Category};
use crate::Locale;
use std::str::FromStr;
#[test]
fn test_get_code_set_format() {
if set_locale(&Locale::POSIX, &Category::CharacterTypes) {
let format = get_code_set_format();
println!("{:#?}", format);
assert_eq!(format.code_set, Some("US-ASCII".to_string()));
assert_eq!(format.multibyte_max_bytes, Some(1));
} else {
panic!("set_locale returned false");
}
}
#[test]
fn test_get_code_set_format_for_locale() {
if set_locale(&Locale::POSIX, &Category::CharacterTypes) {
let format = get_code_set_format_for_locale(Locale::from_str("fr_FR").unwrap(), false);
println!("{:#?}", format);
let format = format.unwrap();
assert_eq!(format.code_set, None);
assert_eq!(format.multibyte_max_bytes, Some(4));
} else {
panic!("set_locale returned false");
}
}
#[test]
fn test_get_code_set_format_for_locale_2() {
if set_locale(&Locale::POSIX, &Category::CharacterTypes) {
let format =
get_code_set_format_for_locale(Locale::from_str("fr_FR.UTF-8").unwrap(), false);
println!("{:#?}", format);
let format = format.unwrap();
assert_eq!(format.code_set, Some("UTF-8".to_string()));
assert_eq!(format.multibyte_max_bytes, Some(4));
} else {
panic!("set_locale returned false");
}
}
}