maybe expose some more things

This commit is contained in:
Chris West (Faux) 2017-07-13 11:57:29 +01:00
parent 7562f18141
commit c4655a5bc5
5 changed files with 59 additions and 10 deletions

View File

@ -35,9 +35,12 @@ extern "C" {
bool pkg_iter_end(PPkgIterator *iterator);
const char *pkg_iter_name(PPkgIterator *iterator);
const char *pkg_iter_arch(PPkgIterator *iterator);
const char *pkg_iter_current_version(PPkgIterator *iterator);
// freed by caller
char *pkg_iter_pretty(PCache *cache, PPkgIterator *iterator);
}
void init_config_system() {
@ -85,6 +88,14 @@ const char *pkg_iter_name(PPkgIterator *wrapper) {
return wrapper->iterator.Name();
}
const char *pkg_iter_arch(PPkgIterator *wrapper) {
return wrapper->iterator.Arch();
}
const char *pkg_iter_current_version(PPkgIterator *wrapper) {
return wrapper->iterator.CurVersion();
}
char *pkg_iter_pretty(PCache *cache, PPkgIterator *wrapper) {
assert(cache);
assert(wrapper);
@ -93,6 +104,3 @@ char *pkg_iter_pretty(PCache *cache, PPkgIterator *wrapper) {
return strdup(ss.str().c_str());
}
//const void *pkg_iter_name(pkgCache::PkgIterator *iterator) {
// return iterator->VersionList();
//}

View File

@ -1,7 +1,10 @@
extern crate libc;
pub mod raw;
pub mod sane;
mod raw;
mod sane;
mod simple;
pub use sane::Cache;
#[cfg(test)]
mod tests {
@ -9,8 +12,8 @@ mod tests {
#[test]
fn list_all() {
let mut cache = sane::Cache::new();
for name in cache.iter().map(|item: &sane::PkgIterator| item.pretty_print()) {
let mut cache = Cache::new();
for name in cache.iter().map(|item| item.pretty_print()) {
println!("{}", name);
}
}

View File

@ -27,6 +27,8 @@ extern {
pub fn pkg_iter_end(iterator: PPkgIterator) -> bool;
pub fn pkg_iter_name(iterator: PPkgIterator) -> *const c_char;
pub fn pkg_iter_arch(iterator: PPkgIterator) -> *const c_char;
pub fn pkg_iter_current_version(iterator: PPkgIterator) -> *const c_char;
pub fn pkg_iter_pretty(cache: PCache, iterator: PPkgIterator) -> *mut c_char;
}

View File

@ -4,6 +4,7 @@ use libc;
use raw;
// Probably not cloneable / copyable.
/// You might only be able to create one of these per process.
#[derive(Debug)]
pub struct Cache {
ptr: raw::PCache
@ -86,7 +87,8 @@ impl<'c> PkgIterator<'c> {
count
}
pub fn map<F>(self, f: F) -> PkgMap<'c, F> {
pub fn map<F, B>(self, f: F) -> PkgMap<'c, F>
where F: FnMut(&PkgIterator) -> B {
PkgMap {
it: self,
f,
@ -94,12 +96,31 @@ impl<'c> PkgIterator<'c> {
}
}
unsafe fn make_owned_ascii_string(ptr: *const libc::c_char) -> String {
ffi::CStr::from_ptr(ptr)
.to_str()
.expect("value should always be low-ascii")
.to_string()
}
/// Actual accessors
impl<'c> PkgIterator<'c> {
pub fn name(&self) -> String {
unsafe {
ffi::CStr::from_ptr(raw::pkg_iter_name(self.ptr))
}.to_str().expect("package names are always low-ascii").to_string()
make_owned_ascii_string(raw::pkg_iter_name(self.ptr))
}
}
pub fn arch(&self) -> String {
unsafe {
make_owned_ascii_string(raw::pkg_iter_arch(self.ptr))
}
}
pub fn current_version(&self) -> String {
unsafe {
make_owned_ascii_string(raw::pkg_iter_current_version(self.ptr))
}
}
pub fn pretty_print(&self) -> String {

15
src/simple.rs Normal file
View File

@ -0,0 +1,15 @@
use sane;
pub struct BinaryPackage {
name: String,
arch: String,
}
impl BinaryPackage {
pub fn from_iter(iter: &sane::PkgIterator) -> BinaryPackage {
BinaryPackage {
name: iter.name(),
arch: iter.arch(),
}
}
}