Skip to content

Commit

Permalink
Merge pull request #20 from ahcm/main
Browse files Browse the repository at this point in the history
Use &Path instead of &str for index.
  • Loading branch information
jguhlin authored Jan 21, 2023
2 parents 28674ea + 37337aa commit 371962c
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::mem::MaybeUninit;
use std::num::NonZeroI32;
use std::path::Path;

use std::os::unix::ffi::OsStrExt;

use minimap2_sys::*;

#[cfg(feature = "map-file")]
Expand Down Expand Up @@ -443,25 +445,29 @@ impl Aligner {
/// // Use the previously built index
/// Aligner::builder().map_ont().with_index("my_index.mmi", None);
/// ```
pub fn with_index(mut self, path: &str, output: Option<&str>) -> Result<Self, &'static str> {
pub fn with_index<P>(mut self, path: P, output: Option<&str>) -> Result<Self, &'static str>
where P: AsRef<Path>
{
return match self.set_index(path, output) {
Ok(_) => Ok(self),
Err(e) => Err(e),
};
}

pub fn set_index(&mut self, path: &str, output: Option<&str>) -> Result<(), &'static str> {
pub fn set_index<P>(&mut self, path: P, output: Option<&str>) -> Result<(), &'static str>
where P: AsRef<Path>
{
// Confirm file exists
if !Path::new(path).exists() {
if !path.as_ref().exists() {
return Err("File does not exist");
}

// Confirm file is not empty
if Path::new(path).metadata().unwrap().len() == 0 {
if path.as_ref().metadata().unwrap().len() == 0 {
return Err("File is empty");
}

let path = match std::ffi::CString::new(path) {
let path = match std::ffi::CString::new(path.as_ref().as_os_str().as_bytes()) {
Ok(path) => path,
Err(_) => return Err("Invalid path"),
};
Expand Down

0 comments on commit 371962c

Please sign in to comment.