apt_pkg_native/
lib.rs

1//! Here lies bindings to `libapt-pkg`, which is what the `apt`, `apt-get`, `apt-cache`, etc.
2//! commands use to view and manipulate the state of packages on the system.
3//!
4//! Currently, not much is exposed. You can pretty much only view basic package
5//! information, like with `apt-cache policy foo`.
6//!
7//! `libapt-pkg` has basically no documentation. `python-apt` is slightly better,
8//! but is also pretty inconsistent on the documentation front. The design of this
9//! crate is closer to `libapt-pkg`, despite it being pretty insane.
10//!
11//! The core concept here is an "iterator". Forget everything you know about iterators,
12//! these iterators are pretty much pointers. The crate attempts to make them act
13//! a bit more like Rust `Iterator`s, but is crippled by the insanity.
14//!
15//! Methods which "find" something will reposition one of these "iterators" at the right place
16//! in an existing stream of items.
17//!
18//! I recommend using `.map()` to turn an "iterator" into a Rust type as soon as possible.
19//! The returned map-like thing *is* a Rust `Iterator`, so you can do normal operations on it.
20//!
21//! Here's an example: normally you wouldn't need this ugly `.map(|_| ())` (read as "map anything
22//! to the empty object"), but here, it is *also* converting a sh... apt "iterator" into a
23//! real Iterator.
24//!
25//! ```rust,no_run
26//! extern crate apt_pkg_native;
27//! let mut cache = apt_pkg_native::Cache::get_singleton();
28//! let total_packages = cache.iter().map(|_| ()).count();
29//! ```
30//!
31//! `libapt-pkg` also just segfaults if you do anything wrong, or re-use anything at the wrong time,
32//! or etc. I've tried to hide this, but I advise you not to push or outsmart the borrow checker.
33
34mod citer;
35mod raw;
36pub mod sane;
37pub mod simple;
38
39pub use crate::sane::Cache;
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn pretty_print_all() {
47        let mut cache = Cache::get_singleton();
48        let read_all_and_count = cache.iter().map(simple::BinaryPackageVersions::new).count();
49        assert!(read_all_and_count > 2);
50        assert_eq!(read_all_and_count, cache.iter().count());
51    }
52
53    #[test]
54    fn find_a_package() {
55        let mut cache = Cache::get_singleton();
56
57        if let Some(view) = cache.find_by_name("apt").next() {
58            assert_eq!("apt", view.name());
59        } else {
60            panic!("not found!");
61        }
62
63        assert!(cache
64            .find_by_name("this-package-doesnt-exist-and-if-someone-makes-it-ill-be-really-angry")
65            .next()
66            .is_none());
67    }
68
69    #[test]
70    fn compare_versions() {
71        use std::cmp::Ordering;
72        let cache = Cache::get_singleton();
73        assert_eq!(Ordering::Less, cache.compare_versions("3.0", "3.1"));
74        assert_eq!(Ordering::Greater, cache.compare_versions("3.1", "3.0"));
75        assert_eq!(Ordering::Equal, cache.compare_versions("3.0", "3.0"));
76    }
77
78    #[test]
79    fn reload() {
80        let mut cache = Cache::get_singleton();
81        cache.reload();
82        cache.reload();
83        cache.reload();
84        cache.reload();
85    }
86}