This refactors a huge chunk of the code base to make it more maintainable.

Main points:

* Proper separation between bin and lib. Bin handles argument parsing &
  validation and (most of) the output. Lib provides interfaces for all
  opreations.

* Before, libgit2 internals were literred throughout the codebase,
  mainly the `Repository` struct and `git2::Error` in Results. They
  library is now properly wrapped in `repo.rs`, which exposes only the
  required functionality. It also standardizes the Error messages
  (they're just Strings for now) and handles stuff like the copious
  usage of Options to wrap maybe-invalid-utf-8 values. The program will
  still panic on non-utf-8 Strings e.g. in git remotes, but I guess this
  is acceptable. If you actually manage to hit this case, I promise I'll
  fix it :D

* Many unwraps() are now gone and properly handled.

* The table printing functionality is now confined to `table.rs`,
  instead of passing tables as parameters through the whole program.
This commit is contained in:
2021-11-30 18:11:33 +01:00
parent da601c2d5f
commit f0c8805cf3
9 changed files with 1629 additions and 1278 deletions

View File

@@ -1,18 +1,48 @@
use serde::{Deserialize, Serialize};
use super::repo::Repo;
use super::repo::RepoConfig;
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub trees: Vec<Tree>,
pub trees: Trees,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Trees(Vec<Tree>);
impl Trees {
pub fn to_config(self) -> Config {
Config { trees: self }
}
pub fn from_vec(vec: Vec<Tree>) -> Self {
Trees(vec)
}
pub fn as_vec(self) -> Vec<Tree> {
self.0
}
pub fn as_vec_ref(&self) -> &Vec<Tree> {
self.0.as_ref()
}
}
impl Config {
pub fn as_toml(&self) -> Result<String, String> {
match toml::to_string(self) {
Ok(toml) => Ok(toml),
Err(error) => Err(error.to_string()),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Tree {
pub root: String,
pub repos: Option<Vec<Repo>>,
pub repos: Option<Vec<RepoConfig>>,
}
pub fn read_config(path: &str) -> Result<Config, String> {