summaryrefslogtreecommitdiff
path: root/src/protocol.rs
blob: 7bfec51faaa922d93ab1e0020ea4fd800bff1542 (plain)
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
pub const PREFIX: &str = "__nonstd_ext_no_ack_";

fn to_no_ack_service_name<T: AsRef<str>>(original_name: T) -> String {
    PREFIX.to_owned() + original_name.as_ref()
}

fn parse_service_name(service_name: &str) -> (&str, bool) {
    if service_name.starts_with(PREFIX) {
        (&service_name[PREFIX.len()..], true)
    } else {
        (service_name, false)
    }
}

pub struct ServiceName<'a> {
    pub service_name: &'a str,
    pub no_ack: bool,
}

impl<'a> ServiceName<'a> {
    pub fn from(s: &'a str) -> Self {
        let (service_name, no_ack) = parse_service_name(s);
        ServiceName {
            service_name,
            no_ack,
        }
    }
    pub fn to_string(&self) -> String {
        if self.no_ack {
            to_no_ack_service_name(self.service_name).to_owned()
        } else {
            self.service_name.to_string()
        }
    }
}