Browse Source

Move repository struct around

Samuel W. Flint 5 years ago
parent
commit
cf45359abf
2 changed files with 40 additions and 18 deletions
  1. 7 18
      src/config.rs
  2. 33 0
      src/repository.rs

+ 7 - 18
src/config.rs

@@ -8,12 +8,17 @@ use std::fs::{
 use std::io::Write;
 
 use home::home_dir;
+
+use crate::repository::{
+    Repository
+};
+
 #[derive(Serialize, Deserialize)]
 pub struct Config {
     #[serde(rename(serialize = "repo_type", deserialize = "repo_type"), default)]
     repo_types: HashMap<String, RepoType>,
     #[serde(rename(serialize = "repository", deserialize = "repository"), default)]
-    repositories: HashMap<String, Repository>,
+    pub repositories: HashMap<String, Repository>,
     #[serde(rename(serialize = "action", deserialize = "action"), default)]
     actions: HashMap<String, Action>,
     #[serde(rename(serialize = "group", deserialize = "group"), default)]
@@ -34,22 +39,6 @@ pub struct RepoType {
     outward: String,
 }
 
-#[derive(Serialize, Deserialize)]
-pub struct Repository {
-    #[serde(default)]
-    name: String,
-    #[serde(default)]
-    location: String,
-    #[serde(default)]
-    repo_type: String,
-    #[serde(default)]
-    auto_create: bool,
-    #[serde(default)]
-    disabled: bool,
-    #[serde(default)]
-    options: HashMap<String, String>,
-}
-
 #[derive(Serialize, Deserialize)]
 pub struct Action {
     #[serde(default)]
@@ -89,7 +78,7 @@ pub fn read_configuration_file(filename: &PathBuf) -> Config {
     let text = read_to_string(filename);
     match text {
         Err(_) => {
-            let mut config = Config {
+            let config = Config {
                 repo_types: HashMap::new(),
                 repositories: HashMap::new(),
                 actions: HashMap::new(),

+ 33 - 0
src/repository.rs

@@ -0,0 +1,33 @@
+use serde::{Deserialize, Serialize};
+use std::collections::HashMap;
+use std::fmt;
+
+#[derive(Serialize, Deserialize)]
+pub struct Repository {
+    #[serde(default)]
+    name: String,
+    #[serde(default)]
+    location: String,
+    #[serde(default)]
+    repo_type: String,
+    #[serde(default)]
+    auto_create: bool,
+    #[serde(default)]
+    disabled: bool,
+    #[serde(default)]
+    options: HashMap<String, String>,
+}
+
+impl fmt::Display for Repository {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "Repository {}:\n\tPath: {}\n\tType: {}\n\tDisabled: {}\n\tOptions:\n",
+               self.name,
+               self.location,
+               self.repo_type,
+               self.disabled);
+        for (key, value) in &self.options {
+            write!(f, "\t\t{}: {}\n", key, value);
+        }
+        write!(f, "")
+    }
+}