use compact_str::CompactString;
use tap::Pipe;
use widestring::U16CStr;
use windows_sys::Win32::Globalization::{GetUserDefaultLocaleName, MAX_LOCALE_NAME};
pub fn get_system_locale<'a>() -> Result<CompactString, &'a str> {
let mut buffer = [0u16; MAX_LOCALE_NAME as _];
let result =
unsafe { GetUserDefaultLocaleName(buffer.as_mut_ptr(), buffer.len() as i32) }
as usize;
match result {
0 => Err("API call failed"), len if len > buffer.len() => Err("Buffer overflow"),
_ => unsafe { U16CStr::from_ptr_str(buffer.as_ptr()) }
.to_string_lossy()
.pipe(CompactString::from)
.pipe(Ok),
}
}