use rustorb_giop::{CodeSetComponent, CodeSetComponentInfo, CodeSetContext, code_set};
use crate::{CompletionStatus, SystemException, SystemExceptionKind};
pub const NATIVE_CHAR: u32 = code_set::UTF_8;
pub const CONVERSION_CHAR: &[u32] = &[code_set::ISO_8859_1];
pub const NATIVE_WCHAR: u32 = code_set::UTF_16;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CodeSets {
pub char_data: u32,
pub wchar_data: Option<u32>,
}
impl CodeSets {
pub const DEFAULT: CodeSets = CodeSets {
char_data: code_set::ISO_8859_1,
wchar_data: None,
};
pub const NATIVE: CodeSets = CodeSets {
char_data: NATIVE_CHAR,
wchar_data: Some(NATIVE_WCHAR),
};
pub fn negotiate(info: &CodeSetComponentInfo) -> Result<CodeSets, SystemException> {
let char_data =
choose(&info.for_char_data, NATIVE_CHAR, CONVERSION_CHAR).ok_or_else(incompatible)?;
let wchar = &info.for_wchar_data;
let wchar_data = if wchar.native_code_set == 0 && wchar.conversion_code_sets.is_empty() {
None
} else {
Some(choose(wchar, NATIVE_WCHAR, &[]).ok_or_else(incompatible)?)
};
Ok(CodeSets {
char_data,
wchar_data,
})
}
pub fn context(&self) -> CodeSetContext {
CodeSetContext {
char_data: self.char_data,
wchar_data: self.wchar_data.unwrap_or(0),
}
}
pub fn encode_string(&self, s: &str) -> Result<Vec<u8>, SystemException> {
match self.char_data {
code_set::UTF_8 => Ok(s.as_bytes().to_vec()),
code_set::ISO_8859_1 => s
.chars()
.map(|c| {
u8::try_from(u32::from(c)).map_err(|_| data_conversion(CompletionStatus::No))
})
.collect(),
_ => Err(incompatible()),
}
}
pub fn decode_string(&self, bytes: &[u8]) -> Result<String, SystemException> {
match self.char_data {
code_set::UTF_8 => String::from_utf8(bytes.to_vec())
.map_err(|_| data_conversion(CompletionStatus::Yes)),
code_set::ISO_8859_1 => Ok(bytes.iter().map(|&b| char::from(b)).collect()),
_ => Err(incompatible()),
}
}
}
fn choose(server: &CodeSetComponent, native: u32, conversion: &[u32]) -> Option<u32> {
if server.native_code_set == native || server.conversion_code_sets.contains(&native) {
Some(native)
} else if conversion.contains(&server.native_code_set) {
Some(server.native_code_set)
} else {
conversion
.iter()
.copied()
.find(|c| server.conversion_code_sets.contains(c))
}
}
fn incompatible() -> SystemException {
SystemException::new(
SystemExceptionKind::CODESET_INCOMPATIBLE,
0,
CompletionStatus::No,
)
}
fn data_conversion(completed: CompletionStatus) -> SystemException {
SystemException::new(SystemExceptionKind::DATA_CONVERSION, 0, completed)
}
#[cfg(test)]
mod tests {
use super::*;
fn info(char_native: u32, char_conv: &[u32], wchar_native: u32) -> CodeSetComponentInfo {
CodeSetComponentInfo {
for_char_data: CodeSetComponent {
native_code_set: char_native,
conversion_code_sets: char_conv.to_vec(),
},
for_wchar_data: CodeSetComponent {
native_code_set: wchar_native,
conversion_code_sets: Vec::new(),
},
}
}
#[test]
fn prefers_utf8_when_the_server_converts_to_it() {
let cs = CodeSets::negotiate(&info(
code_set::ISO_8859_1,
&[code_set::UTF_8],
code_set::UTF_16,
))
.unwrap();
assert_eq!(cs.char_data, code_set::UTF_8);
assert_eq!(cs.wchar_data, Some(code_set::UTF_16));
}
#[test]
fn falls_back_to_latin1() {
let cs = CodeSets::negotiate(&info(code_set::ISO_8859_1, &[], 0)).unwrap();
assert_eq!(cs.char_data, code_set::ISO_8859_1);
assert_eq!(cs.wchar_data, None);
assert_eq!(cs.encode_string("caf\u{e9}").unwrap(), b"caf\xe9");
assert_eq!(cs.decode_string(b"caf\xe9").unwrap(), "caf\u{e9}");
assert!(cs.encode_string("\u{3042}").is_err());
}
#[test]
fn rejects_unknown_code_sets() {
let e = CodeSets::negotiate(&info(0x0001_0020, &[], 0)).unwrap_err();
assert!(e.is(SystemExceptionKind::CODESET_INCOMPATIBLE));
}
}