From a236c36a4f0092ac3c474da3b9940d39de0440a2 Mon Sep 17 00:00:00 2001
From: Chewing_Bever <roosensjef@gmail.com>
Date: Fri, 23 Jun 2023 10:53:17 +0200
Subject: [PATCH] feat: take backup layers as arguments

---
 src/cli.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/src/cli.rs b/src/cli.rs
index d9ea9a9..adf1013 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,6 +1,52 @@
 use crate::server::ServerType;
 use clap::{Args, Parser, Subcommand};
+use std::error::Error;
+use std::fmt;
 use std::path::PathBuf;
+use std::str::FromStr;
+
+#[derive(Clone, Debug)]
+pub struct ManagerConfig {
+    name: String,
+    frequency: u32,
+    chain_len: u64,
+    chains: u64,
+}
+
+#[derive(Debug)]
+pub struct ParseManagerConfigErr;
+
+impl Error for ParseManagerConfigErr {}
+
+impl fmt::Display for ParseManagerConfigErr {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "parse manager config err")
+    }
+}
+
+impl FromStr for ManagerConfig {
+    type Err = ParseManagerConfigErr;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        let splits: Vec<&str> = s.split(',').collect();
+
+        if let [name, frequency, chains, chain_len] = splits[..] {
+            let name: String = name.parse().map_err(|_| ParseManagerConfigErr)?;
+            let frequency: u32 = frequency.parse().map_err(|_| ParseManagerConfigErr)?;
+            let chains: u64 = chains.parse().map_err(|_| ParseManagerConfigErr)?;
+            let chain_len: u64 = chain_len.parse().map_err(|_| ParseManagerConfigErr)?;
+
+            Ok(ManagerConfig {
+                name,
+                chains,
+                chain_len,
+                frequency,
+            })
+        } else {
+            Err(ParseManagerConfigErr)
+        }
+    }
+}
 
 #[derive(Parser)]
 #[command(author, version, about, long_about = None)]
@@ -35,6 +81,11 @@ pub struct Cli {
     )]
     pub backup: PathBuf,
 
+    /// What backup layers to employ, provided as a list of tuples name,frequency,chain_len,chains
+    /// delimited by semicolons (;).
+    #[arg(long, env = "ALEX_LAYERS", global = true, value_delimiter = ';')]
+    pub layers: Vec<ManagerConfig>,
+
     /// Length of a backup chain
     #[arg(
         short = 'l',