remotefs_smb/client/windows/
credentials.rs

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
#[derive(Debug, Default, Clone)]
pub struct SmbCredentials {
    pub(crate) server: String,
    pub(crate) share: String,
    pub(crate) username: Option<String>,
    pub(crate) password: Option<String>,
}

impl SmbCredentials {
    pub fn new<S: AsRef<str>>(server: S, share: S) -> Self {
        Self {
            server: server.as_ref().to_string(),
            share: share.as_ref().to_string(),
            ..Default::default()
        }
    }

    /// Construct SmbCredentials with the provided username
    pub fn username<S: AsRef<str>>(mut self, username: S) -> Self {
        self.username = Some(username.as_ref().to_string());
        self
    }

    /// Construct SmbCredentials with the provided password
    pub fn password<S: AsRef<str>>(mut self, password: S) -> Self {
        self.password = Some(password.as_ref().to_string());
        self
    }
}

#[cfg(test)]
mod test {

    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn should_init_credentials() {
        let credentials = SmbCredentials::new("localhost", "temp");
        assert_eq!(&credentials.server, "localhost");
        assert_eq!(&credentials.share, "temp");
        assert!(credentials.username.is_none());
        assert!(credentials.password.is_none());
    }

    #[test]
    fn should_construct_credentials() {
        let credentials = SmbCredentials::new("localhost", "temp")
            .username("test")
            .password("foobar");
        assert_eq!(&credentials.server, "localhost");
        assert_eq!(&credentials.share, "temp");
        assert_eq!(credentials.username.as_deref().unwrap(), "test");
        assert_eq!(credentials.password.as_deref().unwrap(), "foobar");
    }
}