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
use core::fmt;
use core::str::FromStr;
use ockam_core::compat::string::{String, ToString};
use ockam_transport_core::TransportError;

#[derive(Clone, Debug, Default)]
pub struct BleAddr {
    pub device_name: String,
    pub local_name: String,
}

impl fmt::Display for BleAddr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.local_name)
    }
}

impl From<&BleAddr> for String {
    fn from(other: &BleAddr) -> Self {
        other.to_string()
    }
}

impl FromStr for BleAddr {
    type Err = ockam_core::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self {
            device_name: s.to_string(),
            local_name: s.to_string(),
        })
    }
}

pub fn parse_ble_addr<S: AsRef<str>>(s: S) -> ockam_core::Result<BleAddr> {
    Ok(s.as_ref()
        .parse()
        .map_err(|_| TransportError::InvalidAddress)?)
}