rust_apt/iterators/
provider.rs

1use std::fmt;
2
3use cxx::UniquePtr;
4
5use crate::raw::PrvIterator;
6use 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.
12pub struct Provider<'a> {
13	pub(crate) ptr: UniquePtr<PrvIterator>,
14	cache: &'a Cache,
15}
16
17impl<'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
31impl fmt::Display for Provider<'_> {
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
45impl fmt::Debug for Provider<'_> {
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]
55pub(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		#[cxx_name = "Index"]
95		pub fn index(self: &PrvIterator) -> u64;
96		/// Clone the pointer.
97		///
98		/// # Safety
99		///
100		/// If the inner pointer is null segfaults can occur.
101		///
102		/// Using [`crate::raw::IntoRawIter::make_safe`] to convert to an Option
103		/// is recommended.
104		///
105		/// The returned UniquePtr cannot outlive the cache.
106		unsafe fn unique(self: &PrvIterator) -> UniquePtr<PrvIterator>;
107		pub fn raw_next(self: Pin<&mut PrvIterator>);
108		pub fn end(self: &PrvIterator) -> bool;
109	}
110}