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 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<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>,
}
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct Category {
pub name: String,
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.
@ -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<F>(&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() {
self.on_all(|repo| {
repo.pull();
}
}
});
}
/// Tries to clone all repositories, skips if fail.
pub fn clone_all(&self) {

View file

@ -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 {
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],
};
config.categories[0].repos.push(repo);
},
);
// 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);

View file

@ -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]