3 use anyhow
::{format_err, Error}
;
5 use pbs_api_types
::{Authid, Userid, BACKUP_REPO_URL_REGEX, IP_V6_REGEX}
;
7 /// Reference remote backup locations
11 pub struct BackupRepository
{
12 /// The user name used for Authentication
13 auth_id
: Option
<Authid
>,
14 /// The host name or IP address
18 /// The name of the datastore
22 impl BackupRepository
{
24 auth_id
: Option
<Authid
>,
29 let host
= match host
{
30 Some(host
) if (IP_V6_REGEX
.regex_obj
)().is_match(&host
) => Some(format
!("[{}]", host
)),
41 pub fn auth_id(&self) -> &Authid
{
42 if let Some(ref auth_id
) = self.auth_id
{
46 Authid
::root_auth_id()
49 pub fn user(&self) -> &Userid
{
50 if let Some(auth_id
) = &self.auth_id
{
51 return auth_id
.user();
57 pub fn host(&self) -> &str {
58 if let Some(ref host
) = self.host
{
64 pub fn port(&self) -> u16 {
65 if let Some(port
) = self.port
{
71 pub fn store(&self) -> &str {
76 impl fmt
::Display
for BackupRepository
{
77 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
78 match (&self.auth_id
, &self.host
, self.port
) {
79 (Some(auth_id
), _
, _
) => write
!(
87 (None
, Some(host
), None
) => write
!(f
, "{}:{}", host
, self.store
),
88 (None
, _
, Some(port
)) => write
!(f
, "{}:{}:{}", self.host(), port
, self.store
),
89 (None
, None
, None
) => write
!(f
, "{}", self.store
),
94 impl std
::str::FromStr
for BackupRepository
{
97 /// Parse a repository URL.
99 /// This parses strings like `user@host:datastore`. The `user` and
100 /// `host` parts are optional, where `host` defaults to the local
101 /// host, and `user` defaults to `root@pam`.
102 fn from_str(url
: &str) -> Result
<Self, Self::Err
> {
103 let cap
= (BACKUP_REPO_URL_REGEX
.regex_obj
)()
105 .ok_or_else(|| format_err
!("unable to parse repository url '{}'", url
))?
;
110 .map(|m
| Authid
::try_from(m
.as_str().to_owned()))
112 host
: cap
.get(2).map(|m
| m
.as_str().to_owned()),
113 port
: cap
.get(3).map(|m
| m
.as_str().parse
::<u16>()).transpose()?
,
114 store
: cap
[4].to_owned(),