]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/client/backup_repo.rs
use const api definitions
[proxmox-backup.git] / src / client / backup_repo.rs
index 400130db9b0090a8d2e1f2af4ad9dc4f07b72362..4b3b5f6c0e0202c7d4e4f078227d5351f2c1863a 100644 (file)
@@ -2,49 +2,80 @@ use failure::*;
 
 use crate::api_schema::*;
 
-use std::sync::Arc;
-use lazy_static::lazy_static;
-use regex::Regex;
+use std::fmt;
 
-lazy_static! {
+const_regex! {
     /// Regular expression to parse repository URLs
-    pub static ref BACKUP_REPO_URL_REGEX: Regex =
-        Regex::new(r"^(?:(?:([\w@]+)@)?([\w\-_.]+):)?(\w+)$").unwrap();
-
-    /// API schema format definition for repository URLs
-    pub static ref BACKUP_REPO_URL: Arc<ApiStringFormat> =
-        ApiStringFormat::Pattern(&BACKUP_REPO_URL_REGEX).into();
+    pub BACKUP_REPO_URL_REGEX = r"^(?:(?:([\w@]+)@)?([\w\-_.]+):)?(\w+)$";
 }
 
+/// API schema format definition for repository URLs
+pub const BACKUP_REPO_URL: ApiStringFormat = ApiStringFormat::Pattern(&BACKUP_REPO_URL_REGEX);
+
 /// Reference remote backup locations
 ///
 
 #[derive(Debug)]
 pub struct BackupRepository {
     /// The user name used for Authentication
-    pub user: String,
+    user: Option<String>,
     /// The host name or IP address
-    pub host: String,
+    host: Option<String>,
     /// The name of the datastore
-    pub store: String,
+    store: String,
 }
 
 impl BackupRepository {
 
+    pub fn user(&self) -> &str {
+        if let Some(ref user) = self.user {
+            return user;
+        }
+        "root@pam"
+    }
+
+    pub fn host(&self) -> &str {
+        if let Some(ref host) = self.host {
+            return host;
+        }
+        "localhost"
+    }
+
+    pub fn store(&self) -> &str {
+        &self.store
+    }
+}
+
+impl fmt::Display for BackupRepository {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+       if let Some(ref user) = self.user {
+           write!(f, "{}@{}:{}", user, self.host(), self.store)
+       } else if let Some(ref host) = self.host {
+           write!(f, "{}:{}", host, self.store)
+       } else {
+           write!(f, "{}", self.store)
+       }
+    }
+}
+
+impl std::str::FromStr for BackupRepository {
+    type Err = Error;
+
     /// Parse a repository URL.
     ///
     /// This parses strings like `user@host:datastore`. The `user` and
     /// `host` parts are optional, where `host` defaults to the local
     /// host, and `user` defaults to `root@pam`.
-    pub fn parse(url: &str) -> Result<Self, Error> {
+    fn from_str(url: &str) -> Result<Self, Self::Err> {
 
-        let cap = BACKUP_REPO_URL_REGEX.captures(url)
+        let cap = (BACKUP_REPO_URL_REGEX.regex_obj)().captures(url)
             .ok_or_else(|| format_err!("unable to parse repository url '{}'", url))?;
 
-        Ok(BackupRepository {
-            user: cap.get(1).map_or("root@pam", |m| m.as_str()).to_owned(),
-            host: cap.get(2).map_or("localhost", |m| m.as_str()).to_owned(),
+        Ok(Self {
+            user: cap.get(1).map(|m| m.as_str().to_owned()),
+            host: cap.get(2).map(|m| m.as_str().to_owned()),
             store: cap[3].to_owned(),
         })
     }
 }
+