-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for conversions of paths (#608)
- Loading branch information
Showing
6 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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)) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
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.