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