use std::path::Path;
use std::rc::Rc;
use rustduck_sys::bridge::ffi;
use crate::buffer::{decode_utf16, encode_utf16};
use crate::error::native_exception;
use crate::{DuckContext, Error, Result};
#[repr(i32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CrcValidation {
Ignore = 0,
Check = 1,
Compute = 2,
}
pub struct SectionFile {
native: cxx::UniquePtr<ffi::RustDuckSectionFile>,
duck: Rc<DuckContext>,
}
impl SectionFile {
pub fn new(duck: Rc<DuckContext>) -> Result<Self> {
let native = ffi::rustduck_section_file_new(duck.native())
.map_err(|error| native_exception("SectionFile::new", error))?;
if native.is_null() {
return Err(Error::NativeFailure {
operation: "SectionFile::new",
});
}
Ok(Self { native, duck })
}
pub fn duck_context(&self) -> &Rc<DuckContext> {
&self.duck
}
pub fn clear(&mut self) -> Result<()> {
ffi::rustduck_section_file_clear(self.native.pin_mut())
.map_err(|error| native_exception("SectionFile::clear", error))
}
pub fn binary_size(&self) -> Result<usize> {
ffi::rustduck_section_file_binary_size(self.native())
.map_err(|error| native_exception("SectionFile::binary_size", error))
}
pub fn sections_count(&self) -> Result<usize> {
ffi::rustduck_section_file_sections_count(self.native())
.map_err(|error| native_exception("SectionFile::sections_count", error))
}
pub fn tables_count(&self) -> Result<usize> {
ffi::rustduck_section_file_tables_count(self.native())
.map_err(|error| native_exception("SectionFile::tables_count", error))
}
pub fn set_crc_validation(&mut self, mode: CrcValidation) -> Result<()> {
ffi::rustduck_section_file_set_crc_validation(self.native.pin_mut(), mode as i32)
.map_err(|error| native_exception("SectionFile::set_crc_validation", error))
}
pub fn load_binary_buffer(&mut self, data: &[u8]) -> Result<()> {
ffi::rustduck_section_file_load_binary_buffer(self.native.pin_mut(), data)
.map_err(|error| native_exception("SectionFile::load_binary_buffer", error))?
.then_some(())
.ok_or(Error::NativeFailure {
operation: "SectionFile::load_binary_buffer",
})
}
pub fn to_binary(&self) -> Result<Vec<u8>> {
ffi::rustduck_section_file_to_binary(self.native())
.map_err(|error| native_exception("SectionFile::to_binary", error))
}
pub fn load_binary(&mut self, path: &Path) -> Result<()> {
let path = native_path(path)?;
ffi::rustduck_section_file_load_binary(self.native.pin_mut(), &path)
.map_err(|error| native_exception("SectionFile::load_binary", error))?
.then_some(())
.ok_or(Error::NativeFailure {
operation: "SectionFile::load_binary",
})
}
pub fn save_binary(&self, path: &Path) -> Result<()> {
self.save_path(
path,
"SectionFile::save_binary",
ffi::rustduck_section_file_save_binary,
)
}
pub fn load_xml(&mut self, source: &str) -> Result<()> {
let source = encode_utf16(source);
ffi::rustduck_section_file_load_xml(self.native.pin_mut(), &source)
.map_err(|error| native_exception("SectionFile::load_xml", error))?
.then_some(())
.ok_or(Error::NativeFailure {
operation: "SectionFile::load_xml",
})
}
pub fn save_xml(&self, path: &Path) -> Result<()> {
self.save_path(
path,
"SectionFile::save_xml",
ffi::rustduck_section_file_save_xml,
)
}
pub fn save_json(&self, path: &Path) -> Result<()> {
self.save_path(
path,
"SectionFile::save_json",
ffi::rustduck_section_file_save_json,
)
}
pub fn to_xml(&self) -> Result<String> {
let bytes = ffi::rustduck_section_file_to_xml_utf16(self.native())
.map_err(|error| native_exception("SectionFile::to_xml", error))?;
decode_utf16("SectionFile::to_xml", &bytes)
}
pub fn to_json(&self) -> Result<String> {
let bytes = ffi::rustduck_section_file_to_json_utf16(self.native())
.map_err(|error| native_exception("SectionFile::to_json", error))?;
decode_utf16("SectionFile::to_json", &bytes)
}
fn save_path(
&self,
path: &Path,
operation: &'static str,
save: fn(&ffi::RustDuckSectionFile, &[u8]) -> std::result::Result<bool, cxx::Exception>,
) -> Result<()> {
let path = native_path(path)?;
save(self.native(), &path)
.map_err(|error| native_exception(operation, error))?
.then_some(())
.ok_or(Error::NativeFailure { operation })
}
fn native(&self) -> &ffi::RustDuckSectionFile {
self.native
.as_ref()
.expect("SectionFile construction rejects null owners")
}
}
fn native_path(path: &Path) -> Result<Vec<u8>> {
let path = path.to_str().ok_or_else(|| Error::InvalidArgument {
field: "path",
message: "must be valid UTF-8 for TSDuck's UTF-16 file API".to_owned(),
})?;
Ok(encode_utf16(path))
}