Skip to main content

remote_fs/
types.rs

1use std::path::PathBuf;
2use std::time::SystemTime;
3
4/// Supported remote protocols.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum Protocol {
7    Smb,
8    Ftp,
9    Sftp,
10}
11
12/// File or directory entry returned by listing.
13#[derive(Debug, Clone)]
14pub struct FileEntry {
15    pub path: PathBuf,
16    pub name: String,
17    pub is_dir: bool,
18    pub is_file: bool,
19    pub is_symlink: bool,
20    pub size: u64,
21    pub modified: Option<SystemTime>,
22}
23
24/// Metadata for a remote path.
25#[derive(Debug, Clone)]
26pub struct FileMeta {
27    pub path: PathBuf,
28    pub is_dir: bool,
29    pub is_file: bool,
30    pub is_symlink: bool,
31    pub size: u64,
32    pub modified: Option<SystemTime>,
33}
34
35/// SMB connection configuration.
36#[derive(Debug, Clone)]
37pub struct SmbConfig {
38    pub host: String,
39    pub share: String,
40    pub username: String,
41    pub password: String,
42    pub port: Option<u16>,
43    pub workgroup: Option<String>,
44}
45
46impl SmbConfig {
47    pub fn new(
48        host: impl Into<String>,
49        share: impl Into<String>,
50        username: impl Into<String>,
51        password: impl Into<String>,
52    ) -> Self {
53        Self {
54            host: host.into(),
55            share: share.into(),
56            username: username.into(),
57            password: password.into(),
58            port: None,
59            workgroup: None,
60        }
61    }
62
63    pub fn port(mut self, port: u16) -> Self {
64        self.port = Some(port);
65        self
66    }
67
68    pub fn workgroup(mut self, workgroup: impl Into<String>) -> Self {
69        self.workgroup = Some(workgroup.into());
70        self
71    }
72}
73
74/// FTP connection configuration.
75#[derive(Debug, Clone)]
76pub struct FtpConfig {
77    pub host: String,
78    pub port: u16,
79    pub username: String,
80    pub password: String,
81    pub passive: bool,
82}
83
84impl FtpConfig {
85    pub fn new(
86        host: impl Into<String>,
87        username: impl Into<String>,
88        password: impl Into<String>,
89    ) -> Self {
90        Self {
91            host: host.into(),
92            port: 21,
93            username: username.into(),
94            password: password.into(),
95            passive: true,
96        }
97    }
98
99    pub fn port(mut self, port: u16) -> Self {
100        self.port = port;
101        self
102    }
103
104    pub fn passive(mut self, passive: bool) -> Self {
105        self.passive = passive;
106        self
107    }
108}
109
110/// SFTP connection configuration.
111#[derive(Debug, Clone)]
112pub struct SftpConfig {
113    pub host: String,
114    pub port: u16,
115    pub username: String,
116    pub password: Option<String>,
117    pub private_key_path: Option<PathBuf>,
118}
119
120impl SftpConfig {
121    pub fn with_password(
122        host: impl Into<String>,
123        username: impl Into<String>,
124        password: impl Into<String>,
125    ) -> Self {
126        Self {
127            host: host.into(),
128            port: 22,
129            username: username.into(),
130            password: Some(password.into()),
131            private_key_path: None,
132        }
133    }
134
135    pub fn with_key(
136        host: impl Into<String>,
137        username: impl Into<String>,
138        private_key_path: impl Into<PathBuf>,
139    ) -> Self {
140        Self {
141            host: host.into(),
142            port: 22,
143            username: username.into(),
144            password: None,
145            private_key_path: Some(private_key_path.into()),
146        }
147    }
148
149    pub fn port(mut self, port: u16) -> Self {
150        self.port = port;
151        self
152    }
153}
154
155/// Generic connection configuration.
156#[derive(Debug, Clone)]
157pub enum Config {
158    Smb(SmbConfig),
159    Ftp(FtpConfig),
160    Sftp(SftpConfig),
161}
162
163impl Config {
164    pub fn protocol(&self) -> Protocol {
165        match self {
166            Config::Smb(_) => Protocol::Smb,
167            Config::Ftp(_) => Protocol::Ftp,
168            Config::Sftp(_) => Protocol::Sftp,
169        }
170    }
171}