darling_api/
lib.rs

1/// Data about an (un)installation command. This contains the name of the package to install, as well as
2/// a map of properties specified on the command line.
3#[derive(Clone)]
4pub struct InstallationEntry {
5    /// The name of the package. This is a unique identifier, not a human readable string.
6    pub name: String,
7
8    /// Additional properties specified on the command line. These are arbitrary String-String mappings passed as long arguments
9    /// by the user, and are used for distro-specific or package-manager-specific operations. For example, on Arch linux, a user
10    /// may run `darling install joshuto --source=aur` to install a package such as joshuto from the AUR.
11    pub properties: std::collections::HashMap<String, String>,
12}
13
14/// Global immutable data about the current darling session. This is currently almost entirely unused, but various
15/// configurations in the future are going to go here.
16pub struct Context {
17    /// The configuration cative when running darling.
18    pub config: DarlingConfig,
19}
20
21/// The user-defined configuration options.
22pub struct DarlingConfig {
23    /// The location of the darling source on the users machine; `~/.local/share/darling/source` by default.
24    pub source_location: String,
25}
26
27impl std::default::Default for DarlingConfig {
28    fn default() -> Self {
29        Self {
30            source_location: std::env::var("HOME").unwrap() + "/.local/share/darling/source",
31        }
32    }
33}
34
35/// A package manager which gets a darling implementation. This provides the core functionality on how to install,
36/// uninstall, and list packages through darling. Most of these methods when implemented commonly use
37/// `std::process::Command` to install things through shell commands. In the rare case that a Rust API is available
38/// and advantageous for a particular package manager, that of course could be used as well.
39pub trait PackageManager: Send + Sync {
40    /// Returns the name of this package manager. This is a unique all-lowercase identifier that should not conflict
41    /// with any others. It's common to make this the name of the crate, without the `darling-` prefix. For example,
42    /// this could return `"example".to_owned()`, and the crate would be called `darling-example`.
43    ///
44    /// **TODO:** This may change into returning a `&'static str`. This might be easier to handle on the receiving end
45    /// (such as not having to borrow a value that's already borrowed, and we can convert it to owned when necessary),
46    /// but more importantly, it'd help reinforce that this should be an unchanging constant compile-time known value.
47    /// I set this as an owned `String` because they're just generally easier to work with and is the common convention
48    /// for method returns, but if there aren't any bad ramifications then this option should be considered.
49    fn name(&self) -> String;
50
51    /// Installs a package with the given version. If no version is supplied, this should install the latest version.
52    /// Note that this ***does not*** affect the cache file. This simply supplies the system package install command.
53    ///
54    /// # Parameters
55    /// - `context` - The darling context, which provides global immutable information about the program.
56    /// - `package` - The name of the package to install.
57    ///
58    /// # Returns
59    /// An error if the package could not be installed.
60    fn install(&self, context: &Context, package: &InstallationEntry) -> anyhow::Result<()>;
61
62    /// This is run after a single or group of packages are installed. The difference between placing code here and in
63    /// [install] is that when running commands like `load-installed`, which load all installed packages into the
64    /// darling config file, this is only run once after all packages are installed, instead of every time an individual
65    /// package is installed.
66    ///
67    /// This is useful for modules such as the core module which needs to rebuild the source code every time a new
68    /// module is added. With this system, we can just rebuild the source once after all of the modules are added,
69    /// instead of every time each individual module is added.
70    ///
71    /// **Note that this will still be run for individual installations *after* the [install] method**.
72    ///
73    /// This method is optional, and has a default implementation of just `Ok(())`.
74    ///
75    /// # Parameters
76    /// - `context` - The darling context, which provides global immutable information about the program.
77    ///
78    /// # Returns
79    /// An error if anything went wrong in the post-installation process. This is different module-to-module so
80    /// no more information than this can be specified here.
81    fn post_install(&self, _context: &Context) -> anyhow::Result<()> {
82        Ok(())
83    }
84
85    /// Uninstalls a package from the system. This does ***not*** affect the cache file, it simply removes the package
86    /// from the system itself, and `darling-core` will handle removing the package from the cache file.
87    ///
88    /// # Parameters
89    /// - `context` - The darling context, which provides global immutable information about the program.
90    /// - `package` - The name of the package to remove.
91    ///
92    /// # Returns
93    /// An error if the package could not be removed.
94    fn uninstall(&self, context: &Context, package: &InstallationEntry) -> anyhow::Result<()>;
95
96    /// Returns all *explicitly* installed packages on the system; That is, packages which are not dependencies of
97    /// other packages. This **should not** read from a darling file; Instead, darling uses this method to update
98    /// the file when running `darling require-all`
99    ///
100    /// # Parameters
101    /// - `context` - The darling context, which provides global immutable information about the program.
102    ///
103    /// # Returns
104    /// The name and version of each installed package. as a `Vec<(name: String, version: String)>`.
105    fn get_all_explicit(&self, context: &Context) -> anyhow::Result<Vec<(String, String)>>;
106}