ssh_transfer/
configuration.rs

1use crate::authentication::AuthenticationType;
2
3#[derive(Clone, Debug)]
4pub struct Configuration {
5  pub hostname: String,
6  pub port: u16,
7  pub username: String,
8  pub authentication: AuthenticationType,
9  pub timeout: u32,
10  pub compress: bool,
11  pub trust_host: bool,
12}
13
14impl Configuration {
15  pub fn new(hostname: &str) -> Self {
16    Configuration {
17      hostname: hostname.to_string(),
18      // set default port to 22
19      port: 22,
20      // get current username
21      username: whoami::username(),
22      // ask for password
23      authentication: AuthenticationType::Interactive,
24      // set default timeout to 10 seconds
25      timeout: 10000,
26      // attempt to negotiate compression
27      compress: true,
28      // do not accept connection to unknown host
29      trust_host: false,
30    }
31  }
32
33  pub fn with_port(mut self, port: u16) -> Self {
34    self.port = port;
35    self
36  }
37
38  pub fn with_username(mut self, username: &str) -> Self {
39    self.username = username.to_string();
40    self
41  }
42
43  pub fn with_authentication(mut self, authentication: AuthenticationType) -> Self {
44    self.authentication = authentication;
45    self
46  }
47
48  pub fn with_timeout_ms(mut self, timeout: u32) -> Self {
49    self.timeout = timeout;
50    self
51  }
52
53  pub fn with_compression(mut self, compress: bool) -> Self {
54    self.compress = compress;
55    self
56  }
57
58  pub fn with_host_trust(mut self, trust_host: bool) -> Self {
59    self.trust_host = trust_host;
60    self
61  }
62}
63
64#[test]
65pub fn test_configuration() {
66  let configuration = Configuration::new("localhost");
67
68  assert_eq!("localhost", configuration.hostname);
69  assert_eq!(22, configuration.port);
70  assert_eq!(whoami::username(), configuration.username);
71  assert_eq!(
72    AuthenticationType::Interactive,
73    configuration.authentication
74  );
75  assert_eq!(10000, configuration.timeout);
76  assert_eq!(true, configuration.compress);
77  assert_eq!(false, configuration.trust_host);
78
79  let configuration = configuration
80    .with_port(12345)
81    .with_username("user_name")
82    .with_authentication(AuthenticationType::Password("user_password".to_string()))
83    .with_timeout_ms(54321)
84    .with_compression(false)
85    .with_host_trust(true);
86  assert_eq!("localhost", configuration.hostname);
87  assert_eq!(12345, configuration.port);
88  assert_eq!("user_name", configuration.username);
89  assert_eq!(
90    AuthenticationType::Password("user_password".to_string()),
91    configuration.authentication
92  );
93  assert_eq!(54321, configuration.timeout);
94  assert_eq!(false, configuration.compress);
95  assert_eq!(true, configuration.trust_host);
96}