#[derive(Debug, Clone, Default)]
pub struct FileTransferOptions {
hold_timestamp: bool,
sync_mode: bool,
compress: bool,
mode_sync: bool,
debug_dir: bool,
}
impl FileTransferOptions {
pub fn new() -> Self {
Self::default()
}
pub fn hold_timestamp(mut self, enable: bool) -> Self {
self.hold_timestamp = enable;
self
}
pub fn sync_mode(mut self, enable: bool) -> Self {
self.sync_mode = enable;
self
}
pub fn compress(mut self, enable: bool) -> Self {
self.compress = enable;
self
}
pub fn mode_sync(mut self, enable: bool) -> Self {
self.mode_sync = enable;
self
}
pub fn debug_dir(mut self, enable: bool) -> Self {
self.debug_dir = enable;
self
}
pub(crate) fn to_flags(&self) -> String {
let mut flags = Vec::new();
if self.hold_timestamp {
flags.push("-a");
}
if self.sync_mode {
flags.push("-sync");
}
if self.compress {
flags.push("-z");
}
if self.mode_sync {
flags.push("-m");
}
if self.debug_dir {
flags.push("-b");
}
flags.join(" ")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileTransferDirection {
Send,
Recv,
}
pub(crate) fn validate_path(path: &str) -> bool {
!path.is_empty() && !path.contains('\0')
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_file_options_flags() {
let opts = FileTransferOptions::new()
.hold_timestamp(true)
.compress(true);
assert_eq!(opts.to_flags(), "-a -z");
let opts = FileTransferOptions::new().sync_mode(true).mode_sync(true);
assert_eq!(opts.to_flags(), "-sync -m");
}
#[test]
fn test_validate_path() {
assert!(validate_path("/data/local/tmp/test.txt"));
assert!(validate_path("test.txt"));
assert!(!validate_path(""));
assert!(!validate_path("test\0file"));
}
}