Skip to content

Commit e5d8643

Browse files
committed
fix: get_search_path will returns CString
1 parent ccb48dd commit e5d8643

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/opts.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Bindings to libgit2's git_libgit2_opts function.
22
3+
use std::ffi::CString;
34
use std::sync::Mutex;
45

56
use once_cell::sync::Lazy;
@@ -41,7 +42,7 @@ impl SearchPath {
4142
Ok(())
4243
}
4344

44-
pub fn get(&self, level: ConfigLevel) -> Result<String, Error> {
45+
pub fn get(&self, level: ConfigLevel) -> Result<CString, Error> {
4546
let buf = Buf::new();
4647
unsafe {
4748
call::c_try(raw::git_libgit2_opts(
@@ -50,7 +51,7 @@ impl SearchPath {
5051
buf.raw(),
5152
))?;
5253
}
53-
Ok(buf.as_str().unwrap().to_string())
54+
buf.into_c_string()
5455
}
5556
}
5657

@@ -83,7 +84,7 @@ pub fn reset_search_path(level: ConfigLevel) -> Result<(), Error> {
8384
///
8485
/// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
8586
/// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
86-
pub fn get_search_path(level: ConfigLevel) -> Result<String, Error> {
87+
pub fn get_search_path(level: ConfigLevel) -> Result<CString, Error> {
8788
SEARCH_PATH.lock().unwrap().get(level)
8889
}
8990

@@ -134,15 +135,18 @@ mod test {
134135
fn search_path() -> Result<(), Box<dyn std::error::Error>> {
135136
let path = "fake_path";
136137
let original = get_search_path(ConfigLevel::Global);
137-
assert_ne!(original, Ok(path.into()));
138+
assert_ne!(original, Ok(path.into_c_string()?));
138139

139140
// Set
140141
set_search_path(ConfigLevel::Global, &path)?;
141-
assert_eq!(get_search_path(ConfigLevel::Global), Ok(path.into()));
142+
assert_eq!(
143+
get_search_path(ConfigLevel::Global),
144+
Ok(path.into_c_string()?)
145+
);
142146

143147
// Append
144148
let paths = join_paths(["$PATH", path].iter())?;
145-
let expected_paths = join_paths([path, path].iter())?.into_string().unwrap();
149+
let expected_paths = join_paths([path, path].iter())?.into_c_string()?;
146150
set_search_path(ConfigLevel::Global, paths)?;
147151
assert_eq!(get_search_path(ConfigLevel::Global), Ok(expected_paths));
148152

0 commit comments

Comments
 (0)