From 5ba2d5560f75c461c90aff6ef5f89c9be573c217 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Wed, 26 Jun 2024 17:30:52 -0700 Subject: [PATCH] feat: implement identifier matching system --- src/dartfile.rs | 2 +- src/dartgun.rs | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/dartfile.rs b/src/dartfile.rs index 15348f5..1670a64 100644 --- a/src/dartfile.rs +++ b/src/dartfile.rs @@ -88,7 +88,7 @@ impl DotfileRaw { /// The configuration object parsed from the `config` field in `dartgun.toml` #[derive(Debug)] pub struct Machine { - identifiers: Vec, + pub identifiers: Vec, } impl Machine { diff --git a/src/dartgun.rs b/src/dartgun.rs index f480716..b11d238 100644 --- a/src/dartgun.rs +++ b/src/dartgun.rs @@ -1,17 +1,25 @@ /// Utilities for translating Dartfiles into actual actions on the system. 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 { - pub fn create_symlink(&self) -> Result<(), std::io::Error> { - symlink(self.location.canonicalize()?, &self.destination) + pub fn create_symlink(&self, machine_identifiers: &[String]) -> Result<(), std::io::Error> { + if have_common_elements(&self.identifiers, machine_identifiers) { + return symlink(self.location.canonicalize()?, &self.destination); + } + Ok(()) } } impl Dartfile { pub fn create_symlinks(&self) -> Result<(), std::io::Error> { for dot in self.dots.iter() { - dot.create_symlink()? + dot.create_symlink(&self.machine.identifiers)? } Ok(()) }