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 <apt-pkg/pkgcache.h>
#include <apt-pkg/prettyprinters.h>
#include <apt-pkg/cachefile.h>
struct PCache {
@ -52,9 +51,6 @@ extern "C" {
const char *pkg_iter_current_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);
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) {
return wrapper->cache->cache_file->GetPolicy()->GetCandidateVer(wrapper->iterator).VerStr();
}
char *pkg_iter_pretty(PCache *cache, PPkgIterator *wrapper) {
assert(cache);
assert(wrapper);
std::stringstream ss;
ss << APT::PrettyPkg(cache->cache_file->GetDepCache(), wrapper->iterator);
return strdup(ss.str().c_str());
pkgCache::VerIterator it = wrapper->cache->cache_file->GetPolicy()->GetCandidateVer(wrapper->iterator);
if (it.end()) {
return nullptr;
}
return it.VerStr();
}
PVerIterator *pkg_iter_ver_iter(PPkgIterator *wrapper) {

View File

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

View File

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

View File

@ -49,7 +49,9 @@ mod tests {
#[test]
fn pretty_print_all() {
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]

View File

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

View File

@ -127,18 +127,6 @@ impl<'c> PkgView<'c> {
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> {
CIterator {
first: true,
@ -204,10 +192,9 @@ impl<'c> VerView<'c> {
}
}
pub fn section(&self) -> String {
pub fn section(&self) -> Option<String> {
unsafe {
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".
use std::fmt;
use sane;
#[derive(Clone, Debug)]
pub struct BinaryPackage {
pub name: String,
pub arch: String,
pub current_version: Option<String>,
pub candidate_version: Option<String>,
}
impl BinaryPackage {
pub fn new(iter: &sane::PkgView) -> Self {
pub fn new(view: &sane::PkgView) -> Self {
BinaryPackage {
name: iter.name(),
arch: iter.arch(),
name: view.name(),
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)]
pub struct Version {
pub version: String,
pub arch: String,
pub section: String,
pub section: Option<String>,
pub source_package: String,
pub source_version: String,
pub priority: i32,
@ -29,14 +48,49 @@ pub struct Version {
impl Version {
pub fn new(iter: &sane::VerView) -> Self {
pub fn new(view: &sane::VerView) -> Self {
Version {
version: iter.version(),
arch: iter.arch(),
section: iter.section(),
source_package: iter.source_package(),
source_version: iter.source_version(),
priority: iter.priority(),
version: view.version(),
arch: view.arch(),
section: view.section(),
source_package: view.source_package(),
source_version: view.source_version(),
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())
}
}