Debian Codegraph @

src/iterators/provider.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use std::fmt;
2
3 use cxx::UniquePtr;
4
5 use crate::raw::PrvIterator;
6 use crate::{Cache, Package, Version};
7
8 /// A Provider provides a Version and/or Package.
9 ///
10 /// Typically if you had a virtual package you would get its providers
11 /// to find which Package/Version you should really install.
12 pub struct Provider<'a> {
13 pub(crate) ptr: UniquePtr<PrvIterator>,
14 cache: &'a Cache,
15 }
16
17 impl<'a> Provider<'a> {
18 pub fn new(ptr: UniquePtr<PrvIterator>, cache: &'a Cache) -> Provider<'a> {
19 Provider { ptr, cache }
20 }
21
22 /// Return the Target Package of the provider.
23 pub fn package(&self) -> Package<'a> { Package::new(self.cache, unsafe { self.target_pkg() }) }
24
25 /// Return the Target Version of the provider.
26 pub fn version(&'a self) -> Version<'a> {
27 Version::new(unsafe { self.target_ver() }, self.cache)
28 }
29 }
30
31 impl<'a> fmt::Display for Provider<'a> {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 let version = self.version();
34 write!(
35 f,
36 "{} provides {} {}",
37 self.name(),
38 version.parent().fullname(false),
39 version.version(),
40 )?;
41 Ok(())
42 }
43 }
44
45 impl<'a> fmt::Debug for Provider<'a> {
46 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47 f.debug_struct("Provider")
48 .field("name", &self.name())
49 .field("version", &self.version())
50 .finish()
51 }
52 }
53
54 #[cxx::bridge]
55 pub(crate) mod raw {
56 unsafe extern "C++" {
57 include!("rust-apt/apt-pkg-c/package.h");
58
59 type PrvIterator;
60
61 type PkgIterator = crate::raw::PkgIterator;
62 type VerIterator = crate::raw::VerIterator;
63
64 /// The name of what this provider provides
65 pub fn name(self: &PrvIterator) -> &str;
66
67 /// The version string that this provides
68 pub fn version_str(self: &PrvIterator) -> Result<&str>;
69
70 /// The Target Package that can satisfy this provides
71 ///
72 /// # Safety
73 ///
74 /// If the inner pointer is null segfaults can occur.
75 ///
76 /// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
77 /// is recommended.
78 ///
79 /// The returned UniquePtr cannot outlive the cache.
80 unsafe fn target_pkg(self: &PrvIterator) -> UniquePtr<PkgIterator>;
81
82 /// The Target Version that can satisfy this provides
83 ///
84 /// # Safety
85 ///
86 /// If the inner pointer is null segfaults can occur.
87 ///
88 /// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
89 /// is recommended.
90 ///
91 /// The returned UniquePtr cannot outlive the cache.
92 unsafe fn target_ver(self: &PrvIterator) -> UniquePtr<VerIterator>;
93
•94 pub fn index(self: &PrvIterator) -> usize;
95 /// Clone the pointer.
96 ///
97 /// # Safety
98 ///
99 /// If the inner pointer is null segfaults can occur.
100 ///
101 /// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
102 /// is recommended.
103 ///
104 /// The returned UniquePtr cannot outlive the cache.
105 unsafe fn unique(self: &PrvIterator) -> UniquePtr<PrvIterator>;
106 pub fn raw_next(self: Pin<&mut PrvIterator>);
107 pub fn end(self: &PrvIterator) -> bool;
108 }
109 }