Skip to content

Commit

Permalink
Use dirs crate
Browse files Browse the repository at this point in the history
* On Unix, replaces deprecated `std::env::home_dir`
* On Windows, replaces a function call using `winapi`
  • Loading branch information
murarth committed Sep 17, 2018
1 parent 684ac59 commit 2b1235a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 49 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ readme = "README.md"
travis-ci = { repository = "murarth/linefeed" }

[dependencies]
dirs = "1.0"
mortal = "0.1.5"

[target.'cfg(windows)'.dependencies]
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
#![deny(missing_docs)]

extern crate dirs;
extern crate mortal;

#[cfg(windows)] #[macro_use(DEFINE_GUID)] extern crate winapi;
#[cfg(windows)] extern crate winapi;

#[cfg(test)] #[macro_use] extern crate assert_matches;

Expand Down
4 changes: 3 additions & 1 deletion src/unix/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::env::{home_dir, var_os};
use std::env::var_os;
use std::path::PathBuf;

use dirs::home_dir;

pub fn env_init_file() -> Option<PathBuf> {
var_os("INPUTRC").map(PathBuf::from)
}
Expand Down
49 changes: 2 additions & 47 deletions src/windows/path.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
use std::env::var_os;
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::path::PathBuf;
use std::ptr;
use std::slice;

use winapi::shared::ntdef::PWSTR;
use winapi::shared::minwindef::LPVOID;
use winapi::shared::winerror::S_OK;
use winapi::um::combaseapi::CoTaskMemFree;
use winapi::um::shlobj::SHGetKnownFolderPath;
use winapi::um::winbase::lstrlenW;
use dirs::data_dir;

pub fn env_init_file() -> Option<PathBuf> {
var_os("INPUTRC").map(PathBuf::from)
Expand All @@ -21,41 +12,5 @@ pub fn system_init_file() -> Option<PathBuf> {
}

pub fn user_init_file() -> Option<PathBuf> {
app_data().map(|p| p.join(r"linefeed\inputrc"))
}

#[allow(non_upper_case_globals)]
mod guid {
DEFINE_GUID!{
FOLDERID_RoamingAppData,
0x3EB685DB,
0x65F9, 0x4CF6,
0xA0, 0x3A, 0xE3, 0xEF, 0x65, 0x72, 0x9F, 0x3D
}
}

fn app_data() -> Option<PathBuf> {
let mut path = ptr::null_mut();

let res = unsafe { SHGetKnownFolderPath(
&guid::FOLDERID_RoamingAppData,
0,
ptr::null_mut(),
&mut path) };

match res {
S_OK => {
let s = w_string(path);
unsafe { CoTaskMemFree(path as LPVOID); }
Some(PathBuf::from(s))
}
_ => None
}
}

fn w_string(ptr: PWSTR) -> OsString {
let len = unsafe { lstrlenW(ptr) };
let slice = unsafe { slice::from_raw_parts(ptr, len as usize) };

OsString::from_wide(slice)
data_dir().map(|p| p.join(r"linefeed\inputrc"))
}

0 comments on commit 2b1235a

Please sign in to comment.