1 use std
::convert
::TryFrom
;
4 use anyhow
::{format_err, Error}
;
6 use pbs_api_types
::{BACKUP_REPO_URL_REGEX, IP_V6_REGEX, Authid, Userid}
;
8 /// Reference remote backup locations
12 pub struct BackupRepository
{
13 /// The user name used for Authentication
14 auth_id
: Option
<Authid
>,
15 /// The host name or IP address
19 /// The name of the datastore
23 impl BackupRepository
{
25 pub fn new(auth_id
: Option
<Authid
>, host
: Option
<String
>, port
: Option
<u16>, store
: String
) -> Self {
26 let host
= match host
{
27 Some(host
) if (IP_V6_REGEX
.regex_obj
)().is_match(&host
) => {
28 Some(format
!("[{}]", host
))
32 Self { auth_id, host, port, store }
35 pub fn auth_id(&self) -> &Authid
{
36 if let Some(ref auth_id
) = self.auth_id
{
40 &Authid
::root_auth_id()
43 pub fn user(&self) -> &Userid
{
44 if let Some(auth_id
) = &self.auth_id
{
45 return auth_id
.user();
51 pub fn host(&self) -> &str {
52 if let Some(ref host
) = self.host
{
58 pub fn port(&self) -> u16 {
59 if let Some(port
) = self.port
{
65 pub fn store(&self) -> &str {
70 impl fmt
::Display
for BackupRepository
{
71 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
72 match (&self.auth_id
, &self.host
, self.port
) {
73 (Some(auth_id
), _
, _
) => write
!(f
, "{}@{}:{}:{}", auth_id
, self.host(), self.port(), self.store
),
74 (None
, Some(host
), None
) => write
!(f
, "{}:{}", host
, self.store
),
75 (None
, _
, Some(port
)) => write
!(f
, "{}:{}:{}", self.host(), port
, self.store
),
76 (None
, None
, None
) => write
!(f
, "{}", self.store
),
81 impl std
::str::FromStr
for BackupRepository
{
84 /// Parse a repository URL.
86 /// This parses strings like `user@host:datastore`. The `user` and
87 /// `host` parts are optional, where `host` defaults to the local
88 /// host, and `user` defaults to `root@pam`.
89 fn from_str(url
: &str) -> Result
<Self, Self::Err
> {
91 let cap
= (BACKUP_REPO_URL_REGEX
.regex_obj
)().captures(url
)
92 .ok_or_else(|| format_err
!("unable to parse repository url '{}'", url
))?
;
95 auth_id
: cap
.get(1).map(|m
| Authid
::try_from(m
.as_str().to_owned())).transpose()?
,
96 host
: cap
.get(2).map(|m
| m
.as_str().to_owned()),
97 port
: cap
.get(3).map(|m
| m
.as_str().parse
::<u16>()).transpose()?
,
98 store
: cap
[4].to_owned(),