src/pathname.rs in faster_path-0.2.3 vs src/pathname.rs in faster_path-0.2.4
- old
+ new
@@ -1,15 +1,15 @@
-use helpers::*;
+use helpers::new_pathname_instance;
use basename;
use chop_basename;
use cleanpath_aggressive;
use dirname;
use extname;
use plus;
use ruru;
-use ruru::{RString, Boolean, Array};
+use ruru::{RString, Boolean, Array, AnyObject, NilClass, Object};
use std::path::{MAIN_SEPARATOR,Path};
use std::fs;
type MaybeString = Result<ruru::RString, ruru::result::Error>;
type MaybeBoolean = Result<ruru::Boolean, ruru::result::Error>;
@@ -39,20 +39,21 @@
ext.ok().unwrap_or(RString::new("")).to_str()
)
)
}
-pub fn pn_children(pth: MaybeString, with_dir: MaybeBoolean) -> Array {
- let rstring = pth.ok().unwrap_or(RString::new("."));
- let val = rstring.to_str();
- let mut with_directory = with_dir.ok().unwrap_or(Boolean::new(true)).to_bool();
- if val == "." {
- with_directory = false;
- }
+pub fn pn_children(pth: MaybeString, with_dir: MaybeBoolean) -> AnyObject {
let mut arr = Array::new();
+ let val = pth.ok().unwrap_or(RString::new("."));
+ let val = val.to_str();
if let Ok(entries) = fs::read_dir(val) {
+ let mut with_directory = with_dir.ok().unwrap_or(Boolean::new(true)).to_bool();
+ if val == "." {
+ with_directory = false;
+ }
+
for entry in entries {
if with_directory {
match entry {
Ok(v) => { arr.push(RString::new(v.path().to_str().unwrap())); },
_ => {}
@@ -62,45 +63,48 @@
Ok(v) => { arr.push(RString::new(v.file_name().to_str().unwrap())); },
_ => {}
};
}
}
+
+ arr.to_any_object()
+ } else {
+ // TODO: When ruru exceptions are available switch the exception logic
+ // from the Ruby side to the Rust side
+ NilClass::new().to_any_object()
}
- arr
}
-pub fn pn_children_compat(pth: MaybeString, with_dir: MaybeBoolean) -> Array {
- let rstring = pth.ok().unwrap_or(RString::new("."));
- let val = rstring.to_str();
- let mut with_directory = with_dir.ok().unwrap_or(Boolean::new(true)).to_bool();
- if val == "." {
- with_directory = false;
- }
+pub fn pn_children_compat(pth: MaybeString, with_dir: MaybeBoolean) -> AnyObject {
let mut arr = Array::new();
+ let val = pth.ok().unwrap_or(RString::new("."));
+ let val = val.to_str();
if let Ok(entries) = fs::read_dir(val) {
+ let mut with_directory = with_dir.ok().unwrap_or(Boolean::new(true)).to_bool();
+ if val == "." {
+ with_directory = false;
+ }
+
for entry in entries {
if with_directory {
- match entry {
- Ok(v) => { arr.push(
- class_new("Pathname", vec![str_to_any_obj(v.path().to_str().unwrap())])
- );
- },
- _ => {}
+ if let Ok(v) = entry {
+ arr.push(new_pathname_instance(v.path().to_str().unwrap()));
};
} else {
- match entry {
- Ok(v) => { arr.push(
- class_new("Pathname", vec![str_to_any_obj(v.file_name().to_str().unwrap())])
- );
- },
- _ => {}
+ if let Ok(v) = entry {
+ arr.push(new_pathname_instance(v.file_name().to_str().unwrap()));
};
}
}
+
+ arr.to_any_object()
+ } else {
+ // TODO: When ruru exceptions are available switch the exception logic
+ // from the Ruby side to the Rust side
+ NilClass::new().to_any_object()
}
- arr
}
pub fn pn_chop_basename(pth: MaybeString) -> Array {
let mut arr = Array::with_capacity(2);
let results = chop_basename::chop_basename(pth.ok().unwrap_or(RString::new("")).to_str());
@@ -150,38 +154,48 @@
// pub fn pn_each_filename(pth: MaybeString) -> NilClass {
// NilClass::new()
// }
-pub fn pn_entries(pth: MaybeString) -> Array {
- let files = fs::read_dir(pth.ok().unwrap_or(RString::new("")).to_str()).unwrap();
+pub fn pn_entries(pth: MaybeString) -> AnyObject {
let mut arr = Array::new();
- arr.push(RString::new("."));
- arr.push(RString::new(".."));
+ if let Ok(files) = fs::read_dir(pth.ok().unwrap_or(RString::new("")).to_str()) {
+ arr.push(RString::new("."));
+ arr.push(RString::new(".."));
- for file in files {
- let file_name_str = file.unwrap().file_name().into_string().unwrap();
- arr.push(RString::new(&file_name_str[..]));
- }
+ for file in files {
+ let file_name_str = file.unwrap().file_name().into_string().unwrap();
+ arr.push(RString::new(&file_name_str[..]));
+ }
- arr
+ arr.to_any_object()
+ } else {
+ // TODO: When ruru exceptions are available switch the exception logic
+ // from the Ruby side to the Rust side
+ NilClass::new().to_any_object()
+ }
}
-pub fn pn_entries_compat(pth: MaybeString) -> Array {
- let files = fs::read_dir(pth.ok().unwrap_or(RString::new("")).to_str()).unwrap();
+pub fn pn_entries_compat(pth: MaybeString) -> AnyObject {
let mut arr = Array::new();
- arr.push(class_new("Pathname", vec![str_to_any_obj(&"."[..])]));
- arr.push(class_new("Pathname", vec![str_to_any_obj(&".."[..])]));
+ if let Ok(files) = fs::read_dir(pth.ok().unwrap_or(RString::new("")).to_str()) {
+ arr.push(new_pathname_instance("."));
+ arr.push(new_pathname_instance(".."));
- for file in files {
- let file_name_str = file.unwrap().file_name().into_string().unwrap();
- arr.push(class_new("Pathname", vec![str_to_any_obj(&file_name_str[..])]));
- }
+ for file in files {
+ let file_name_str = file.unwrap().file_name().into_string().unwrap();
+ arr.push(new_pathname_instance(&file_name_str));
+ }
- arr
+ arr.to_any_object()
+ } else {
+ // TODO: When ruru exceptions are available switch the exception logic
+ // from the Ruby side to the Rust side
+ NilClass::new().to_any_object()
+ }
}
pub fn pn_extname(pth: MaybeString) -> RString {
RString::new(
extname::extname(pth.ok().unwrap_or(RString::new("")).to_str())
@@ -210,11 +224,16 @@
// pub fn pn_parent(pth: MaybeString){}
// also need impl +
pub fn pn_plus(pth1: MaybeString, pth2: MaybeString) -> RString {
- RString::new(&plus::plus_paths(pth1.ok().unwrap().to_str(), pth2.ok().unwrap().to_str())[..])
+ RString::new(
+ &plus::plus_paths(
+ pth1.ok().unwrap_or(RString::new("")).to_str(),
+ pth2.ok().unwrap_or(RString::new("")).to_str()
+ )[..]
+ )
}
// pub fn pn_prepend_prefix(prefix: MaybeString, relpath: MaybeString){}
pub fn pn_is_relative(pth: MaybeString) -> Boolean {
@@ -233,5 +252,6 @@
// pub fn pn_relative_path_from(){}
// pub fn pn_rmtree(pth: MaybeString) -> NilClass {
// NilClass::new()
// }
+//