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
//! The module holding the `User` struct

use crate::get::{get_mapping, get_string};
use serde::{Deserialize, Deserializer};
use serde_yaml::Mapping;
use std::path::PathBuf;

/// A user represents a user that can be used to log in to one of the clusters
/// given in the `Cluster` struct. The mapping of which user can log in to which
/// clusters are maintained in the `Context` set found in the `Config` struct.
///
/// Note: The user struct is flattened when compared to its representation in
/// the yaml file. There is no `user` mapping, the values of the `user`
/// mapping are directly accessible on the `User` struct.
#[derive(Debug, Clone)]
pub struct User {
    /// The name given to this user by the user
    pub name: String,
    pub token: Option<String>,
    pub username: Option<String>,
    pub password: Option<String>,

    /// A `PathBuf` representing the client certificate associated with this
    /// user. This is a path to a file on the disk.
    pub client_certificate: Option<PathBuf>,

    /// A string representing the client certificate associated with this
    /// user. This is a base64 encoded string containing the CA data.
    pub client_certificate_data: Option<String>,

    /// A `PathBuf` representing the client key associated with this
    /// user. This is a path to a file on the disk.
    pub client_key: Option<PathBuf>,

    /// A string representing the client key associated with this
    /// user. This is a base64 encoded string containing the CA data.
    pub client_key_data: Option<String>,
}

impl<'de> Deserialize<'de> for User {
    fn deserialize<D>(d: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let map: Mapping = Deserialize::deserialize(d)?;
        let name = get_string(&map, "name")?;
        let user = get_mapping(map, "user")?;

        Ok(User {
            name,
            token: get_string::<D::Error>(&user, "token").ok(),
            username: get_string::<D::Error>(&user, "username").ok(),
            password: get_string::<D::Error>(&user, "password").ok(),
            client_certificate: get_string::<D::Error>(&user, "client-certificate")
                .map(PathBuf::from)
                .ok(),
            client_certificate_data: get_string::<D::Error>(&user, "client-certificate-data").ok(),
            client_key: get_string::<D::Error>(&user, "client-key")
                .map(PathBuf::from)
                .ok(),
            client_key_data: get_string::<D::Error>(&user, "client-key-data").ok(),
        })

        // User::try_from(map)
    }
}

/*
impl TryFrom<Mapping> for User {
    type Error = de::Error;
    fn try_from(map: Mapping) -> Result<Self, Self::Error> {
        let name = get_string(&map, "name")?;
        let user = get_mapping(map, "user")?;

        Ok(User {
            name,
            token: get_string::<D::Error>(&user, "token").ok(),
            username: get_string::<D::Error>(&user, "username").ok(),
            password: get_string::<D::Error>(&user, "password").ok(),
            client_certificate: get_string::<D::Error>(&user, "client-certificate")
                .map(PathBuf::from)
                .ok(),
            client_certificate_data: get_string::<D::Error>(&user, "client-certificate-data").ok(),
            client_key: get_string::<D::Error>(&user, "client-key")
                .map(PathBuf::from)
                .ok(),
            client_key_data: get_string::<D::Error>(&user, "client-key-data").ok(),
        })
    }
}
*/