Skip to content

Commit

Permalink
Loose locator parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierHecart committed Jun 12, 2020
1 parent 2390e02 commit 8c55028
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions zenoh-protocol/src/link/locator.rs
Expand Up @@ -24,9 +24,14 @@ use zenoh_util::core::{ZError, ZErrorKind};
/*************************************/
/* LOCATOR */
/*************************************/
const SEPARATOR: char = '/';
const PROTO_SEPARATOR: char = '/';
const PORT_SEPARATOR: char = ':';
// Protocol literals
const STR_TCP: &str = "tcp";
// Defaults
const DEFAULT_TRANSPORT: &str = STR_TCP;
const DEFAULT_HOST: &str = "127.0.0.1";
const DEFAULT_PORT: &str = "7447";

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum LocatorProtocol {
Expand All @@ -51,18 +56,23 @@ impl FromStr for Locator {
type Err = ZError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if !s.contains(SEPARATOR) {
let e = format!("Invalid locator: {} - the format should be '<protocol>/<address>'", s);
log::warn!("{}", e);
return zerror!(ZErrorKind::InvalidLocator {
descr: e
})
}
let mut iter = s.split(SEPARATOR);
let proto = iter.next().unwrap();
let addr = iter.next().unwrap();
let split = s.split(PROTO_SEPARATOR).collect::<Vec<&str>>();
let (proto, addr) = match split.len() {
1 => {(DEFAULT_TRANSPORT, s)}
_ => {(split[0], split[1])}
};
match proto {
STR_TCP => {
let split = addr.split(PORT_SEPARATOR).collect::<Vec<&str>>();
let addr = match split.len() {
1 => {
match addr.parse::<u16>() {
Ok(_) => {[DEFAULT_HOST, addr].join(&PORT_SEPARATOR.to_string())} // port only
Err(_) => {[addr, DEFAULT_PORT].join(&PORT_SEPARATOR.to_string())} // host only
}
}
_ => {addr.to_string()}
};
let addr: SocketAddr = match addr.parse() {
Ok(addr) => addr,
Err(e) => {
Expand Down

0 comments on commit 8c55028

Please sign in to comment.