Skip to content

Commit

Permalink
add windows support
Browse files Browse the repository at this point in the history
  • Loading branch information
kaaboaye committed Mar 19, 2023
1 parent 44b94b1 commit 8780f04
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions rustler_tests/native/dynamic_load/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustler::Atom;
use std::{ffi::OsStr, fs::read_to_string, os::unix::prelude::OsStrExt, path::PathBuf};
use std::{ffi::OsStr, fs::read_to_string, path::PathBuf};

static mut DATASET: Option<Box<str>> = None;

Expand Down Expand Up @@ -29,12 +29,27 @@ fn load<'a>(env: rustler::Env<'a>, args: rustler::Term<'a>) -> bool {
let key = Atom::from_str(env, "priv_path").unwrap().to_term(env);
let priv_path = args.map_get(key).unwrap();
let priv_path = priv_path.into_binary().unwrap().as_slice();
let priv_path = OsStr::from_bytes(priv_path);
let asset_path = PathBuf::from(priv_path);

let asset_path = build_path_buf(priv_path);

initialize_dataset(asset_path);

true
}

#[cfg(unix)]
fn build_path_buf(priv_path: &[u8]) -> PathBuf {
use std::os::unix::prelude::OsStrExt;

let priv_path = OsStr::from_bytes(priv_path);
PathBuf::from(priv_path)
}

#[cfg(windows)]
fn build_path_buf(priv_path: &[u8]) -> PathBuf {
let string_slice = std::str::from_utf8(priv_path).expect("Data is not valid UTF-8, we could decode it without valid UTF-8 requirements but lets not do that for now because its easier this way");
let priv_path = OsStr::new(string_slice);
PathBuf::from(priv_path)
}

rustler::init!("Elixir.DynamicData", [get_dataset], load = load);

0 comments on commit 8780f04

Please sign in to comment.