refactor(test, git): naive nested hashmap

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 <christina@cafkafk.com>
This commit is contained in:
Christina Sørensen 2023-06-27 11:56:04 +02:00
parent 6cebe88131
commit fac64f5d11
Signed by: cafkafk
GPG key ID: CDDC792F655251ED
3 changed files with 52 additions and 30 deletions

View file

@ -16,6 +16,7 @@
use log::{debug, error, info, trace, warn}; use log::{debug, error, info, trace, warn};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::canonicalize; use std::fs::canonicalize;
use std::os::unix::fs::symlink; use std::os::unix::fs::symlink;
use std::path::Path; use std::path::Path;
@ -33,15 +34,20 @@ pub enum RepoFlags {
/// Represents the config.toml file. /// Represents the config.toml file.
#[derive(PartialEq, Debug, Serialize, Deserialize)] #[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct Config { pub struct Config {
pub categories: Vec<Category>, /// map of all categories
///
/// Key should conceptually be seen as the name of the category.
pub categories: HashMap<String, Category>,
pub links: Vec<Links>, pub links: Vec<Links>,
} }
#[derive(PartialEq, Debug, Serialize, Deserialize)] #[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct Category { pub struct Category {
pub name: String,
pub flags: Vec<RepoFlags>, // FIXME: not implemented pub flags: Vec<RepoFlags>, // FIXME: not implemented
pub repos: Vec<GitRepo>, /// map of all categories
///
/// Key should conceptually be seen as the name of the category.
pub repos: HashMap<String, GitRepo>,
} }
/// Contain fields for a single link. /// Contain fields for a single link.
@ -211,12 +217,15 @@ impl Config {
}) })
} }
/// Runs associated function on all repos in config /// Runs associated function on all repos in config
///
/// TODO: need to be made over a generic repo type
fn on_all<F>(&self, f: F) fn on_all<F>(&self, f: F)
where where
F: Fn(&GitRepo), F: Fn(&GitRepo),
{ {
for category in self.categories.iter() { for (_, category) in self.categories.iter() {
for repo in category.repos.iter() { println!("{category:?}");
for (_, repo) in category.repos.iter() {
f(repo); f(repo);
} }
} }
@ -224,11 +233,9 @@ impl Config {
/// Tries to pull all repositories, skips if fail. /// Tries to pull all repositories, skips if fail.
pub fn pull_all(&self) { pub fn pull_all(&self) {
debug!("exectuting pull_all"); debug!("exectuting pull_all");
for category in self.categories.iter() { self.on_all(|repo| {
for repo in category.repos.iter() { repo.pull();
repo.pull(); });
}
}
} }
/// Tries to clone all repositories, skips if fail. /// Tries to clone all repositories, skips if fail.
pub fn clone_all(&self) { pub fn clone_all(&self) {

View file

@ -74,42 +74,57 @@ mod config {
use git::RepoFlags::{Clone, Push}; use git::RepoFlags::{Clone, Push};
use git::{Category, GitRepo}; use git::{Category, GitRepo};
use relative_path::RelativePath; use relative_path::RelativePath;
use std::collections::HashMap;
use std::env::current_dir; use std::env::current_dir;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
#[test] #[test]
fn init_config() { fn init_config() {
let _config = Config { let _config = Config {
categories: vec![], categories: HashMap::new(),
links: vec![], links: vec![],
}; };
} }
#[test] #[test]
fn init_config_populate() { fn init_config_populate() {
let defaultCategory = Category { let defaultCategory = Category {
name: "Default".to_string(),
flags: vec![], flags: vec![],
repos: vec![], repos: HashMap::new(),
}; };
let mut config = Config { let mut config = Config {
categories: vec![defaultCategory], categories: HashMap::new(),
links: vec![], links: vec![],
}; };
for _ in 0..=5 { for _ in 0..=5 {
let category = Category { let category = Category {
name: "ehh".to_string(),
flags: vec![], flags: vec![],
repos: vec![], repos: HashMap::new(),
}; };
} }
for _ in 0..=5 { config
let repo = GitRepo { .categories
name: "test repo".to_string(), .insert(format!("{}", 0).to_string(), defaultCategory);
path: "/tmp".to_string(), for i in 0..=5 {
url: "https://github.com/cafkafk/gg".to_string(), config
flags: vec![Clone, Push], .categories
}; .get_mut(&format!("{}", 0).to_string())
config.categories[0].repos.push(repo); .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(); let yaml = serde_yaml::to_string(&config).unwrap();
println!("{}", yaml); println!("{}", yaml);

View file

@ -1,23 +1,23 @@
categories: categories:
- name: config config:
flags: [] flags: []
repos: repos:
- name: qmk_firmware qmk_firmware:
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
url: git@github.com:cafkafk/qmk_firmware.git url: git@github.com:cafkafk/qmk_firmware.git
flags: [Clone, Push] flags: [Clone, Push]
- name: starship starship:
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
url: https://github.com/starship/starship.git url: https://github.com/starship/starship.git
flags: [Clone, Push] flags: [Clone, Push]
- name: utils utils:
flags: [] flags: []
repos: repos:
- name: gg gg:
path: /home/ces/.dots/ path: /home/ces/.dots/
url: git@github.com:cafkafk/gg.git url: git@github.com:cafkafk/gg.git
flags: [Clone, Push] flags: [Clone, Push]
- name: li li:
path: /home/ces/org/src/git/ path: /home/ces/org/src/git/
url: git@github.com:cafkafk/li.git url: git@github.com:cafkafk/li.git
flags: [Clone, Push] flags: [Clone, Push]