hamsando 0.2.0

A simple and type-safe client for the Porkbun API.
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#[cfg(test)]
mod tests;

use std::alloc::LayoutError;
use std::borrow::Borrow;
use std::cmp;
use std::fmt::{self, Display};
use std::hash::{self, Hash};
use std::ops::Deref;
use std::ptr::slice_from_raw_parts;
use std::str::{self, FromStr};

use serde::{Deserialize, Serialize};
use simple_dst::{CloneToUninit, Dst, ToOwned};
use thiserror::Error;

const MAX_DOMAIN_LEN: usize = 253;
const MAX_LABEL_LEN: usize = 63;

/// Errors representing invalid or malformed domain strings.
#[derive(Debug, Error, PartialEq, Eq, Clone)]
pub enum DomainParseError {
    /// The domain is empty.
    #[error("domain is empty")]
    Empty,
    /// The domain contains an empty label.
    #[error("{domain}: domain contains an empty label")]
    EmptyLabel { domain: String },
    /// The domain has a prefix when it shouldn't ([`Root`]).
    #[error("{domain}: domain has a prefix: {prefix}")]
    HasPrefix { domain: String, prefix: String },
    /// The domain has no root.
    #[error("{domain}: domain has no root")]
    MissingRoot { domain: String },
    /// The domain is missing a suffix.
    ///
    /// This case seems to be unreachable with this psl implementation.
    #[error("{domain}: domain is missing a suffix")]
    MissingSuffix { domain: String },
    /// The domain is too long.
    #[error("{domain}: domain is too long")]
    TooLong { domain: String },
    /// The domain contains a too-long label.
    #[error("{domain}: domain contains a too-long label: {label}")]
    TooLongLabel { domain: String, label: String },
    /// The domain has an unknown suffix.
    #[error("{domain}: domain has an unknown suffix: {suffix}")]
    UnknownSuffix { domain: String, suffix: String },
}

/// Errors that can occur when creating a domain instance.
#[derive(Debug, Error, PartialEq, Eq, Clone)]
pub enum DomainCreateError {
    /// The domain string failed to parse.
    #[error(transparent)]
    Parse(#[from] DomainParseError),
    /// Failure to calculate the layout of the domain type.
    ///
    /// This could happen if the length of the input string is almost [`isize::MAX`].
    #[error(transparent)]
    Layout(#[from] LayoutError),
}

/// Gets the not-fully-qualified part of the given domain.
fn get_not_fqdn(s: &str) -> &str {
    s.strip_suffix('.').unwrap_or(s)
}

/// Returns the indices for the `.`s between the prefix and root, and before the suffix.
fn parse_domain(domain: &str) -> Result<(Option<usize>, usize), DomainParseError> {
    let not_fqdn = get_not_fqdn(domain);

    if not_fqdn.is_empty() {
        return Err(DomainParseError::Empty);
    }

    if not_fqdn.len() > MAX_DOMAIN_LEN {
        return Err(DomainParseError::TooLong {
            domain: domain.to_owned(),
        });
    }

    for label in not_fqdn.split('.') {
        if label.is_empty() {
            return Err(DomainParseError::EmptyLabel {
                domain: domain.to_owned(),
            });
        } else if label.len() > MAX_LABEL_LEN {
            return Err(DomainParseError::TooLongLabel {
                domain: domain.to_owned(),
                label: label.to_owned(),
            });
        }
    }

    let suffix =
        psl::suffix(not_fqdn.as_bytes()).ok_or_else(|| DomainParseError::MissingSuffix {
            domain: domain.to_owned(),
        })?;
    #[allow(clippy::expect_used, reason = "PSL shouldn't return invalid UTF-8")]
    let suffix_str = str::from_utf8(suffix.as_bytes())
        .expect("psl crate returned invalid UTF-8 when slicing domain suffix");
    if !suffix.is_known() {
        return Err(DomainParseError::UnknownSuffix {
            domain: domain.to_owned(),
            suffix: suffix_str.to_owned(),
        });
    }

    let suffix_len = suffix_str.len();
    if not_fqdn.len() == suffix_len {
        return Err(DomainParseError::MissingRoot {
            domain: domain.to_owned(),
        });
    }
    let suffix_separator_idx = not_fqdn.len() - suffix_len - 1;
    let without_suffix = &not_fqdn[..suffix_separator_idx];

    Ok((without_suffix.rfind('.'), suffix_separator_idx))
}

/// The root part of a domain name.
// LAYOUT: This struct must have the same layout as [`Domain`] so that it can be used to
// create a [`Root`] without re-allocating.
#[repr(transparent)]
#[derive(Debug, Dst, CloneToUninit, ToOwned)]
#[dst(new_unchecked_vis = pub)]
pub struct Root(Domain);

impl Root {
    /// Parses a string and creates an owned Root.
    ///
    /// # Errors
    ///
    /// Will return an error in case the domain is invalid, contains a prefix, or if an
    /// error occured during allocation.
    pub fn parse(input: &str) -> Result<Box<Self>, DomainCreateError> {
        let (root_separator_idx, suffix_separator_idx) = parse_domain(input)?;
        if let Some(root_separator_idx) = root_separator_idx {
            return Err(DomainCreateError::Parse(DomainParseError::HasPrefix {
                domain: input.to_owned(),
                prefix: input[..root_separator_idx].to_string(),
            }));
        }

        Ok({
            // SAFETY: the internal invariants of the Root type are the same as those of
            // the Domain type, so if the invariants of Root are upheld, so are those of
            // Domain, and you can use new_unchecked.
            let domain =
                unsafe { Domain::new_unchecked(root_separator_idx, suffix_separator_idx, input) }?;
            // SAFETY: Root is #[repr(transparent)], so it is safe to transmute a Domain
            // to it.
            unsafe { Box::from_raw(Box::into_raw(domain) as *mut Self) }
        })
    }

    /// Returns a string representing the domain.
    #[must_use]
    pub fn as_str(&self) -> &str {
        let offset = self.0.root_separator_idx.map_or(0, |i| i + 1);
        &self.0.domain[offset..]
    }

    /// Returns the suffix (TLD) of the domain.
    #[must_use]
    pub fn suffix(&self) -> &str {
        &self.0.domain[self.0.suffix_separator_idx + 1..]
    }

    /// Returns whether the domain is fully-qualified (i.e. ends in a `.`).
    #[must_use]
    pub fn is_fqdn(&self) -> bool {
        self.as_str().ends_with('.')
    }

    // Returns a Root representing the not-fully-qualified part.
    #[must_use]
    pub fn not_fqdn(&self) -> &Self {
        if !self.is_fqdn() {
            return self;
        }

        // SAFETY: the pointer metadata for the Root type is just the length of the
        // string in it, so creating a new pointer with the metadata of the length minus
        // one is safe. Lowering the length by one doesn't influence the stored indices.
        unsafe {
            let ptr = (&raw const *self).cast::<()>();
            // FUTURE: switch to using ptr_from_raw_parts when it has stabilised.
            #[allow(
                clippy::cast_ptr_alignment,
                reason = "the pointer points to a valid value already"
            )]
            &*(slice_from_raw_parts(ptr, self.len() - 1) as *const Self)
        }
    }
}

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

impl Borrow<str> for Root {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl Deref for Root {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl PartialEq for Root {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl Eq for Root {}

impl PartialOrd for Root {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Root {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl Hash for Root {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.as_str().hash(state);
    }
}

impl Display for Root {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

impl FromStr for Box<Root> {
    type Err = DomainCreateError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Root::parse(s)
    }
}

impl TryFrom<&str> for Box<Root> {
    type Error = DomainCreateError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Root::parse(value)
    }
}

impl<'de> Deserialize<'de> for Box<Root> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::Visitor;

        struct RootVisitor;

        impl Visitor<'_> for RootVisitor {
            type Value = Box<Root>;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("a string representing a domain root")
            }

            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Root::parse(v).map_err(E::custom)
            }
        }

        deserializer.deserialize_str(RootVisitor)
    }
}

impl Serialize for Box<Root> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

/// A domain name.
// LAYOUT: This struct must have the same layout as [`Root`] so that it can be used to
// create a [`Root`] without re-allocating.
#[repr(C)]
#[derive(Debug, Dst, CloneToUninit, ToOwned)]
#[dst(new_unchecked_vis = pub)]
pub struct Domain {
    root_separator_idx: Option<usize>,
    suffix_separator_idx: usize,
    #[allow(clippy::struct_field_names, reason = "it's the underlying value")]
    domain: str,
}

impl Domain {
    /// Parses a string and creates an owned Domain.
    ///
    /// # Errors
    ///
    /// Will return an error in case the domain is invalid or if an error occured during
    /// allocation.
    pub fn parse(input: &str) -> Result<Box<Self>, DomainCreateError> {
        let (root_separator_idx, suffix_separator_idx) = parse_domain(input)?;

        // SAFETY: the parse_domain function makes sure that the input is valid, and
        // only returns values that uphold the internal invariants.
        Ok(unsafe { Self::new_unchecked(root_separator_idx, suffix_separator_idx, input) }?)
    }

    /// Returns a string representing the domain.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.domain
    }

    /// Returns the prefix (subdomain) of the domain.
    #[must_use]
    pub fn prefix(&self) -> Option<&str> {
        self.root_separator_idx.map(|i| &self.domain[..i])
    }

    /// Returns the root part of the domain.
    #[must_use]
    pub fn root(&self) -> &Root {
        // SAFETY: Domain and Root have the exact same fields in the same order
        // and are both repr(C), meaning that they have the same layout.
        unsafe { &*((&raw const *self) as *const Root) }
    }

    /// Returns the root part of the domain as a string.
    #[must_use]
    pub fn root_str(&self) -> &str {
        self.root().as_str()
    }

    /// Returns the suffix (TLD) of the domain.
    #[must_use]
    pub fn suffix(&self) -> &str {
        &self.domain[self.suffix_separator_idx + 1..]
    }

    /// Returns whether the domain is fully-qualified (i.e. ends in a `'`).
    #[must_use]
    pub fn is_fqdn(&self) -> bool {
        self.as_str().ends_with('.')
    }

    // Returns a Domain representing the not-fully-qualified part.
    #[must_use]
    pub fn not_fqdn(&self) -> &Self {
        if !self.is_fqdn() {
            return self;
        }

        // SAFETY: the pointer metadata for the Domain type is just the length of the
        // string in it, so creating a new pointer with the metadata of the length minus
        // one is safe. Lowering the length by one doesn't influence the stored indices.
        unsafe {
            let ptr = (&raw const *self).cast::<()>();
            // FUTURE: switch to using ptr_from_raw_parts when it has stabilised.
            #[allow(
                clippy::cast_ptr_alignment,
                reason = "the pointer points to a valid value already"
            )]
            &*(slice_from_raw_parts(ptr, self.len() - 1) as *const Self)
        }
    }
}

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

impl Borrow<str> for Domain {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl Deref for Domain {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl PartialEq for Domain {
    fn eq(&self, other: &Self) -> bool {
        self.as_str() == other.as_str()
    }
}

impl Eq for Domain {}

impl PartialOrd for Domain {
    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Domain {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl Hash for Domain {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.as_str().hash(state);
    }
}

impl Display for Domain {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.as_str().fmt(f)
    }
}

impl FromStr for Box<Domain> {
    type Err = DomainCreateError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Domain::parse(s)
    }
}

impl TryFrom<&str> for Box<Domain> {
    type Error = DomainCreateError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Domain::parse(value)
    }
}

impl<'de> Deserialize<'de> for Box<Domain> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::Visitor;

        struct DomainVisitor;

        impl Visitor<'_> for DomainVisitor {
            type Value = Box<Domain>;

            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                formatter.write_str("a string representing a domain")
            }

            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Domain::parse(v).map_err(E::custom)
            }
        }

        deserializer.deserialize_str(DomainVisitor)
    }
}

impl Serialize for Box<Domain> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}