Skip to content

Commit 8085b51

Browse files
committed
Refactor code in lib.rs
1 parent a687db7 commit 8085b51

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

src/lib.rs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ extern crate libc;
22

33
mod ffi;
44

5+
use ffi::*;
56
use libc::c_char;
67
use std::ffi::*;
7-
use ffi::*;
88

99
/// Result of opening a file dialog
1010
pub enum NFDResult {
@@ -34,44 +34,36 @@ pub fn open_save_dialog(filter_list: &str, default_path: &str) -> NFDResult {
3434
}
3535

3636
fn open_dialog(filter_list: &str, default_path: &str, dialog_type: &DialogType) -> NFDResult {
37+
let result: nfdresult_t;
38+
let result_cstring;
3739

38-
let mut final_cstring = CString::new("").unwrap();
40+
let filter_list_cstring = CString::new(filter_list).unwrap();
41+
let default_path_cstring = CString::new(default_path).unwrap();
3942
let out_path = CString::new("").unwrap().into_raw() as *mut *mut c_char;
40-
let result: nfdresult_t;
4143

4244
unsafe {
4345
result = match dialog_type {
4446
&DialogType::SingleFile => {
45-
NFD_OpenDialog(
46-
CString::new(filter_list).unwrap().as_ptr(),
47-
CString::new(default_path).unwrap().as_ptr(),
48-
out_path)
47+
NFD_OpenDialog(filter_list_cstring.as_ptr(), default_path_cstring.as_ptr(), out_path)
4948
},
5049

5150
&DialogType::SaveFile => {
52-
NFD_SaveDialog(
53-
CString::new(filter_list).unwrap().as_ptr(),
54-
CString::new(default_path).unwrap().as_ptr(),
55-
out_path)
51+
NFD_SaveDialog(filter_list_cstring.as_ptr(), default_path_cstring.as_ptr(), out_path)
5652
},
5753
};
5854

59-
match result {
60-
nfdresult_t::NFD_OKAY => {
61-
final_cstring = CString::from_raw(*out_path);
62-
},
63-
nfdresult_t::NFD_ERROR => {
64-
final_cstring = CStr::from_ptr(NFD_GetError()).to_owned();
65-
}
66-
_ => {},
55+
result_cstring = match result {
56+
nfdresult_t::NFD_OKAY => CString::from_raw(*out_path),
57+
nfdresult_t::NFD_ERROR => CStr::from_ptr(NFD_GetError()).to_owned(),
58+
_ => CString::new("").unwrap()
6759
}
6860
}
6961

70-
let final_string = final_cstring.to_str().unwrap().to_string();
62+
let result_string = result_cstring.to_str().unwrap().to_string();
7163

7264
match result {
73-
nfdresult_t::NFD_OKAY => NFDResult::Okay(final_string),
65+
nfdresult_t::NFD_OKAY => NFDResult::Okay(result_string),
7466
nfdresult_t::NFD_CANCEL => NFDResult::Cancel,
75-
nfdresult_t::NFD_ERROR => NFDResult::Error(final_string)
67+
nfdresult_t::NFD_ERROR => NFDResult::Error(result_string)
7668
}
7769
}

0 commit comments

Comments
 (0)