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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::{fmt::Display, ops::Add};

use thiserror::Error;

use crate::{DomainName, FullyQualifiedDomainName, PartiallyQualifiedDomainName};

/// Segment of a domain.
///
/// This is the part between dots.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DomainSegment(String);

impl DomainSegment {
    /// Constructs a new DomainSegment without checking the validity of it.
    pub const fn new_unchecked(segment: String) -> Self {
        DomainSegment(segment)
    }

    /// Length in characters of the domain segment.
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true if the segment is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    // Returns true if the segment is equal to "*"
    pub fn is_wildcard(&self) -> bool {
        self.0 == "*"
    }
}

/// Produced when attempting to construct a [`DomainSegment`] from
/// an invalid string.
#[derive(Error, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DomainSegmentError {
    /// Domain name segments can contain hyphens, but crucially:
    ///
    /// * Not at the beginning of a segment.
    /// * Not at the end of a segment.
    /// * Not at the 3rd and 4th position *simultaneously* (used for [Punycode encoding](https://en.wikipedia.org/wiki/Punycode))
    #[error("illegal hyphen at position {0}")]
    IllegalHyphen(usize),
    /// Segment contains invalid character.
    #[error("invalid character {0}")]
    InvalidCharacter(char),
    /// Domain segment is longer than the permitted 63 characters.
    #[error("segment too long {0} > 63")]
    TooLong(usize),
    /// Domain segment is empty.
    #[error("segment is an empty string")]
    EmptyString,
    /// Domain segments can be wildcards, but must then *only* contain the wildcard.
    #[error("wildcard segments must have length 1")]
    NonStandaloneWildcard,
}

const VALID_CHARACTERS: &str = "_-0123456789abcdefghijklmnopqrstuvwxyz*";

impl TryFrom<&str> for DomainSegment {
    type Error = DomainSegmentError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let value = value.to_ascii_lowercase();

        if value.is_empty() {
            return Err(DomainSegmentError::EmptyString);
        }

        if value.len() > 63 {
            return Err(DomainSegmentError::TooLong(value.len()));
        }

        if value.contains('*') && value.len() != 1 {
            return Err(DomainSegmentError::NonStandaloneWildcard);
        }

        if let Some(character) = value.chars().find(|c| !VALID_CHARACTERS.contains(*c)) {
            return Err(DomainSegmentError::InvalidCharacter(character));
        }

        if value.starts_with('-') {
            return Err(DomainSegmentError::IllegalHyphen(1));
        }

        if value.ends_with('-') {
            return Err(DomainSegmentError::IllegalHyphen(value.len()));
        }

        if value.get(2..4) == Some("--") {
            return Err(DomainSegmentError::IllegalHyphen(3));
        }

        Ok(DomainSegment(value))
    }
}

impl TryFrom<String> for DomainSegment {
    type Error = DomainSegmentError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::try_from(value.as_str())
    }
}

impl Display for DomainSegment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl AsRef<str> for DomainSegment {
    fn as_ref(&self) -> &str {
        self.0.as_str()
    }
}

impl Add for DomainSegment {
    type Output = PartiallyQualifiedDomainName;

    fn add(self, rhs: Self) -> Self::Output {
        PartiallyQualifiedDomainName::from_iter([self, rhs])
    }
}

impl Add<PartiallyQualifiedDomainName> for DomainSegment {
    type Output = PartiallyQualifiedDomainName;

    fn add(self, mut rhs: PartiallyQualifiedDomainName) -> Self::Output {
        rhs.0.insert(0, self);
        rhs
    }
}

impl Add<&PartiallyQualifiedDomainName> for DomainSegment {
    type Output = PartiallyQualifiedDomainName;

    fn add(self, rhs: &PartiallyQualifiedDomainName) -> Self::Output {
        let mut out = rhs.clone();
        out.0.insert(0, self);
        out
    }
}

impl Add<FullyQualifiedDomainName> for DomainSegment {
    type Output = FullyQualifiedDomainName;

    fn add(self, mut rhs: FullyQualifiedDomainName) -> Self::Output {
        rhs.0.insert(0, self);
        rhs
    }
}

impl Add<&FullyQualifiedDomainName> for DomainSegment {
    type Output = FullyQualifiedDomainName;

    fn add(self, rhs: &FullyQualifiedDomainName) -> Self::Output {
        let mut out = rhs.clone();
        out.0.insert(0, self);
        out
    }
}

impl Add<DomainName> for DomainSegment {
    type Output = DomainName;

    fn add(self, rhs: DomainName) -> Self::Output {
        match rhs {
            DomainName::Full(full) => DomainName::Full(self + full),
            DomainName::Partial(partial) => DomainName::Partial(self + partial),
        }
    }
}

impl Add<&DomainName> for DomainSegment {
    type Output = DomainName;

    fn add(self, rhs: &DomainName) -> Self::Output {
        match rhs {
            DomainName::Full(full) => DomainName::Full(self + full),
            DomainName::Partial(partial) => DomainName::Partial(self + partial),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::segment::{DomainSegment, DomainSegmentError};

    #[test]
    fn segment_construction() {
        assert_eq!(DomainSegment::try_from("abcd").unwrap().as_ref(), "abcd");

        assert_eq!(
            DomainSegment::try_from(""),
            Err(DomainSegmentError::EmptyString)
        );
    }

    #[test]
    fn invalid_character() {
        assert_eq!(
            DomainSegment::try_from("ab.cd"),
            Err(DomainSegmentError::InvalidCharacter('.'))
        );
    }

    #[test]
    fn invalid_hyphens() {
        assert_eq!(DomainSegment::try_from("ab-cd").unwrap().as_ref(), "ab-cd");

        assert_eq!(DomainSegment::try_from("abc-d").unwrap().as_ref(), "abc-d");

        assert_eq!(
            DomainSegment::try_from("ab--cd"),
            Err(DomainSegmentError::IllegalHyphen(3))
        );

        assert_eq!(
            DomainSegment::try_from("-abcd"),
            Err(DomainSegmentError::IllegalHyphen(1))
        );

        assert_eq!(
            DomainSegment::try_from("abcd-"),
            Err(DomainSegmentError::IllegalHyphen(5))
        );
    }

    #[test]
    fn wildcards() {
        assert_eq!(DomainSegment::try_from("*").unwrap().as_ref(), "*");

        assert!(DomainSegment::try_from("*").unwrap().is_wildcard())
    }
}