aur_depends/
cb.rs

1use std::{any, fmt};
2
3use alpm::Db;
4use raur::Raur;
5
6use crate::Resolver;
7
8pub(crate) struct Callback<F: ?Sized>(pub(crate) Option<Box<F>>);
9
10pub(crate) type ProviderCB = Callback<dyn Fn(&str, &[&str]) -> usize + 'static>;
11pub(crate) type GroupCB<'a> = Callback<dyn Fn(&[Group<'a>]) -> Vec<&'a alpm::Package> + 'static>;
12pub(crate) type IsDevelCb = Callback<dyn Fn(&str) -> bool + 'static>;
13
14/// An alpm Db+Group pair passed to the group callback.
15pub struct Group<'a> {
16    /// The db the group belongs to.
17    pub db: &'a Db,
18    /// The group.
19    pub group: &'a alpm::Group,
20}
21
22impl<T: ?Sized> fmt::Debug for Callback<T> {
23    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
24        fmt.write_str(any::type_name::<Self>())
25    }
26}
27
28impl<T: ?Sized> Default for Callback<T> {
29    fn default() -> Self {
30        Callback(None)
31    }
32}
33
34impl<T: ?Sized> Callback<T> {
35    pub fn get(&self) -> Option<&T> {
36        self.0.as_deref()
37    }
38}
39
40impl<'a, 'b, E: std::error::Error + Sync + Send + 'static, H: Raur<Err = E> + Sync>
41    Resolver<'a, 'b, H>
42{
43    /// Set the provider callback
44    ///
45    /// The provider callback will be called any time there is a choice of multiple AUR packages
46    /// that can satisfy a dependency. This callback receives the dependency that we are trying to
47    /// satisfy and a slice of package names satisfying it.
48    ///
49    /// The callback returns returns the index of which package to pick.
50    ///
51    /// Retuning an invalid index will cause a panic.
52    pub fn provider_callback<F: Fn(&str, &[&str]) -> usize + 'static>(mut self, f: F) -> Self {
53        self.provider_callback = Callback(Some(Box::new(f)));
54        self
55    }
56
57    /// Set the group callback
58    ///
59    /// The group callback is called whenever a group is processed. The callback recieves the group
60    /// and returns a list of packages should be installed from the group;
61    ///
62    pub fn group_callback<F: Fn(&[Group<'a>]) -> Vec<&'a alpm::Package> + 'static>(
63        mut self,
64        f: F,
65    ) -> Self {
66        self.group_callback = Callback(Some(Box::new(f)));
67        self
68    }
69
70    /// Set the function for determining if a package is devel.
71    ///
72    /// Devel packages are never skipped when using NEEDED.
73    ///
74    /// By default, no packages are considered devel.
75    pub fn is_devel<F: Fn(&str) -> bool + 'static>(mut self, f: F) -> Self {
76        self.is_devel = Callback(Some(Box::new(f)));
77        self
78    }
79}