]>
git.proxmox.com Git - proxmox-backup.git/blob - src/client/backup_repo.rs
525da9093219f342ca632e86e366800cfe71cc90
5 use proxmox
::api
::const_regex
;
6 use proxmox
::api
::schema
::*;
9 /// Regular expression to parse repository URLs
10 pub BACKUP_REPO_URL_REGEX
= r
"^(?:(?:([\w@]+)@)?([\w\-_.]+):)?(\w+)$";
13 /// API schema format definition for repository URLs
14 pub const BACKUP_REPO_URL
: ApiStringFormat
= ApiStringFormat
::Pattern(&BACKUP_REPO_URL_REGEX
);
16 /// Reference remote backup locations
20 pub struct BackupRepository
{
21 /// The user name used for Authentication
23 /// The host name or IP address
25 /// The name of the datastore
29 impl BackupRepository
{
31 pub fn new(user
: Option
<String
>, host
: Option
<String
>, store
: String
) -> Self {
32 Self { user, host, store }
35 pub fn user(&self) -> &str {
36 if let Some(ref user
) = self.user
{
42 pub fn host(&self) -> &str {
43 if let Some(ref host
) = self.host
{
49 pub fn store(&self) -> &str {
54 impl fmt
::Display
for BackupRepository
{
55 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
56 if let Some(ref user
) = self.user
{
57 write
!(f
, "{}@{}:{}", user
, self.host(), self.store
)
58 } else if let Some(ref host
) = self.host
{
59 write
!(f
, "{}:{}", host
, self.store
)
61 write
!(f
, "{}", self.store
)
66 impl std
::str::FromStr
for BackupRepository
{
69 /// Parse a repository URL.
71 /// This parses strings like `user@host:datastore`. The `user` and
72 /// `host` parts are optional, where `host` defaults to the local
73 /// host, and `user` defaults to `root@pam`.
74 fn from_str(url
: &str) -> Result
<Self, Self::Err
> {
76 let cap
= (BACKUP_REPO_URL_REGEX
.regex_obj
)().captures(url
)
77 .ok_or_else(|| format_err
!("unable to parse repository url '{}'", url
))?
;
80 user
: cap
.get(1).map(|m
| m
.as_str().to_owned()),
81 host
: cap
.get(2).map(|m
| m
.as_str().to_owned()),
82 store
: cap
[3].to_owned(),