impl Display; remove prettyprint for compat; fix section

This commit is contained in:
Chris West (Faux) 2017-07-13 21:50:53 +01:00
parent d1948421ce
commit 457c31b4a4
7 changed files with 82 additions and 43 deletions

View File

@ -4,7 +4,6 @@
#include <assert.h> #include <assert.h>
#include <apt-pkg/pkgcache.h> #include <apt-pkg/pkgcache.h>
#include <apt-pkg/prettyprinters.h>
#include <apt-pkg/cachefile.h> #include <apt-pkg/cachefile.h>
struct PCache { struct PCache {
@ -52,9 +51,6 @@ extern "C" {
const char *pkg_iter_current_version(PPkgIterator *iterator); const char *pkg_iter_current_version(PPkgIterator *iterator);
const char *pkg_iter_candidate_version(PPkgIterator *iterator); const char *pkg_iter_candidate_version(PPkgIterator *iterator);
// freed by caller
char *pkg_iter_pretty(PCache *cache, PPkgIterator *iterator);
PVerIterator *pkg_iter_ver_iter(PPkgIterator *iterator); PVerIterator *pkg_iter_ver_iter(PPkgIterator *iterator);
void ver_iter_release(PVerIterator *iterator); void ver_iter_release(PVerIterator *iterator);
@ -138,15 +134,11 @@ const char *pkg_iter_current_version(PPkgIterator *wrapper) {
} }
const char *pkg_iter_candidate_version(PPkgIterator *wrapper) { const char *pkg_iter_candidate_version(PPkgIterator *wrapper) {
return wrapper->cache->cache_file->GetPolicy()->GetCandidateVer(wrapper->iterator).VerStr(); pkgCache::VerIterator it = wrapper->cache->cache_file->GetPolicy()->GetCandidateVer(wrapper->iterator);
} if (it.end()) {
return nullptr;
char *pkg_iter_pretty(PCache *cache, PPkgIterator *wrapper) { }
assert(cache); return it.VerStr();
assert(wrapper);
std::stringstream ss;
ss << APT::PrettyPkg(cache->cache_file->GetDepCache(), wrapper->iterator);
return strdup(ss.str().c_str());
} }
PVerIterator *pkg_iter_ver_iter(PPkgIterator *wrapper) { PVerIterator *pkg_iter_ver_iter(PPkgIterator *wrapper) {

View File

@ -1,8 +1,12 @@
extern crate gcc; extern crate gcc;
const SRC: &str = "apt-pkg-c/lib.cpp";
fn main() { fn main() {
println!("cargo:rerun-if-changed={}", SRC);
gcc::Config::new() gcc::Config::new()
.file("apt-pkg-c/lib.cpp") .file(SRC)
.cpp(true) .cpp(true)
.flag("-std=gnu++11") .flag("-std=gnu++11")
.compile("libapt-pkg-c.a"); .compile("libapt-pkg-c.a");

View File

@ -1,10 +1,11 @@
extern crate apt_pkg_native; extern crate apt_pkg_native;
use apt_pkg_native::Cache; use apt_pkg_native::Cache;
use apt_pkg_native::simple;
fn main() { fn main() {
let mut cache = Cache::get_singleton(); let mut cache = Cache::get_singleton();
for item in cache.iter().map(|item| item.pretty_print()) { for item in cache.iter().map(simple::BinaryPackageVersions::new) {
println!("{}", item); println!("{}: {:?}", item.pkg, item.versions);
} }
} }

View File

@ -49,7 +49,9 @@ mod tests {
#[test] #[test]
fn pretty_print_all() { fn pretty_print_all() {
let mut cache = Cache::get_singleton(); let mut cache = Cache::get_singleton();
assert!(cache.iter().map(|item| item.pretty_print()).count() > 0); let read_all_and_count = cache.iter().map(simple::BinaryPackageVersions::new).count();
assert!(read_all_and_count > 2);
assert_eq!(read_all_and_count, cache.iter().count());
} }
#[test] #[test]

View File

@ -41,7 +41,6 @@ extern "C" {
pub fn pkg_iter_arch(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_current_version(iterator: PPkgIterator) -> *const c_char;
pub fn pkg_iter_candidate_version(iterator: PPkgIterator) -> *const c_char; pub fn pkg_iter_candidate_version(iterator: PPkgIterator) -> *const c_char;
pub fn pkg_iter_pretty(cache: PCache, iterator: PPkgIterator) -> *mut c_char;
// Version iterators // Version iterators

View File

@ -127,18 +127,6 @@ impl<'c> PkgView<'c> {
unsafe { make_owned_ascii_string(raw::pkg_iter_candidate_version(self.ptr)) } unsafe { make_owned_ascii_string(raw::pkg_iter_candidate_version(self.ptr)) }
} }
pub fn pretty_print(&self) -> String {
unsafe {
let ptr = raw::pkg_iter_pretty(self.cache.ptr, self.ptr);
let result = ffi::CStr::from_ptr(ptr)
.to_str()
.expect("package names are always low-ascii")
.to_string();
libc::free(ptr as *mut libc::c_void);
result
}
}
pub fn versions(&self) -> CIterator<VerIterator> { pub fn versions(&self) -> CIterator<VerIterator> {
CIterator { CIterator {
first: true, first: true,
@ -204,10 +192,9 @@ impl<'c> VerView<'c> {
} }
} }
pub fn section(&self) -> String { pub fn section(&self) -> Option<String> {
unsafe { unsafe {
make_owned_ascii_string(raw::ver_iter_section(self.ptr)) make_owned_ascii_string(raw::ver_iter_section(self.ptr))
.expect("versions always have a section")
} }
} }

View File

@ -1,27 +1,46 @@
//! Some structs representing basic concepts, and utilities to copy out of "iterators". //! Some structs representing basic concepts, and utilities to copy out of "iterators".
use std::fmt;
use sane; use sane;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct BinaryPackage { pub struct BinaryPackage {
pub name: String, pub name: String,
pub arch: String, pub arch: String,
pub current_version: Option<String>,
pub candidate_version: Option<String>,
} }
impl BinaryPackage { impl BinaryPackage {
pub fn new(iter: &sane::PkgView) -> Self { pub fn new(view: &sane::PkgView) -> Self {
BinaryPackage { BinaryPackage {
name: iter.name(), name: view.name(),
arch: iter.arch(), arch: view.arch(),
current_version: view.current_version(),
candidate_version: view.candidate_version(),
} }
} }
} }
impl fmt::Display for BinaryPackage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.name, self.arch)?;
if let Some(ref version) = self.current_version {
write!(f, " @ {}", version)?;
}
if let Some(ref version) = self.candidate_version {
write!(f, " -> {}", version)?;
}
Ok(())
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Version { pub struct Version {
pub version: String, pub version: String,
pub arch: String, pub arch: String,
pub section: String, pub section: Option<String>,
pub source_package: String, pub source_package: String,
pub source_version: String, pub source_version: String,
pub priority: i32, pub priority: i32,
@ -29,14 +48,49 @@ pub struct Version {
impl Version { impl Version {
pub fn new(iter: &sane::VerView) -> Self { pub fn new(view: &sane::VerView) -> Self {
Version { Version {
version: iter.version(), version: view.version(),
arch: iter.arch(), arch: view.arch(),
section: iter.section(), section: view.section(),
source_package: iter.source_package(), source_package: view.source_package(),
source_version: iter.source_version(), source_version: view.source_version(),
priority: iter.priority(), priority: view.priority(),
} }
} }
} }
impl fmt::Display for Version {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{} in {:?} from {}:{} at {}",
self.version,
self.arch,
self.section,
self.source_package,
self.source_version,
self.priority,
)
}
}
#[derive(Clone, Debug)]
pub struct BinaryPackageVersions {
pub pkg: BinaryPackage,
pub versions: Vec<Version>,
}
impl BinaryPackageVersions {
pub fn new(view: &sane::PkgView) -> Self {
BinaryPackageVersions {
pkg: BinaryPackage::new(view),
versions: view.versions().map(Version::new).collect(),
}
}
}
impl fmt::Display for BinaryPackageVersions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} + {} versions", self.pkg, self.versions.len())
}
}