please_install/
pls_command.rs

1use crate::package::Package;
2use crate::vendor_data::VendorData;
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum PlsCommand {
6    Install,
7    ReinstallAll,
8    Remove,
9    Upgrade,
10    Search,
11    Info,
12    Update,
13    UpgradeAll,
14    List,
15}
16
17impl PlsCommand {
18    pub fn format(self, vendor: VendorData, args: &Vec<Package>, yes: bool, pager: Option<String>) -> String {
19        let args = match self {
20            PlsCommand::Remove |
21            PlsCommand::Info => args.iter()
22                .map(|arg| arg.name.clone())
23                .collect::<Vec<String>>()
24                .join(" "),
25
26            _ => args.iter()
27                .map(|arg| arg.to_string())
28                .collect::<Vec<String>>()
29                .join(" "),
30        };
31
32        match self {
33            PlsCommand::Install |
34            PlsCommand::ReinstallAll => vendor.1[2].to_owned(),
35            PlsCommand::Remove => vendor.1[3].to_owned(),
36            PlsCommand::Upgrade => vendor.1[4].to_owned(),
37            PlsCommand::Info => vendor.1[6].to_owned(),
38            PlsCommand::Update => vendor.1[7].to_owned(),
39            PlsCommand::UpgradeAll => vendor.1[8].to_owned(),
40            PlsCommand::Search => {
41                if let Some(pager) = pager {
42                    format!("{} | {}", vendor.1[5], pager)
43                } else {
44                    vendor.1[5].to_owned()
45                }
46            }
47            PlsCommand::List => {
48                if let Some(pager) = pager {
49                    format!("{} | {}", vendor.1[9], pager)
50                } else {
51                    vendor.1[9].to_owned()
52                }
53            }
54        }
55        .replace("$yes", if yes {vendor.1[1]} else {""})
56        .replace("$args", &args)
57    }
58}