Created
September 4, 2023 01:22
-
-
Save Verdagon/b7d61bbeded3fe0b138375e7a90371cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use vale_rust_ffi_utils::ValeStr; | |
use vale_rust_ffi_utils::make_vale_str; | |
use vale_rust_ffi_utils::load_vale_str; | |
use std::ffi::OsString; | |
use std::fs; | |
#[repr(C)] | |
pub struct DirReader { | |
data: [u8; std::mem::size_of::<fs::ReadDir>()] | |
} | |
#[allow(non_camel_case_types)] | |
#[repr(u64)] | |
pub enum Result__DirReader__FileErr { | |
Ok(DirReader), | |
Err(FileError) | |
} | |
#[repr(C)] | |
pub struct DirEntry { | |
data: [u8; std::mem::size_of::<fs::DirEntry>()] | |
} | |
#[allow(non_camel_case_types)] | |
#[repr(u64)] | |
pub enum Option_ValeStr { | |
None(), | |
Some(*mut ValeStr) | |
} | |
#[repr(u64)] | |
pub enum FileError { | |
NotFound(), | |
NameNotUtf8(), | |
Unknown(*mut ValeStr) | |
} | |
#[allow(non_camel_case_types)] | |
#[repr(u64)] | |
pub enum Result__Option_ValeStr__FileErr { | |
Ok(Option_ValeStr), | |
Err(FileError) | |
} | |
fn valeify_io_error(err: &std::io::Error) -> FileError { | |
if err.kind() == std::io::ErrorKind::NotFound { | |
return FileError::NotFound(); | |
} else { | |
return FileError::Unknown( | |
unsafe { make_vale_str(&err.to_string()) }); | |
} | |
} | |
#[no_mangle] | |
pub extern "C" fn ReadDir(path: *mut ValeStr) -> Result__DirReader__FileErr { | |
match fs::read_dir(unsafe { load_vale_str(path) }) { | |
Err(err) => { | |
return Result__DirReader__FileErr::Err( | |
valeify_io_error(&err)); | |
} | |
Ok(dir_reader) => { | |
return Result__DirReader__FileErr::Ok( | |
DirReader { data: unsafe { std::mem::transmute(dir_reader) } }); | |
} | |
} | |
} | |
#[no_mangle] | |
pub extern "C" fn NextFileName(iter: DirReader) -> Result__Option_ValeStr__FileErr { | |
let mut dir_reader: fs::ReadDir = unsafe { std::mem::transmute(iter.data) }; | |
if let Some(dir_entry_result) = dir_reader.next() { | |
match dir_entry_result { | |
Ok(dir_entry) => { | |
let file_name_os_str: OsString = dir_entry.file_name(); | |
if let Some(file_name_str) = file_name_os_str.to_str() { | |
return Result__Option_ValeStr__FileErr::Ok( | |
Option_ValeStr::Some( | |
unsafe { make_vale_str(file_name_str) })); | |
} else { | |
return Result__Option_ValeStr__FileErr::Err( | |
FileError::NameNotUtf8()); | |
} | |
} | |
Err(err) => { | |
return Result__Option_ValeStr__FileErr::Err( | |
valeify_io_error(&err)); | |
} | |
} | |
} else { | |
return Result__Option_ValeStr__FileErr::Ok( | |
Option_ValeStr::None()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment