#[derive(Debug, Clone)]
pub struct NixList {
pub open_line: &'static str,
pub close_line: &'static str,
pub item_indent: usize,
pub quoted: bool,
}
impl NixList {
pub fn format_item(&self, pkg: &str) -> String {
let indent = " ".repeat(self.item_indent);
if self.quoted {
format!("{indent}\"{pkg}\"")
} else {
format!("{indent}{pkg}")
}
}
pub fn parse_item(&self, line: &str) -> Option<String> {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with("//") {
return None;
}
if self.quoted {
let stripped = trimmed.trim_start_matches('"');
let end = stripped.find('"')?;
Some(stripped[..end].to_string())
} else {
let word = trimmed.split_whitespace().next()?;
if word.starts_with('[')
|| word.starts_with(']')
|| word.starts_with('{')
|| word.starts_with('}')
|| word.contains('=')
|| word.contains('.')
|| word.contains('/')
{
return None;
}
Some(word.to_string())
}
}
}
pub const NIX_PACKAGES: NixList = NixList {
open_line: "home.packages = with pkgs; [",
close_line: "];",
item_indent: 4,
quoted: false,
};
pub const HOMEBREW_BREWS: NixList = NixList {
open_line: "brews = [",
close_line: "];",
item_indent: 6,
quoted: true,
};
pub const HOMEBREW_CASKS: NixList = NixList {
open_line: "casks = [",
close_line: "];",
item_indent: 6,
quoted: true,
};