feat: implement identifier matching system

This commit is contained in:
Youwen Wu 2024-06-26 17:30:52 -07:00
parent 4e25e59422
commit 5ba2d5560f
Signed by: youwen5
GPG key ID: 865658ED1FE61EC3
2 changed files with 13 additions and 5 deletions

View file

@ -88,7 +88,7 @@ impl DotfileRaw {
/// The configuration object parsed from the `config` field in `dartgun.toml` /// The configuration object parsed from the `config` field in `dartgun.toml`
#[derive(Debug)] #[derive(Debug)]
pub struct Machine { pub struct Machine {
identifiers: Vec<String>, pub identifiers: Vec<String>,
} }
impl Machine { impl Machine {

View file

@ -1,17 +1,25 @@
/// Utilities for translating Dartfiles into actual actions on the system. /// Utilities for translating Dartfiles into actual actions on the system.
use crate::dartfile::{Dartfile, Dotfile}; use crate::dartfile::{Dartfile, Dotfile};
use std::os::unix::fs::symlink; use std::{collections::HashSet, os::unix::fs::symlink};
fn have_common_elements(vec1: &[String], vec2: &[String]) -> bool {
let set: HashSet<_> = vec1.iter().collect();
vec2.iter().any(|item| set.contains(item))
}
impl Dotfile { impl Dotfile {
pub fn create_symlink(&self) -> Result<(), std::io::Error> { pub fn create_symlink(&self, machine_identifiers: &[String]) -> Result<(), std::io::Error> {
symlink(self.location.canonicalize()?, &self.destination) if have_common_elements(&self.identifiers, machine_identifiers) {
return symlink(self.location.canonicalize()?, &self.destination);
}
Ok(())
} }
} }
impl Dartfile { impl Dartfile {
pub fn create_symlinks(&self) -> Result<(), std::io::Error> { pub fn create_symlinks(&self) -> Result<(), std::io::Error> {
for dot in self.dots.iter() { for dot in self.dots.iter() {
dot.create_symlink()? dot.create_symlink(&self.machine.identifiers)?
} }
Ok(()) Ok(())
} }