Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions source/ports/rs_port/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,53 @@ use std::{
ptr,
};

/// Loads a script from a single file. Usage example: ...
/// Loads a path from a single file. Usage example: ...
/// ```
/// // A Nodejs script
/// // A Nodejs path
/// metacall::load::from_single_file("node", "index.js").unwrap();
/// ```
pub fn from_single_file(
tag: impl ToString,
script: impl AsRef<Path>,
path: impl AsRef<Path>,
) -> Result<(), MetaCallLoaderError> {
from_file(tag, [script])
from_file(tag, [path])
}
/// Loads a script from file. Usage example: ...
/// Loads a path from file. Usage example: ...
/// ```
/// // A Nodejs script
/// // A Nodejs path
/// metacall::load::from_file("node", ["index.js", "main.js"]).unwrap();
/// ```
pub fn from_file(
tag: impl ToString,
scripts: impl IntoIterator<Item = impl AsRef<Path>>,
paths: impl IntoIterator<Item = impl AsRef<Path>>,
) -> Result<(), MetaCallLoaderError> {
let c_tag = cstring_enum!(tag, MetaCallLoaderError)?;
let mut c_script: CString;
let mut c_path: CString;

let mut new_scripts: Vec<*const i8> = Vec::new();
for script in scripts.into_iter() {
let script_as_pathbuf = PathBuf::from(script.as_ref());
let script_as_str = script_as_pathbuf.to_str().unwrap();
let mut new_paths: Vec<*const i8> = Vec::new();
for path in paths.into_iter() {
let path_as_pathbuf = PathBuf::from(path.as_ref());
let path_as_str = path_as_pathbuf.to_str().unwrap();

if !script_as_pathbuf.exists() {
return Err(MetaCallLoaderError::FileNotFound(script_as_pathbuf));
if !path_as_pathbuf.exists() {
return Err(MetaCallLoaderError::FileNotFound(path_as_pathbuf));
}
if !script_as_pathbuf.is_file() {
if !path_as_pathbuf.is_file() {
return Err(MetaCallLoaderError::NotAFileOrPermissionDenied(
script_as_pathbuf,
path_as_pathbuf,
));
}

c_script = cstring_enum!(script_as_str, MetaCallLoaderError)?;
c_path = cstring_enum!(path_as_str, MetaCallLoaderError)?;

new_scripts.push(c_script.as_ptr());
new_paths.push(c_path.as_ptr());
}

if unsafe {
metacall_load_from_file(
c_tag.as_ptr(),
new_scripts.as_mut_ptr(),
new_scripts.len(),
new_paths.as_mut_ptr(),
new_paths.len(),
ptr::null_mut(),
)
} != 0
Expand Down
Loading