use crate::records::formats::common::{
EncodedText, ExportOptions, TextEncoding, encode_text, encode_text_with_options,
};
use crate::records::record::Record;
use encoding_rs::SHIFT_JIS;
use std::borrow::Cow;
use super::{Ki2Error, KifError, export_ki2, export_kif, parse_ki2_str, parse_kif_str};
fn decode_kif_bytes(data: &[u8]) -> Cow<'_, str> {
std::str::from_utf8(data).map_or_else(
|_| {
let (decoded, _, _) = SHIFT_JIS.decode(data);
decoded
},
Cow::Borrowed,
)
}
pub fn parse_kif_bytes(data: &[u8]) -> Result<Record, KifError> {
let decoded = decode_kif_bytes(data);
parse_kif_str(&decoded)
}
pub fn parse_ki2_bytes(data: &[u8]) -> Result<Record, Ki2Error> {
let decoded = decode_kif_bytes(data);
parse_ki2_str(&decoded)
}
pub fn export_kif_bytes(record: &Record, encoding: TextEncoding) -> Result<EncodedText, KifError> {
let text = export_kif(record)?;
Ok(encode_text(&text, encoding))
}
pub fn export_kif_bytes_with_options(
record: &Record,
options: ExportOptions,
) -> Result<EncodedText, KifError> {
let text = export_kif(record)?;
Ok(encode_text_with_options(&text, options))
}
pub fn export_ki2_bytes(record: &Record, encoding: TextEncoding) -> Result<EncodedText, Ki2Error> {
let text = export_ki2(record)?;
Ok(encode_text(&text, encoding))
}
pub fn export_ki2_bytes_with_options(
record: &Record,
options: ExportOptions,
) -> Result<EncodedText, Ki2Error> {
let text = export_ki2(record)?;
Ok(encode_text_with_options(&text, options))
}