src/client.rs: improve library structure and docu

This commit is contained in:
Dietmar Maurer
2019-02-14 11:11:39 +01:00
parent 18a1aa4858
commit 151c6ce27d
6 changed files with 71 additions and 39 deletions

48
src/client/backup_repo.rs Normal file
View File

@ -0,0 +1,48 @@
use failure::*;
use crate::api::schema::*;
use std::sync::Arc;
use lazy_static::lazy_static;
use regex::Regex;
lazy_static! {
// user@host:datastore
pub static ref BACKUP_REPO_URL_REGEX: Regex = Regex::new(r"^(?:(?:([\w@]+)@)?(\w+):)?(\w+)$").unwrap();
pub static ref BACKUP_REPO_URL: Arc<ApiStringFormat> =
ApiStringFormat::Pattern(&BACKUP_REPO_URL_REGEX).into();
}
/// Reference remote backup locations
///
#[derive(Debug)]
pub struct BackupRepository {
/// The user name used for Authentication
pub user: String,
/// The host name or IP address
pub host: String,
/// The name of the datastore
pub store: String,
}
impl BackupRepository {
/// Parse a repository string.
///
/// This parses strings like `user@host:datastore`. The `user` and
/// `host` parts are optional, where `host` defaults to the local
/// host, and `user` defaults to `root@pam`.
pub fn parse(url: &str) -> Result<Self, Error> {
let cap = BACKUP_REPO_URL_REGEX.captures(url)
.ok_or_else(|| format_err!("unable to parse reepository url '{}'", url))?;
Ok(BackupRepository {
user: cap.get(1).map_or("root@pam", |m| m.as_str()).to_owned(),
host: cap.get(2).map_or("localhost", |m| m.as_str()).to_owned(),
store: cap[3].to_owned(),
})
}
}

View File

@ -13,6 +13,13 @@ use nix::dir::Dir;
use crate::catar::encoder::*;
/// Stream implementation to encode and upload .catar archives.
///
/// The hyper client needs an async Stream for file upload, so we
/// spawn an extra thread to encode the .catar data and pipe it to the
/// consumer.
///
/// Note: The currect implementation is not fully ansync and can block.
pub struct CaTarBackupStream {
pipe: Option<std::fs::File>,
buffer: Vec<u8>,

View File

@ -11,6 +11,7 @@ use futures::stream::Stream;
use serde_json::{Value};
use url::percent_encoding::{percent_encode, DEFAULT_ENCODE_SET};
/// HTTP(S) API client
pub struct HttpClient {
username: String,
server: String,