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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
//   ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
use zenoh_core::bail;

use crate::{
    locators::{ArcProperties, Locator},
    split_once,
};

use super::locators::{CONFIG_SEPARATOR, FIELD_SEPARATOR, LIST_SEPARATOR, METADATA_SEPARATOR};

use std::{
    convert::{TryFrom, TryInto},
    iter::FromIterator,
    str::FromStr,
};

/// A `String` that respects the [`EndPoint`] canon form: `<locator>#<config>`, such that `<locator>` is a valid [`Locator`] `<config>` is of the form `<key1>=<value1>;...;<keyN>=<valueN>` where keys are alphabetically sorted.
#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
#[serde(into = "String")]
#[serde(try_from = "String")]
pub struct EndPoint {
    pub locator: Locator,
    pub config: Option<ArcProperties>,
}
impl core::fmt::Display for EndPoint {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", &self.locator)?;
        if let Some(meta) = &self.config {
            let mut iter = meta.iter();
            if let Some((k, v)) = iter.next() {
                write!(f, "{}{}{}{}", CONFIG_SEPARATOR, k, FIELD_SEPARATOR, v)?;
            }
            for (k, v) in iter {
                write!(f, "{}{}{}{}", LIST_SEPARATOR, k, FIELD_SEPARATOR, v)?;
            }
        }
        Ok(())
    }
}
impl From<EndPoint> for String {
    fn from(v: EndPoint) -> String {
        v.to_string()
    }
}

impl EndPoint {
    #[must_use = "returns true on success"]
    pub fn set_addr(&mut self, addr: &str) -> bool {
        unsafe { std::mem::transmute::<_, &mut Locator>(self) }.set_addr(addr)
    }
    pub fn extend_configuration(&mut self, extension: impl IntoIterator<Item = (String, String)>) {
        match self.config.is_some() {
            true => match &mut self.config {
                Some(config) => config.extend(extension),
                None => unsafe { std::hint::unreachable_unchecked() },
            },
            false => {
                self.config =
                    Some(std::collections::HashMap::from_iter(extension.into_iter()).into())
            }
        }
    }
}
impl From<EndPoint> for Locator {
    fn from(val: EndPoint) -> Self {
        val.locator
    }
}
impl From<Locator> for EndPoint {
    fn from(val: Locator) -> Self {
        EndPoint {
            locator: val,
            config: None,
        }
    }
}

impl TryFrom<String> for EndPoint {
    type Error = zenoh_core::Error;
    fn try_from(mut value: String) -> Result<Self, Self::Error> {
        let (l, r) = split_once(&value, CONFIG_SEPARATOR);
        if r.contains(METADATA_SEPARATOR) {
            bail!(
                "{} is a forbidden character in endpoint metadata",
                METADATA_SEPARATOR
            );
        }
        let config = r.parse().ok();
        let locator_len = l.len();
        value.truncate(locator_len);
        Ok(EndPoint {
            locator: value.try_into()?,
            config,
        })
    }
}
impl FromStr for EndPoint {
    type Err = zenoh_core::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (l, r) = split_once(s, CONFIG_SEPARATOR);
        if r.contains(METADATA_SEPARATOR) {
            bail!(
                "{} is a forbidden character in endpoint metadata",
                METADATA_SEPARATOR
            );
        }
        Ok(EndPoint {
            locator: l.parse()?,
            config: r.parse().ok(),
        })
    }
}