From fac64f5d114092609092c303ecd218ac880fa50d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christina=20S=C3=B8rensen?= Date: Tue, 27 Jun 2023 11:56:04 +0200 Subject: [PATCH] refactor(test, git): naive nested hashmap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an implementation of the nested hashmap architecture, outlined in architecture.md, however, with a hashmap instead of a btreemap. This is ultimately just a sketch. Signed-off-by: Christina Sørensen --- src/git.rs | 27 +++++++++++++++++---------- src/main.rs | 43 +++++++++++++++++++++++++++++-------------- src/test/config.yaml | 12 ++++++------ 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/git.rs b/src/git.rs index cf9aa9a..97fd8d6 100644 --- a/src/git.rs +++ b/src/git.rs @@ -16,6 +16,7 @@ use log::{debug, error, info, trace, warn}; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; use std::fs::canonicalize; use std::os::unix::fs::symlink; use std::path::Path; @@ -33,15 +34,20 @@ pub enum RepoFlags { /// Represents the config.toml file. #[derive(PartialEq, Debug, Serialize, Deserialize)] pub struct Config { - pub categories: Vec, + /// map of all categories + /// + /// Key should conceptually be seen as the name of the category. + pub categories: HashMap, pub links: Vec, } #[derive(PartialEq, Debug, Serialize, Deserialize)] pub struct Category { - pub name: String, pub flags: Vec, // FIXME: not implemented - pub repos: Vec, + /// map of all categories + /// + /// Key should conceptually be seen as the name of the category. + pub repos: HashMap, } /// Contain fields for a single link. @@ -211,12 +217,15 @@ impl Config { }) } /// Runs associated function on all repos in config + /// + /// TODO: need to be made over a generic repo type fn on_all(&self, f: F) where F: Fn(&GitRepo), { - for category in self.categories.iter() { - for repo in category.repos.iter() { + for (_, category) in self.categories.iter() { + println!("{category:?}"); + for (_, repo) in category.repos.iter() { f(repo); } } @@ -224,11 +233,9 @@ impl Config { /// Tries to pull all repositories, skips if fail. pub fn pull_all(&self) { debug!("exectuting pull_all"); - for category in self.categories.iter() { - for repo in category.repos.iter() { - repo.pull(); - } - } + self.on_all(|repo| { + repo.pull(); + }); } /// Tries to clone all repositories, skips if fail. pub fn clone_all(&self) { diff --git a/src/main.rs b/src/main.rs index 07275b9..b029e3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,42 +74,57 @@ mod config { use git::RepoFlags::{Clone, Push}; use git::{Category, GitRepo}; use relative_path::RelativePath; + use std::collections::HashMap; use std::env::current_dir; use std::fs::File; use std::io::prelude::*; #[test] fn init_config() { let _config = Config { - categories: vec![], + categories: HashMap::new(), links: vec![], }; } #[test] fn init_config_populate() { let defaultCategory = Category { - name: "Default".to_string(), flags: vec![], - repos: vec![], + repos: HashMap::new(), }; let mut config = Config { - categories: vec![defaultCategory], + categories: HashMap::new(), links: vec![], }; for _ in 0..=5 { let category = Category { - name: "ehh".to_string(), flags: vec![], - repos: vec![], + repos: HashMap::new(), }; } - for _ in 0..=5 { - let repo = GitRepo { - name: "test repo".to_string(), - path: "/tmp".to_string(), - url: "https://github.com/cafkafk/gg".to_string(), - flags: vec![Clone, Push], - }; - config.categories[0].repos.push(repo); + config + .categories + .insert(format!("{}", 0).to_string(), defaultCategory); + for i in 0..=5 { + config + .categories + .get_mut(&format!("{}", 0).to_string()) + .expect("category not found") + .repos + .insert( + format!("{}", i).to_string(), + GitRepo { + name: "test repo".to_string(), + path: "/tmp".to_string(), + url: "https://github.com/cafkafk/gg".to_string(), + flags: vec![Clone, Push], + }, + ); + // let repo = GitRepo { + // name: "test repo".to_string(), + // path: "/tmp".to_string(), + // url: "https://github.com/cafkafk/gg".to_string(), + // flags: vec![Clone, Push], + // }; } let yaml = serde_yaml::to_string(&config).unwrap(); println!("{}", yaml); diff --git a/src/test/config.yaml b/src/test/config.yaml index f555e14..6543a7f 100644 --- a/src/test/config.yaml +++ b/src/test/config.yaml @@ -1,23 +1,23 @@ categories: - - name: config + config: flags: [] repos: - - name: qmk_firmware + qmk_firmware: path: /home/ces/org/src/git/ url: git@github.com:cafkafk/qmk_firmware.git flags: [Clone, Push] - - name: starship + starship: path: /home/ces/org/src/git/ url: https://github.com/starship/starship.git flags: [Clone, Push] - - name: utils + utils: flags: [] repos: - - name: gg + gg: path: /home/ces/.dots/ url: git@github.com:cafkafk/gg.git flags: [Clone, Push] - - name: li + li: path: /home/ces/org/src/git/ url: git@github.com:cafkafk/li.git flags: [Clone, Push]