Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for conversions of paths #608

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions rustler/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{Env, Error, NifResult, Term};
#[macro_use]
pub mod atom;
pub mod i128;
pub mod path;
pub use crate::types::atom::Atom;

pub mod binary;
Expand Down
29 changes: 29 additions & 0 deletions rustler/src/types/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::{Decoder, Encoder, Env, Error, NifResult, Term};
use std::path::{Path, PathBuf};

impl Encoder for Path {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
self.as_os_str().to_str().encode(env)
}
}

impl Encoder for PathBuf {
fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
self.as_path().encode(env)
}
}

impl<'a> Decoder<'a> for &'a Path {
fn decode(term: Term<'a>) -> NifResult<Self> {
let bin = term.decode_as_binary().or(Err(Error::BadArg))?;
let s = std::str::from_utf8(bin.as_slice()).or(Err(Error::BadArg))?;
Ok(Path::new(s))
}
}

impl<'a> Decoder<'a> for PathBuf {
fn decode(term: Term<'a>) -> NifResult<Self> {
let s: &str = term.decode()?;
Ok(PathBuf::from(s))
}
}
2 changes: 2 additions & 0 deletions rustler_tests/lib/rustler_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,6 @@ defmodule RustlerTest do
def maybe_add_one_to_tuple(_tuple), do: err()
def add_i32_from_tuple(_tuple), do: err()
def greeting_person_from_tuple(_tuple), do: err()

def append_to_path(_path, _to_append), do: err()
end
2 changes: 2 additions & 0 deletions rustler_tests/native/rustler_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod test_error;
mod test_list;
mod test_map;
mod test_nif_attrs;
mod test_path;
mod test_primitives;
mod test_range;
mod test_resource;
Expand Down Expand Up @@ -103,6 +104,7 @@ rustler::init!(
test_codegen::reserved_keywords::reserved_keywords_type_echo,
test_codegen::generic_types::generic_struct_echo,
test_codegen::generic_types::mk_generic_map,
test_path::append_to_path,
],
load = load
);
Expand Down
8 changes: 8 additions & 0 deletions rustler_tests/native/rustler_test/src/test_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use std::path::{Path, PathBuf};

#[rustler::nif]
pub fn append_to_path(p: &Path, comp: &str) -> PathBuf {
let mut p = p.to_path_buf();
p.push(comp);
p
}
Empty file.
Loading