use std::io;
use std::os::windows::io::OwnedHandle;
use windows_namespace_request_sys::PathCaptureError;
use windows_namespace_request_sys::open::OpenFile;
use wtf_string::Wtf16String;
#[cfg(test)]
mod tests;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Parameters {
pub access: u32,
pub disposition: u32,
pub flags: u32,
pub share: u32,
}
pub(super) fn request(path: Wtf16String, parameters: Parameters) -> io::Result<OpenFile> {
Ok(OpenFile::new(path)
.map_err(path_error)?
.with_desired_access(parameters.access)
.with_share_mode(parameters.share)
.with_creation_disposition(parameters.disposition)
.with_flags_and_attributes(parameters.flags))
}
pub(super) fn perform(request: &OpenFile) -> io::Result<OwnedHandle> {
request
.perform()
.map_err(|error| io::Error::from_raw_os_error(raw_code(error.code())))
}
fn path_error(error: PathCaptureError) -> io::Error {
match error {
PathCaptureError::CurrentDirectory(code) => {
io::Error::from_raw_os_error(raw_code(code.code()))
}
other => io::Error::new(io::ErrorKind::InvalidInput, other),
}
}
pub(super) fn raw_code(code: u32) -> i32 {
i32::from_ne_bytes(code.to_ne_bytes())
}