]>
Commit | Line | Data |
---|---|---|
cad540e9 | 1 | use std::fmt; |
151c6ce2 | 2 | |
f7d4e4b5 | 3 | use anyhow::{format_err, Error}; |
151c6ce2 | 4 | |
cad540e9 | 5 | use proxmox::api::schema::*; |
151c6ce2 | 6 | |
090decbe | 7 | use crate::api2::types::*; |
151c6ce2 | 8 | |
255f378a DM |
9 | /// API schema format definition for repository URLs |
10 | pub const BACKUP_REPO_URL: ApiStringFormat = ApiStringFormat::Pattern(&BACKUP_REPO_URL_REGEX); | |
11 | ||
151c6ce2 DM |
12 | /// Reference remote backup locations |
13 | /// | |
14 | ||
15 | #[derive(Debug)] | |
16 | pub struct BackupRepository { | |
17 | /// The user name used for Authentication | |
d0a03d40 | 18 | user: Option<String>, |
151c6ce2 | 19 | /// The host name or IP address |
d0a03d40 | 20 | host: Option<String>, |
151c6ce2 | 21 | /// The name of the datastore |
d0a03d40 | 22 | store: String, |
151c6ce2 DM |
23 | } |
24 | ||
25 | impl BackupRepository { | |
26 | ||
25de1c80 DM |
27 | pub fn new(user: Option<String>, host: Option<String>, store: String) -> Self { |
28 | Self { user, host, store } | |
29 | } | |
30 | ||
d0a03d40 DM |
31 | pub fn user(&self) -> &str { |
32 | if let Some(ref user) = self.user { | |
33 | return user; | |
34 | } | |
35 | "root@pam" | |
36 | } | |
37 | ||
38 | pub fn host(&self) -> &str { | |
39 | if let Some(ref host) = self.host { | |
40 | return host; | |
41 | } | |
42 | "localhost" | |
43 | } | |
44 | ||
45 | pub fn store(&self) -> &str { | |
46 | &self.store | |
47 | } | |
874acb70 | 48 | } |
d0a03d40 | 49 | |
874acb70 DM |
50 | impl fmt::Display for BackupRepository { |
51 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
52 | if let Some(ref user) = self.user { | |
53 | write!(f, "{}@{}:{}", user, self.host(), self.store) | |
54 | } else if let Some(ref host) = self.host { | |
55 | write!(f, "{}:{}", host, self.store) | |
56 | } else { | |
57 | write!(f, "{}", self.store) | |
58 | } | |
d0a03d40 | 59 | } |
151c6ce2 | 60 | } |
edd3c8c6 DM |
61 | |
62 | impl std::str::FromStr for BackupRepository { | |
63 | type Err = Error; | |
64 | ||
65 | /// Parse a repository URL. | |
66 | /// | |
67 | /// This parses strings like `user@host:datastore`. The `user` and | |
68 | /// `host` parts are optional, where `host` defaults to the local | |
69 | /// host, and `user` defaults to `root@pam`. | |
70 | fn from_str(url: &str) -> Result<Self, Self::Err> { | |
71 | ||
255f378a | 72 | let cap = (BACKUP_REPO_URL_REGEX.regex_obj)().captures(url) |
edd3c8c6 DM |
73 | .ok_or_else(|| format_err!("unable to parse repository url '{}'", url))?; |
74 | ||
75 | Ok(Self { | |
76 | user: cap.get(1).map(|m| m.as_str().to_owned()), | |
77 | host: cap.get(2).map(|m| m.as_str().to_owned()), | |
78 | store: cap[3].to_owned(), | |
79 | }) | |
80 | } | |
81 | } |