Skip to content

Commit d7c8fd0

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

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/opts.rs

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

56
use once_cell::sync::Lazy;
67

@@ -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,15 @@ 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!(get_search_path(ConfigLevel::Global), Ok(path.into_c_string()?));
142143

143144
// Append
144145
let paths = join_paths(["$PATH", path].iter())?;
145-
let expected_paths = join_paths([path, path].iter())?.into_string().unwrap();
146+
let expected_paths = join_paths([path, path].iter())?.into_c_string()?;
146147
set_search_path(ConfigLevel::Global, paths)?;
147148
assert_eq!(get_search_path(ConfigLevel::Global), Ok(expected_paths));
148149

0 commit comments

Comments
 (0)