pacmanconf/
options.rs

1use crate::{pacmanconf, Config, Error};
2
3/// The options struct allows you to change settings prior to building.
4#[derive(Debug, Clone, PartialEq, Eq, Default)]
5pub struct Options {
6    conf_binrary: Option<String>,
7    pacman_conf: Option<String>,
8    root_dir: Option<String>,
9}
10
11impl Config {
12    /// Creates a new empty Options instance. This allows you to change parsing options prior to
13    /// parsing the config file.
14    pub fn options() -> Options {
15        Options::new()
16    }
17}
18
19impl Options {
20    /// Creates a new empty Options instance. This allows you to change parsing options prior to
21    /// parsing the config file.
22    pub fn new() -> Self {
23        Default::default()
24    }
25
26    /// Configure the path of the pacman-conf helper utility.
27    pub fn pacman_conf_bin<S: Into<String>>(&mut self, s: S) -> &mut Self {
28        self.conf_binrary = Some(s.into());
29        self
30    }
31
32    /// Configure the path for the pacman config file. Not setting this
33    /// will cause the system default to be used.
34    pub fn pacman_conf<S: Into<String>>(&mut self, s: S) -> &mut Self {
35        self.pacman_conf = Some(s.into());
36        self
37    }
38
39    /// Configures pacman's  root directory/
40    pub fn root_dir<S: Into<String>>(&mut self, s: S) -> &mut Self {
41        self.root_dir = Some(s.into());
42        self
43    }
44
45    /// Read the config file into a config instance.
46    pub fn read(&self) -> Result<Config, Error> {
47        pacmanconf::Config::with_opts(
48            self.conf_binrary.as_ref(),
49            self.pacman_conf.as_ref(),
50            self.root_dir.as_ref(),
51        )
52    }
53
54    /// Expand and dump the config file into a string.
55    pub fn expand(&self) -> Result<String, Error> {
56        pacmanconf::Config::expand_with_opts(
57            self.conf_binrary.as_ref(),
58            self.pacman_conf.as_ref(),
59            self.root_dir.as_ref(),
60        )
61    }
62}