dgraph_tonic/client/
endpoints.rs

1use http::Uri;
2use std::convert::TryInto;
3
4///
5/// Helper struct for endpoints input argument in new client function.
6/// Allows to create client with one or more endpoints.
7///
8#[derive(Debug)]
9pub struct Endpoints<S: TryInto<Uri>> {
10    pub(crate) endpoints: Vec<S>,
11}
12
13impl<S: TryInto<Uri>> From<Vec<S>> for Endpoints<S> {
14    fn from(endpoints: Vec<S>) -> Self {
15        Self { endpoints }
16    }
17}
18
19impl<S: TryInto<Uri>> From<S> for Endpoints<S> {
20    fn from(endpoint: S) -> Self {
21        Self {
22            endpoints: vec![endpoint],
23        }
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30
31    #[test]
32    fn from_vector() {
33        let urls = vec!["http://localhost:2379", "http://localhost:22379"];
34        let endpoints = Endpoints::from(urls.clone());
35        assert_eq!(endpoints.endpoints.len(), 2);
36        assert_eq!(endpoints.endpoints[0], urls[0]);
37        assert_eq!(endpoints.endpoints[1], urls[1]);
38    }
39
40    #[test]
41    fn from_str() {
42        let url = "http://localhost:2379";
43        let endpoints = Endpoints::from(url);
44        assert_eq!(endpoints.endpoints.len(), 1);
45        assert_eq!(endpoints.endpoints[0], url);
46    }
47}