]> git.proxmox.com Git - proxmox-backup.git/blame - src/client/backup_repo.rs
rename src/api to src/api_schema
[proxmox-backup.git] / src / client / backup_repo.rs
CommitLineData
151c6ce2
DM
1use failure::*;
2
dc9a007b 3use crate::api_schema::schema::*;
151c6ce2
DM
4
5use std::sync::Arc;
6use lazy_static::lazy_static;
7use regex::Regex;
8
9lazy_static! {
db4868ef 10 /// Regular expression to parse repository URLs
151c6ce2
DM
11 pub static ref BACKUP_REPO_URL_REGEX: Regex = Regex::new(r"^(?:(?:([\w@]+)@)?(\w+):)?(\w+)$").unwrap();
12
db4868ef 13 /// API schema format definition for repository URLs
151c6ce2
DM
14 pub static ref BACKUP_REPO_URL: Arc<ApiStringFormat> =
15 ApiStringFormat::Pattern(&BACKUP_REPO_URL_REGEX).into();
16}
17
18/// Reference remote backup locations
19///
20
21#[derive(Debug)]
22pub struct BackupRepository {
23 /// The user name used for Authentication
24 pub user: String,
25 /// The host name or IP address
26 pub host: String,
27 /// The name of the datastore
28 pub store: String,
29}
30
31impl BackupRepository {
32
db4868ef 33 /// Parse a repository URL.
151c6ce2
DM
34 ///
35 /// This parses strings like `user@host:datastore`. The `user` and
36 /// `host` parts are optional, where `host` defaults to the local
37 /// host, and `user` defaults to `root@pam`.
38 pub fn parse(url: &str) -> Result<Self, Error> {
39
40 let cap = BACKUP_REPO_URL_REGEX.captures(url)
41 .ok_or_else(|| format_err!("unable to parse reepository url '{}'", url))?;
42
43 Ok(BackupRepository {
44 user: cap.get(1).map_or("root@pam", |m| m.as_str()).to_owned(),
45 host: cap.get(2).map_or("localhost", |m| m.as_str()).to_owned(),
46 store: cap[3].to_owned(),
47 })
48 }
49}