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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use super::{Buffer, Checked, Code, Codec, Protocol};
use crate::proto::{DnsAddr, Node, Project, Secure, Service, Space, Tcp, Worker};
use crate::{Error, ProtoValue};
use core::fmt;
use unsigned_varint::decode;

pub struct StdCodec;

impl Codec for StdCodec {
    fn split_str<'a>(
        &self,
        _prefix: &str,
        input: &'a str,
    ) -> Result<(Checked<&'a str>, &'a str), Error> {
        if let Some(p) = input.find('/') {
            let (x, y) = input.split_at(p);
            Ok((Checked(x), y))
        } else {
            Ok((Checked(input), ""))
        }
    }

    fn split_bytes<'a>(
        &self,
        code: Code,
        input: &'a [u8],
    ) -> Result<(Checked<&'a [u8]>, &'a [u8]), Error> {
        match code {
            #[cfg(feature = "std")]
            crate::proto::Ip4::CODE => {
                if input.len() < 4 {
                    return Err(Error::required_bytes(crate::proto::Ip4::CODE, 4));
                }
                let (x, y) = input.split_at(4);
                Ok((Checked(x), y))
            }
            #[cfg(feature = "std")]
            crate::proto::Ip6::CODE => {
                if input.len() < 16 {
                    return Err(Error::required_bytes(crate::proto::Ip6::CODE, 16));
                }
                let (x, y) = input.split_at(16);
                Ok((Checked(x), y))
            }
            Tcp::CODE => {
                if input.len() < 2 {
                    return Err(Error::required_bytes(Tcp::CODE, 2));
                }
                let (x, y) = input.split_at(2);
                Ok((Checked(x), y))
            }
            c @ Worker::CODE
            | c @ DnsAddr::CODE
            | c @ Service::CODE
            | c @ Node::CODE
            | c @ Project::CODE
            | c @ Space::CODE
            | c @ Secure::CODE => {
                let (len, input) = decode::usize(input)?;
                if input.len() < len {
                    return Err(Error::required_bytes(c, len));
                }
                let (x, y) = input.split_at(len);
                Ok((Checked(x), y))
            }
            _ => Err(Error::unregistered(code)),
        }
    }

    fn is_valid_bytes(&self, code: Code, input: Checked<&[u8]>) -> bool {
        match code {
            Worker::CODE => Worker::read_bytes(input).is_ok(),
            #[cfg(feature = "std")]
            crate::proto::Ip4::CODE => crate::proto::Ip4::read_bytes(input).is_ok(),
            #[cfg(feature = "std")]
            crate::proto::Ip6::CODE => crate::proto::Ip6::read_bytes(input).is_ok(),
            Tcp::CODE => Tcp::read_bytes(input).is_ok(),
            DnsAddr::CODE => DnsAddr::read_bytes(input).is_ok(),
            Service::CODE => Service::read_bytes(input).is_ok(),
            Node::CODE => Node::read_bytes(input).is_ok(),
            Project::CODE => Project::read_bytes(input).is_ok(),
            Space::CODE => Space::read_bytes(input).is_ok(),
            Secure::CODE => Secure::read_bytes(input).is_ok(),
            _ => false,
        }
    }

    fn write_bytes(&self, val: &ProtoValue, buf: &mut dyn Buffer) -> Result<(), Error> {
        match val.code() {
            Worker::CODE => Worker::read_bytes(val.data())?.write_bytes(buf),
            #[cfg(feature = "std")]
            crate::proto::Ip4::CODE => crate::proto::Ip4::read_bytes(val.data())?.write_bytes(buf),
            #[cfg(feature = "std")]
            crate::proto::Ip6::CODE => crate::proto::Ip6::read_bytes(val.data())?.write_bytes(buf),
            Tcp::CODE => Tcp::read_bytes(val.data())?.write_bytes(buf),
            DnsAddr::CODE => DnsAddr::read_bytes(val.data())?.write_bytes(buf),
            Service::CODE => Service::read_bytes(val.data())?.write_bytes(buf),
            Node::CODE => Node::read_bytes(val.data())?.write_bytes(buf),
            Project::CODE => Project::read_bytes(val.data())?.write_bytes(buf),
            Space::CODE => Space::read_bytes(val.data())?.write_bytes(buf),
            Secure::CODE => Secure::read_bytes(val.data())?.write_bytes(buf),
            code => return Err(Error::unregistered(code)),
        }
        Ok(())
    }

    fn transcode_str(
        &self,
        prefix: &str,
        value: Checked<&str>,
        buf: &mut dyn Buffer,
    ) -> Result<(), Error> {
        match prefix {
            Worker::PREFIX => {
                Worker::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            #[cfg(feature = "std")]
            crate::proto::Ip4::PREFIX => {
                crate::proto::Ip4::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            #[cfg(feature = "std")]
            crate::proto::Ip6::PREFIX => {
                crate::proto::Ip6::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            Tcp::PREFIX => {
                Tcp::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            DnsAddr::PREFIX => {
                DnsAddr::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            Service::PREFIX => {
                Service::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            Node::PREFIX => {
                Node::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            Project::PREFIX => {
                Project::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            Space::PREFIX => {
                Space::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            Secure::PREFIX => {
                Secure::read_str(value)?.write_bytes(buf);
                Ok(())
            }
            _ => Err(Error::unregistered_prefix(prefix)),
        }
    }

    fn transcode_bytes(
        &self,
        code: Code,
        value: Checked<&[u8]>,
        f: &mut fmt::Formatter,
    ) -> Result<(), Error> {
        match code {
            Worker::CODE => {
                Worker::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            #[cfg(feature = "std")]
            crate::proto::Ip4::CODE => {
                crate::proto::Ip4::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            #[cfg(feature = "std")]
            crate::proto::Ip6::CODE => {
                crate::proto::Ip6::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            Tcp::CODE => {
                Tcp::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            DnsAddr::CODE => {
                DnsAddr::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            Service::CODE => {
                Service::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            Node::CODE => {
                Node::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            Project::CODE => {
                Project::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            Space::CODE => {
                Space::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            Secure::CODE => {
                Secure::read_bytes(value)?.write_str(f)?;
                Ok(())
            }
            _ => Err(Error::unregistered(code)),
        }
    }
}