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
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,
}

impl Configuration {
  pub fn new(hostname: &str) -> Self {
    Configuration {
      hostname: hostname.to_string(),
      // set default port to 22
      port: 22,
      // get current username
      username: whoami::username(),
      // ask for password
      authentication: AuthenticationType::Interactive,
      // set default timeout to 10 seconds
      timeout: 10000,
      // attempt to negotiate compression
      compress: true,
    }
  }

  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
  }
}

#[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);

  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);
  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);
}