use crate::command::traits::CommandBuilder;
use std::convert::AsRef;
use std::ffi::{OsStr, OsString};
use std::path::PathBuf;
#[derive(Clone, Debug, Default)]
pub struct VacuumLoBuilder {
program_dir: Option<PathBuf>,
limit: Option<usize>,
dry_run: bool,
verbose: bool,
version: bool,
help: bool,
host: Option<OsString>,
port: Option<u16>,
username: Option<OsString>,
no_password: bool,
password: bool,
}
impl VacuumLoBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn program_dir<P: Into<PathBuf>>(mut self, path: P) -> Self {
self.program_dir = Some(path.into());
self
}
pub fn limit(mut self, limit: usize) -> Self {
self.limit = Some(limit);
self
}
pub fn dry_run(mut self) -> Self {
self.dry_run = true;
self
}
pub fn verbose(mut self) -> Self {
self.verbose = true;
self
}
pub fn version(mut self) -> Self {
self.version = true;
self
}
pub fn help(mut self) -> Self {
self.help = true;
self
}
pub fn host<S: AsRef<OsStr>>(mut self, host: S) -> Self {
self.host = Some(host.as_ref().to_os_string());
self
}
pub fn port(mut self, port: u16) -> Self {
self.port = Some(port);
self
}
pub fn username<S: AsRef<OsStr>>(mut self, username: S) -> Self {
self.username = Some(username.as_ref().to_os_string());
self
}
pub fn no_password(mut self) -> Self {
self.no_password = true;
self
}
pub fn password(mut self) -> Self {
self.password = true;
self
}
}
impl CommandBuilder for VacuumLoBuilder {
fn get_program(&self) -> &'static OsStr {
"vacuumlo".as_ref()
}
fn get_program_dir(&self) -> &Option<PathBuf> {
&self.program_dir
}
fn get_args(&self) -> Vec<OsString> {
let mut args: Vec<OsString> = Vec::new();
if let Some(limit) = &self.limit {
args.push("--limit".into());
args.push(limit.to_string().into());
}
if self.dry_run {
args.push("--dry-run".into());
}
if self.verbose {
args.push("--verbose".into());
}
if self.version {
args.push("--version".into());
}
if self.help {
args.push("--help".into());
}
if let Some(host) = &self.host {
args.push("--host".into());
args.push(host.into());
}
if let Some(port) = &self.port {
args.push("--port".into());
args.push(port.to_string().into());
}
if let Some(username) = &self.username {
args.push("--username".into());
args.push(username.into());
}
if self.no_password {
args.push("--no-password".into());
}
if self.password {
args.push("--password".into());
}
args
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::command::traits::CommandToString;
use test_log::test;
#[test]
fn test_builder_new() {
let command = VacuumLoBuilder::new().program_dir(".").build();
assert_eq!(
PathBuf::from(".").join("vacuumlo"),
PathBuf::from(command.to_command_string().replace("\"", ""))
);
}
#[test]
fn test_builder() {
let command = VacuumLoBuilder::new()
.limit(100)
.dry_run()
.verbose()
.version()
.help()
.host("localhost")
.port(5432)
.username("postgres")
.no_password()
.password()
.build();
assert_eq!(
r#""vacuumlo" "--limit" "100" "--dry-run" "--verbose" "--version" "--help" "--host" "localhost" "--port" "5432" "--username" "postgres" "--no-password" "--password""#,
command.to_command_string()
);
}
}