hostaddr 0.2.4

Type-safe, validated DNS domain names, hosts, and host addresses with IDNA/punycode support. Generic over storage (`String`, `Arc<str>`, `Vec<u8>`, stack-allocated `Buffer`, etc.) and `no_std`/`no-alloc` compatible.
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
//! Stack-allocated buffer for storing validated domain names.
//!
//! This module provides the `Buffer` type, a fixed-size buffer designed for
//! storing domain names without heap allocation. It is ideal for `no_std` and
//! `no-alloc` environments.
//!
//! ## Design
//!
//! The `Buffer` type uses a 255-byte array to store domain names:
//! - 253 bytes for the domain name content (max DNS domain length)
//! - 1 byte for a trailing dot (for FQDNs)
//! - 1 byte to store the actual length
//!
//! This design allows `Buffer` to be used in const contexts and on the stack
//! without requiring any heap allocation.
//!
//! ## Usage
//!
//! The `Buffer` type is typically used with the `Domain<Buffer>` type:
//!
//! ```rust
//! use hostaddr::{Domain, Buffer};
//!
//! // Create a stack-allocated domain (no heap allocation)
//! let domain: Domain<Buffer> = Domain::try_from("example.com").unwrap();
//!
//! // Access as str or bytes
//! assert_eq!(domain.as_inner().as_str(), "example.com");
//! assert_eq!(domain.as_inner().as_bytes(), b"example.com");
//! ```
//!
//! ## Converting to Other Types
//!
//! `Buffer` can be converted to various string and byte types:
//!
//! ```rust
//! # #[cfg(any(feature = "std", feature = "alloc"))]
//! # {
//! use hostaddr::{Domain, Buffer};
//!
//! let domain: Domain<Buffer> = Domain::try_from("example.com").unwrap();
//!
//! // Convert to String
//! let s: String = domain.into_inner().into();
//!
//! // Convert to Vec<u8>
//! let domain: Domain<Buffer> = Domain::try_from("example.com").unwrap();
//! let v: Vec<u8> = domain.into_inner().into();
//! # }
//! ```

use core::borrow::Borrow;

/// An immutable buffer which contains a valid domain.
///
/// The internal storage is a `[u8; 255]` array with the following layout:
/// - bytes 0-253: domain name content
/// - byte 254: length of the domain name (0-254)
///
/// This fixed-size design allows the `Buffer` to be used in `no_std` and
/// `no-alloc` environments without requiring heap allocation.
///
/// ## Example
///
/// ```rust
/// use hostaddr::{Domain, Buffer};
///
/// let domain: Domain<Buffer> = Domain::try_from("example.com").unwrap();
/// assert_eq!(domain.as_inner().as_str(), "example.com");
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, derive_more::Display)]
#[repr(transparent)]
#[display("{}", self.as_str())]
pub struct Buffer {
  /// 253 bytes for possible domain name, 1 byte for '.', 1 byte for length.
  buf: [u8; 255],
}

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

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

#[allow(unused)]
#[cfg(any(
  feature = "std",
  feature = "alloc",
  feature = "smol_str_0_3",
  feature = "triomphe_0_1",
  feature = "bytes_1",
  feature = "tinyvec_1",
  feature = "smallvec_1",
  feature = "bytes_1"
))]
macro_rules! impl_from_domain_buffer {
  ($($as:ident(
    $($into:ident -> $ty:ty), +$(,)?
  )), +$(,)?) => {
    $(
      $(
        impl_from_domain_buffer!($as -> $into -> $ty);
      )*
    )*
  };
  ($as:ident -> $into:ident -> $ty:ty) => {
    impl From<Buffer> for $ty {
      fn from(value: Buffer) -> Self {
        value.$as().$into()
      }
    }
  };
}

#[cfg(feature = "smol_str_0_3")]
impl_from_domain_buffer!(as_str -> into -> smol_str_0_3::SmolStr);
#[cfg(feature = "triomphe_0_1")]
impl_from_domain_buffer!(as_str -> into -> triomphe_0_1::Arc<str>);
#[cfg(feature = "triomphe_0_1")]
impl_from_domain_buffer!(as_bytes -> into -> triomphe_0_1::Arc<[u8]>);
#[cfg(feature = "bytes_1")]
const _: () = {
  use bytes_1::Bytes;

  impl From<Buffer> for Bytes {
    fn from(value: Buffer) -> Self {
      Bytes::copy_from_slice(value.as_bytes())
    }
  }
};
#[cfg(feature = "tinyvec_1")]
const _: () = {
  use tinyvec_1::TinyVec;

  impl<const N: usize> From<Buffer> for TinyVec<[u8; N]> {
    fn from(value: Buffer) -> Self {
      TinyVec::from(value.as_bytes())
    }
  }
};
#[cfg(feature = "smallvec_1")]
const _: () = {
  use smallvec_1::SmallVec;

  impl<const N: usize> From<Buffer> for SmallVec<[u8; N]> {
    fn from(value: Buffer) -> Self {
      SmallVec::from_slice(value.as_bytes())
    }
  }
};

#[cfg(any(feature = "std", feature = "alloc"))]
const _: () = {
  use std::{borrow::ToOwned, string::String, vec::Vec};

  impl_from_domain_buffer!(
    as_str(
      into -> String,
      into -> std::sync::Arc<str>,
      into -> std::boxed::Box<str>,
      into -> std::rc::Rc<str>,
    ),
    as_bytes(
      into -> Vec<u8>,
      into -> std::sync::Arc<[u8]>,
      into -> std::boxed::Box<[u8]>,
      into -> std::rc::Rc<[u8]>,
    ),
  );

  impl From<Buffer> for std::borrow::Cow<'_, str> {
    /// ```rust
    /// use hostaddr::{Buffer, Domain};
    ///
    /// let domain: Domain<Buffer> = "example.com".parse().unwrap();
    /// let str: std::borrow::Cow<'_, str> = domain.into_inner().into();
    /// ```
    fn from(value: Buffer) -> Self {
      std::borrow::Cow::Owned(value.as_str().to_owned())
    }
  }

  impl From<Buffer> for std::borrow::Cow<'_, [u8]> {
    /// ```rust
    /// use hostaddr::{Buffer, Domain};
    ///
    /// let domain: Domain<Buffer> = "example.com".parse().unwrap();
    /// let bytes: std::borrow::Cow<'_, [u8]> = domain.into_inner().into();
    /// ```
    fn from(value: Buffer) -> Self {
      std::borrow::Cow::Owned(value.as_bytes().to_owned())
    }
  }
};

impl<'a> From<&'a Buffer> for &'a str {
  /// ```rust
  /// # #[cfg(any(feature = "std", feature = "alloc"))]
  /// # {
  /// use hostaddr::{Buffer, Domain};
  ///
  /// let domain: Domain<Buffer> = "example.com".parse().unwrap();
  /// let str: &str = (&domain.into_inner()).into();
  /// # }
  /// ```
  fn from(value: &'a Buffer) -> Self {
    value.as_str()
  }
}

impl<'a> From<&'a Buffer> for &'a [u8] {
  /// ```rust
  /// # #[cfg(any(feature = "std", feature = "alloc"))]
  /// # {
  /// use hostaddr::{Buffer, Domain};
  ///
  /// let domain: Domain<Buffer> = "example.com".parse().unwrap();
  /// let bytes: &[u8] = (&domain.into_inner()).into();
  /// # }
  /// ```
  fn from(value: &'a Buffer) -> Self {
    value.as_bytes()
  }
}

impl Borrow<str> for Buffer {
  /// ```rust
  /// # #[cfg(any(feature = "std", feature = "alloc"))]
  /// # {
  /// use hostaddr::{Buffer, Domain};
  /// use std::borrow::Borrow;
  ///
  /// let domain: Domain<Buffer> = "example.com".parse().unwrap();
  /// let str: &str = domain.into_inner().borrow();
  /// # }
  /// ```
  #[inline]
  fn borrow(&self) -> &str {
    self.as_ref()
  }
}

impl AsRef<[u8]> for Buffer {
  /// ```rust
  /// # #[cfg(any(feature = "std", feature = "alloc"))]
  /// # {
  /// use hostaddr::{Buffer, Domain};
  ///
  /// let domain: Domain<Buffer> = "example.com".parse().unwrap();
  /// let bytes: &[u8] = domain.into_inner().as_ref();
  /// # }
  /// ```
  fn as_ref(&self) -> &[u8] {
    self.as_bytes()
  }
}

impl AsRef<str> for Buffer {
  /// ```rust
  /// # #[cfg(any(feature = "std", feature = "alloc"))]
  /// # {
  /// use hostaddr::{Buffer, Domain};
  ///
  /// let domain: Domain<Buffer> = "example.com".parse().unwrap();
  /// let str: &str = domain.into_inner().as_ref();
  /// # }
  /// ```
  fn as_ref(&self) -> &str {
    self.as_str()
  }
}

impl Buffer {
  #[inline]
  pub(super) const fn new() -> Self {
    Self { buf: [0; 255] }
  }

  /// Returns the domain as a `str`.
  #[inline]
  pub fn as_str(&self) -> &str {
    // SAFETY: The domain is guaranteed to be valid UTF-8.
    unsafe { core::str::from_utf8_unchecked(&self.buf[..self.len()]) }
  }

  /// Returns the domain as a `str`.
  ///
  /// ## Example
  ///
  /// ```rust
  /// # #[cfg(any(feature = "std", feature = "alloc"))]
  /// # {
  /// use hostaddr::{Buffer, Domain};
  ///
  /// let domain: Domain<Buffer> = "example.com".parse().unwrap();
  /// assert_eq!("example.com", domain.into_inner().const_as_str());
  /// # }
  /// ```
  #[inline]
  pub const fn const_as_str(&self) -> &str {
    // SAFETY: The domain is guaranteed to be valid UTF-8.
    unsafe { core::str::from_utf8_unchecked(self.as_bytes()) }
  }

  /// Returns the domain as a `[u8]`
  #[inline]
  pub const fn as_bytes(&self) -> &[u8] {
    let len = self.len();
    self.buf.split_at(len).0
  }

  /// Push a `u8` to the buffer.
  #[inline]
  pub const fn push(&mut self, byte: u8) -> Result<(), u8> {
    let len = self.len();
    if len == 254 {
      return Err(byte);
    }
    self.buf[len] = byte;
    self.buf[254] += 1;
    Ok(())
  }

  #[inline]
  const fn len(&self) -> usize {
    *self.buf.last().unwrap() as usize
  }

  #[inline]
  pub(super) fn copy_from_slice(slice: &[u8]) -> Self {
    let len = slice.len();
    assert!(len <= 254, "domain name too long");

    let mut buf = Self::new();
    buf.buf[..len].copy_from_slice(slice);
    buf.buf[254] = len as u8;
    buf
  }

  #[inline]
  pub(super) fn copy_from_str(s: &str) -> Self {
    Self::copy_from_slice(s.as_bytes())
  }
}

impl core::fmt::Write for Buffer {
  fn write_str(&mut self, s: &str) -> core::fmt::Result {
    let len = s.len();
    let pos = self.len();
    if pos + len > 254 {
      return Err(core::fmt::Error);
    }
    self.buf[pos..pos + len].copy_from_slice(s.as_bytes());
    *self.buf.last_mut().unwrap() += len as u8;
    Ok(())
  }
}

#[cfg(feature = "serde")]
const _: () = {
  #[cfg(any(feature = "std", feature = "alloc"))]
  use either::Either;

  use serde::{Deserialize, Serialize};

  use super::Domain;

  impl Serialize for Buffer {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
      S: serde::Serializer,
    {
      if serializer.is_human_readable() {
        serializer.serialize_str(self.as_str())
      } else {
        serializer.serialize_bytes(self.as_bytes())
      }
    }
  }

  impl<'de> Deserialize<'de> for Buffer {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
      D: serde::Deserializer<'de>,
    {
      if deserializer.is_human_readable() {
        let s = <&str>::deserialize(deserializer)?;

        #[cfg(any(feature = "std", feature = "alloc"))]
        {
          let res = Domain::try_from_str(s).map_err(serde::de::Error::custom)?;

          Ok(match res {
            Either::Left(d) => Self::copy_from_slice(d.0.as_bytes()),
            Either::Right(d) => d,
          })
        }

        #[cfg(not(any(feature = "std", feature = "alloc")))]
        {
          let res = Domain::try_from_ascii_str(s).map_err(serde::de::Error::custom)?;
          Ok(Self::copy_from_slice(res.0.as_bytes()))
        }
      } else {
        let bytes = <&[u8]>::deserialize(deserializer)?;

        #[cfg(any(feature = "std", feature = "alloc"))]
        {
          let res = Domain::try_from_bytes(bytes).map_err(serde::de::Error::custom)?;
          Ok(match res {
            Either::Left(d) => Self::copy_from_slice(d.0),
            Either::Right(d) => d,
          })
        }

        #[cfg(not(any(feature = "std", feature = "alloc")))]
        {
          let res = Domain::try_from_ascii_bytes(bytes).map_err(serde::de::Error::custom)?;
          Ok(Self::copy_from_slice(&res.0))
        }
      }
    }
  }
};

#[cfg(test)]
mod test {
  #[cfg(any(feature = "std", feature = "alloc", feature = "serde"))]
  #[test]
  fn test_ord() {
    use super::*;

    let a = Buffer::copy_from_str("a");
    let b = Buffer::copy_from_str("b");
    assert!(a < b);

    assert!(a.partial_cmp(&b) == Some(core::cmp::Ordering::Less));
  }
}