use {
crate::{
error::result_from_raw,
progress::{trampoline, ProgressCallback},
string::{TStr, ThinTStr},
sys, Error, OpenFlags, Wim, WimLib, WriteFlags,
},
std::marker::PhantomData,
};
impl WimLib {
#[doc(alias = "wimlib_join")]
pub fn join(
&self,
split_wims: &[ThinTStr],
output_path: &TStr,
split_open_flags: OpenFlags,
joined_write_flags: WriteFlags,
) -> Result<(), Error> {
let split_wims_count = cast_length_to_u32(split_wims.len())?;
result_from_raw(unsafe {
sys::wimlib_join(
split_wims.as_ptr().cast(),
split_wims_count,
output_path.as_ptr(),
split_open_flags.bits(),
joined_write_flags.bits(),
)
})
}
#[doc(alias = "wimlib_join_with_progress")]
pub fn join_with_progress_callback(
&self,
split_wims: &[ThinTStr],
output_path: &TStr,
split_open_flags: OpenFlags,
joined_write_flags: WriteFlags,
progress_callback: &mut ProgressCallback,
) -> Result<(), Error> {
let split_wims_count = cast_length_to_u32(split_wims.len())?;
let progress_thin: *mut *mut ProgressCallback = &mut (progress_callback as *mut _);
result_from_raw(unsafe {
sys::wimlib_join_with_progress(
split_wims.as_ptr().cast(),
split_wims_count,
output_path.as_ptr(),
split_open_flags.bits(),
joined_write_flags.bits(),
Some(trampoline),
progress_thin.cast(),
)
})
}
}
impl Wim {
#[doc(alias = "wimlib_reference_resource_files")]
pub fn reference_resource_files(
&self,
resource_wimfiles_or_globs: &[ThinTStr],
ref_flags: ReferenceFlags,
open_flags: OpenFlags,
) -> Result<(), Error> {
let resource_wimfiles_or_globs_count =
cast_length_to_u32(resource_wimfiles_or_globs.len())?;
result_from_raw(unsafe {
sys::wimlib_reference_resource_files(
self.wimstruct,
resource_wimfiles_or_globs.as_ptr().cast(),
resource_wimfiles_or_globs_count,
ref_flags.bits(),
open_flags.bits(),
)
})
}
#[doc(alias = "wimlib_reference_resources")]
pub fn reference_resources(
&self,
resource_wims: &[ResourceWimRef],
ref_flags: ReferenceFlags,
) -> Result<(), Error> {
let resource_wims_count = cast_length_to_u32(resource_wims.len())?;
result_from_raw(unsafe {
sys::wimlib_reference_resources(
self.wimstruct,
resource_wims.as_ptr().cast_mut().cast(),
resource_wims_count,
ref_flags.bits(),
)
})
}
pub fn split(
&self,
split_name: &TStr,
size: u64,
write_flags: WriteFlags,
) -> Result<(), Error> {
result_from_raw(unsafe {
sys::wimlib_split(
self.wimstruct,
split_name.as_ptr(),
size,
write_flags.bits(),
)
})
}
}
#[repr(transparent)]
pub struct ResourceWimRef<'a> {
inner: *mut sys::WIMStruct,
_borrow: PhantomData<&'a TStr>,
}
impl<'a> From<&'a Wim> for ResourceWimRef<'a> {
fn from(value: &'a Wim) -> Self {
Self::new(value)
}
}
impl<'a> ResourceWimRef<'a> {
pub fn new(wim: &'a Wim) -> Self {
Self {
inner: wim.wimstruct,
_borrow: PhantomData,
}
}
pub fn array<const N: usize>(paths: [&'a Wim; N]) -> [Self; N] {
paths.map(Self::new)
}
}
fn cast_length_to_u32(value: usize) -> Result<u32, Error> {
u32::try_from(value).map_err(|_| Error::InvalidParam)
}
bitflags::bitflags! {
pub struct ReferenceFlags: std::ffi::c_int {
const GLOB_ENABLE = sys::WIMLIB_REF_FLAG_GLOB_ENABLE as _;
const GLOB_ERR_ON_NOMATCH = sys::WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH as _;
}
}