Skip to content
Open
Changes from 2 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
38 changes: 19 additions & 19 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 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_script = cstring_enum!(path_as_str, MetaCallLoaderError)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need this from c_script to c_path

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, I keep missing those 😅


new_scripts.push(c_script.as_ptr());
new_paths.push(c_script.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