1use anyhow::Error;
2
3pub mod search;
4
5pub fn add_package(file: &str, pkg: &str) -> Result<String, Error> {
6 let mut packages = nix_editor::read::readvalue(file, "home.packages")?;
7 let pkg = match pkg.starts_with("pkgs.") {
8 true => pkg.to_string(),
9 false => format!("pkgs.{}", pkg),
10 };
11
12 if packages.contains(&pkg) {
13 return Ok(packages);
14 }
15
16 let entry = format!("\n {}\n]", pkg);
17 packages = packages.replace("\n]", &entry);
18
19 packages = packages.replace("\n", "\n ");
21
22 let output = nix_editor::write::write(file, "home.packages", &packages)?;
23 Ok(output)
24}
25
26pub fn add_packages(file: &str, pkgs: Vec<String>) -> Result<String, Error> {
27 let mut packages = nix_editor::read::readvalue(file, "home.packages")?;
28 let mut entry = String::new();
29 for pkg in pkgs {
30 let pkg = match pkg.starts_with("pkgs.") {
31 true => pkg.to_string(),
32 false => format!("pkgs.{}", pkg),
33 };
34 if packages.contains(&pkg) {
35 continue;
36 }
37 entry.push_str(&format!("\n {}", pkg));
38 }
39 entry.push_str("\n]");
40
41 packages = packages.replace("\n]", &entry);
42
43 packages = packages.replace("\n", "\n ");
45
46 let output = nix_editor::write::write(file, "home.packages", &packages)?;
47 Ok(output)
48}
49
50pub fn remove_package(file: &str, pkg: &str) -> Result<String, Error> {
51 let mut packages = nix_editor::read::readvalue(file, "home.packages")?;
52 let pkg = match pkg.starts_with("pkgs.") {
53 true => pkg.to_string(),
54 false => format!("pkgs.{}", pkg),
55 };
56 let entry = format!(" {}\n", pkg);
57 packages = packages.replace(&entry, "");
58
59 packages = packages.replace("\n", "\n ");
61
62 let output = nix_editor::write::write(file, "home.packages", &packages)?;
63 Ok(output)
64}
65
66pub fn remove_packages(file: &str, pkgs: Vec<String>) -> Result<String, Error> {
67 let mut packages = nix_editor::read::readvalue(file, "home.packages")?;
68 let mut entry = String::new();
69 for pkg in pkgs {
70 let pkg = match pkg.starts_with("pkgs.") {
71 true => pkg.to_string(),
72 false => format!("pkgs.{}", pkg),
73 };
74 entry.push_str(&format!(" {}\n", pkg));
75 }
76 packages = packages.replace(&entry, "");
77
78 packages = packages.replace("\n", "\n ");
80
81 let output = nix_editor::write::write(file, "home.packages", &packages)?;
82 Ok(output)
83}
84
85#[cfg(test)]
86mod tests {
87 use super::*;
88 use std::fs;
89
90 #[test]
91 fn test_add_package() {
92 let home_nix = fs::read_to_string("tests/home.nix").unwrap();
94 let output = add_package(&home_nix, "vim").unwrap();
95 let expected = fs::read_to_string("tests/home-with-vim.nix").unwrap();
96 assert_eq!(output, expected);
97 }
98
99 #[test]
100 fn test_add_package_with_pkgs() {
101 let home_nix = fs::read_to_string("tests/home.nix").unwrap();
103 let output = add_package(&home_nix, "pkgs.vim").unwrap();
104 let expected = fs::read_to_string("tests/home-with-vim.nix").unwrap();
105 assert_eq!(output, expected);
106 }
107
108 #[test]
109 fn test_remove_package() {
110 let home_nix = fs::read_to_string("tests/home-with-vim.nix").unwrap();
112 let output = remove_package(&home_nix, "vim").unwrap();
113 assert!(!output.contains("vim"));
114 }
115
116 #[test]
117 fn test_remove_package_with_pkgs() {
118 let home_nix = fs::read_to_string("tests/home-with-vim.nix").unwrap();
120 let output = remove_package(&home_nix, "pkgs.vim").unwrap();
121 assert!(!output.contains("vim"));
122 }
123
124 #[test]
125 fn test_add_packages() {
126 let home_nix = fs::read_to_string("tests/home.nix").unwrap();
128 let output = add_packages(&home_nix, vec!["vim".into(), "git".into()]).unwrap();
129 let expected = fs::read_to_string("tests/home-with-vim-git.nix").unwrap();
130 assert_eq!(output, expected);
131 }
132
133 #[test]
134 fn test_add_packages_with_pkgs() {
135 let home_nix = fs::read_to_string("tests/home.nix").unwrap();
137 let output = add_packages(&home_nix, vec!["pkgs.vim".into(), "pkgs.git".into()]).unwrap();
138 let expected = fs::read_to_string("tests/home-with-vim-git.nix").unwrap();
139 assert_eq!(output, expected);
140 }
141
142 #[test]
143 fn test_remove_packages() {
144 let home_nix = fs::read_to_string("tests/home-with-vim-git.nix").unwrap();
146 let output = remove_packages(&home_nix, vec!["vim".into(), "git".into()]).unwrap();
147 assert!(!output.contains("vim"));
148 assert!(!output.contains("git"));
149 }
150}