From 246534f8c65f47be903beafb6d21a08c32dd45d4 Mon Sep 17 00:00:00 2001 From: Roey Darwish Dror Date: Mon, 11 Jun 2018 08:21:39 +0300 Subject: [PATCH] Intoduce a struct for collecting repositories --- src/git.rs | 24 +++++++++++++++++++++--- src/main.rs | 17 ++++++++--------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/git.rs b/src/git.rs index 4064e068..f351f77c 100644 --- a/src/git.rs +++ b/src/git.rs @@ -9,6 +9,11 @@ pub struct Git { git: Option, } +pub struct Repositories<'a> { + git: &'a Git, + repositories: HashSet, +} + impl Git { pub fn new() -> Self { Self { @@ -56,10 +61,23 @@ impl Git { Ok(None) } +} - pub fn insert_if_valid>(&self, git_repos: &mut HashSet, path: P) { - if let Some(repo) = self.get_repo_root(path) { - git_repos.insert(repo); +impl<'a> Repositories<'a> { + pub fn new(git: &'a Git) -> Self { + Self { + git, + repositories: HashSet::new(), } } + + pub fn insert>(&mut self, path: P) { + if let Some(repo) = self.git.get_repo_root(path) { + self.repositories.insert(repo); + } + } + + pub fn repositories(&self) -> &HashSet { + &self.repositories + } } diff --git a/src/main.rs b/src/main.rs index b35b4804..81ecf87c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,9 +20,8 @@ mod vim; use config::Config; use failure::Error; -use git::Git; +use git::{Git, Repositories}; use report::{Report, Reporter}; -use std::collections::HashSet; use std::env::home_dir; use std::path::{Path, PathBuf}; use std::process::ExitStatus; @@ -80,26 +79,26 @@ fn tpm() -> Option { fn main() -> Result<(), Error> { let git = Git::new(); - let mut git_repos: HashSet = HashSet::new(); + let mut git_repos = Repositories::new(&git); let terminal = Terminal::new(); let mut reports = Report::new(); let config = Config::read()?; - git.insert_if_valid(&mut git_repos, home_path(".emacs.d")); + git_repos.insert(home_path(".emacs.d")); if cfg!(unix) { - git.insert_if_valid(&mut git_repos, home_path(".zshrc")); - git.insert_if_valid(&mut git_repos, home_path(".oh-my-zsh")); - git.insert_if_valid(&mut git_repos, home_path(".tmux")); + git_repos.insert(home_path(".zshrc")); + git_repos.insert(home_path(".oh-my-zsh")); + git_repos.insert(home_path(".tmux")); } if let Some(custom_git_repos) = config.git_repos() { for git_repo in custom_git_repos { - git.insert_if_valid(&mut git_repos, git_repo); + git_repos.insert(git_repo); } } - for repo in git_repos { + for repo in git_repos.repositories() { terminal.print_separator(format!("Pulling {}", repo)); if let Some(success) = git.pull(&repo).ok().and_then(|i| i) { success.report(format!("git: {}", repo), &mut reports);