|
| 1 | +use std::ffi::{c_void, CStr, CString}; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +#[cfg(has_std__ffi__c_char)] |
| 5 | +use std::ffi::{c_char, c_int}; |
| 6 | + |
| 7 | +#[cfg(not(has_std__ffi__c_char))] |
| 8 | +#[allow(non_camel_case_types)] |
| 9 | +pub type c_char = i8; |
| 10 | + |
| 11 | +#[cfg(not(has_std__ffi__c_char))] |
| 12 | +#[allow(non_camel_case_types)] |
| 13 | +pub type c_int = i32; |
| 14 | + |
| 15 | +use libgit_sys::*; |
| 16 | + |
| 17 | +pub struct ConfigSet(*mut libgit_config_set); |
| 18 | +impl ConfigSet { |
| 19 | + pub fn new() -> Self { |
| 20 | + unsafe { ConfigSet(libgit_configset_alloc()) } |
| 21 | + } |
| 22 | + |
| 23 | + pub fn add_files(&mut self, files: &[&Path]) { |
| 24 | + for file in files { |
| 25 | + let pstr = file.to_str().expect("Invalid UTF-8"); |
| 26 | + let rs = CString::new(pstr).expect("Couldn't convert to CString"); |
| 27 | + unsafe { |
| 28 | + libgit_configset_add_file(self.0, rs.as_ptr()); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + pub fn get_int(&mut self, key: &str) -> Option<c_int> { |
| 34 | + let key = CString::new(key).expect("Couldn't convert to CString"); |
| 35 | + let mut val: c_int = 0; |
| 36 | + unsafe { |
| 37 | + if libgit_configset_get_int(self.0, key.as_ptr(), &mut val as *mut c_int) != 0 { |
| 38 | + return None; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + Some(val) |
| 43 | + } |
| 44 | + |
| 45 | + pub fn get_string(&mut self, key: &str) -> Option<String> { |
| 46 | + let key = CString::new(key).expect("Couldn't convert key to CString"); |
| 47 | + let mut val: *mut c_char = std::ptr::null_mut(); |
| 48 | + unsafe { |
| 49 | + if libgit_configset_get_string(self.0, key.as_ptr(), &mut val as *mut *mut c_char) != 0 |
| 50 | + { |
| 51 | + return None; |
| 52 | + } |
| 53 | + let borrowed_str = CStr::from_ptr(val); |
| 54 | + let owned_str = |
| 55 | + String::from(borrowed_str.to_str().expect("Couldn't convert val to str")); |
| 56 | + free(val as *mut c_void); // Free the xstrdup()ed pointer from the C side |
| 57 | + Some(owned_str) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl Default for ConfigSet { |
| 63 | + fn default() -> Self { |
| 64 | + Self::new() |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl Drop for ConfigSet { |
| 69 | + fn drop(&mut self) { |
| 70 | + unsafe { |
| 71 | + libgit_configset_free(self.0); |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[cfg(test)] |
| 77 | +mod tests { |
| 78 | + use super::*; |
| 79 | + |
| 80 | + #[test] |
| 81 | + fn load_configs_via_configset() { |
| 82 | + let mut cs = ConfigSet::new(); |
| 83 | + cs.add_files(&[ |
| 84 | + Path::new("testdata/config1"), |
| 85 | + Path::new("testdata/config2"), |
| 86 | + Path::new("testdata/config3"), |
| 87 | + ]); |
| 88 | + // ConfigSet retrieves correct value |
| 89 | + assert_eq!(cs.get_int("trace2.eventTarget"), Some(1)); |
| 90 | + // ConfigSet respects last config value set |
| 91 | + assert_eq!(cs.get_int("trace2.eventNesting"), Some(3)); |
| 92 | + // ConfigSet returns None for missing key |
| 93 | + assert_eq!(cs.get_string("foo.bar"), None); |
| 94 | + } |
| 95 | +} |
0 commit comments