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

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

/// A cluster represents a cluster that the user knows how to connect to.
///
/// Note: The cluster struct is flattened when compared to its representation in
/// the yaml file. There is no `cluster` mapping, the values of the `cluster`
/// mapping are directly accessible on the `Cluster` struct.
#[derive(Debug, Clone)]
pub struct Cluster {
    /// The name given to the cluster by the user
    pub name: String,

    /// The HTTP address to the server, including protocol
    pub server: String,

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

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

    /// When set to true this is a signal that any certificate checking should
    /// be bypassed by the user agent.
    pub insecure_skip_tls_verify: bool,
}

impl<'de> Deserialize<'de> for Cluster {
    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 cluster = get_mapping(map, "cluster")?;

        Ok(Cluster {
            name,
            server: get_string::<D::Error>(&cluster, "server")?,
            certificate_authority: get_string::<D::Error>(&cluster, "certificate-authority")
                .map(PathBuf::from)
                .ok(),
            certificate_authority_data: get_string::<D::Error>(
                &cluster,
                "certificate-authority-data",
            )
            .ok(),
            insecure_skip_tls_verify: get_bool::<D::Error>(&cluster, "insecure-skip-tls-verify")
                .unwrap_or_default(),
        })

        // Cluster::try_from(map)
    }
}

/*
TODO Write a working TryFrom impl

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

        Ok(Cluster {
            name,
            server: get_string::<D::Error>(&cluster, "server")?,
            certificate_authority: get_string::<D::Error>(&cluster, "certificate-authority")
                .map(PathBuf::from)
                .ok(),
            certificate_authority_data: get_string::<D::Error>(
                &cluster,
                "certificate-authority-data",
            )
            .ok(),
            insecure_skip_tls_verify: get_bool::<D::Error>(&cluster, "insecure-skip-tls-verify")
                .unwrap_or_default(),
        })
    }
}
*/