domain/base/name/
builder.rs

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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
//! Building a domain name.
//!
//! This is a private module for tidiness. `NameBuilder` and `PushError`
//! are re-exported by the parent module.

use super::super::scan::{BadSymbol, Symbol, SymbolCharsError, Symbols};
use super::absolute::Name;
use super::relative::{RelativeName, RelativeNameError};
use super::traits::{ToName, ToRelativeName};
use super::Label;
#[cfg(feature = "bytes")]
use bytes::BytesMut;
use core::fmt;
use octseq::builder::{EmptyBuilder, FreezeBuilder, OctetsBuilder, ShortBuf};
#[cfg(feature = "std")]
use std::vec::Vec;

//------------ NameBuilder --------------------------------------------------

/// Builds a domain name step by step by appending data.
///
/// The domain name builder is the most fundamental way to construct a new
/// domain name. It wraps an octets builder that assembles the name step by
/// step.
///
/// The methods [`push`][Self::push] and [`append_slice`][Self::append_slice]
/// to add the octets of a label to end of the builder. Once a label is
/// complete, [`end_label`][Self::end_label] finishes the current label and
/// starts a new one.
///
/// The method [`append_label`][Self::append_label] combines this process
/// and appends the given octets as a label.
///
/// The name builder currently is not aware of internationalized domain
/// names. The octets passed to it are used as is and are not converted.
#[derive(Clone)]
pub struct NameBuilder<Builder> {
    /// The buffer to build the name in.
    builder: Builder,

    /// The position in `octets` where the current label started.
    ///
    /// If this is `None` we currently do not have a label.
    head: Option<usize>,
}

impl<Builder> NameBuilder<Builder> {
    /// Creates a new domain name builder from an octets builder.
    ///
    /// Whatever is in the buffer already is considered to be a relative
    /// domain name. Since that may not be the case, this function is
    /// unsafe.
    pub(super) unsafe fn from_builder_unchecked(builder: Builder) -> Self {
        NameBuilder {
            builder,
            head: None,
        }
    }

    /// Creates a new, empty name builder.
    #[must_use]
    pub fn new() -> Self
    where
        Builder: EmptyBuilder,
    {
        unsafe { NameBuilder::from_builder_unchecked(Builder::empty()) }
    }

    /// Creates a new, empty builder with a given capacity.
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self
    where
        Builder: EmptyBuilder,
    {
        unsafe {
            NameBuilder::from_builder_unchecked(Builder::with_capacity(
                capacity,
            ))
        }
    }

    /// Creates a new domain name builder atop an existing octets builder.
    ///
    /// The function checks that whatever is in the builder already
    /// consititutes a correctly encoded relative domain name.
    pub fn from_builder(builder: Builder) -> Result<Self, RelativeNameError>
    where
        Builder: OctetsBuilder + AsRef<[u8]>,
    {
        RelativeName::check_slice(builder.as_ref())?;
        Ok(unsafe { NameBuilder::from_builder_unchecked(builder) })
    }
}

#[cfg(feature = "std")]
impl NameBuilder<Vec<u8>> {
    /// Creates an empty domain name builder atop a `Vec<u8>`.
    #[must_use]
    pub fn new_vec() -> Self {
        Self::new()
    }

    /// Creates an empty builder atop a `Vec<u8>` with given capacity.
    ///
    /// Names are limited to a length of 255 octets, but you can provide any
    /// capacity you like here.
    #[must_use]
    pub fn vec_with_capacity(capacity: usize) -> Self {
        Self::with_capacity(capacity)
    }
}

#[cfg(feature = "bytes")]
impl NameBuilder<BytesMut> {
    /// Creates an empty domain name bulider atop a bytes value.
    pub fn new_bytes() -> Self {
        Self::new()
    }

    /// Creates an empty bulider atop a bytes value with given capacity.
    ///
    /// Names are limited to a length of 255 octets, but you can provide any
    /// capacity you like here.
    pub fn bytes_with_capacity(capacity: usize) -> Self {
        Self::with_capacity(capacity)
    }
}

impl<Builder: AsRef<[u8]>> NameBuilder<Builder> {
    /// Returns the already assembled domain name as an octets slice.
    pub fn as_slice(&self) -> &[u8] {
        self.builder.as_ref()
    }

    /// Returns the length of the already assembled domain name.
    pub fn len(&self) -> usize {
        self.builder.as_ref().len()
    }

    /// Returns whether the name is still empty.
    pub fn is_empty(&self) -> bool {
        self.builder.as_ref().is_empty()
    }
}

impl<Builder> NameBuilder<Builder>
where
    Builder: OctetsBuilder + AsRef<[u8]> + AsMut<[u8]>,
{
    /// Returns whether there currently is a label under construction.
    ///
    /// This returns `false` if the name is still empty or if the last thing
    /// that happend was a call to [`end_label`].
    ///
    /// [`end_label`]: NameBuilder::end_label
    pub fn in_label(&self) -> bool {
        self.head.is_some()
    }

    /// Attempts to append a slice to the underlying builder.
    ///
    /// This method doesn’t perform any checks but only does the necessary
    /// error conversion.
    fn _append_slice(&mut self, slice: &[u8]) -> Result<(), PushError> {
        self.builder
            .append_slice(slice)
            .map_err(|_| PushError::ShortBuf)
    }

    /// Pushes an octet to the end of the domain name.
    ///
    /// Starts a new label if necessary. Returns an error if pushing the
    /// octet would exceed the size limits for labels or domain names.
    pub fn push(&mut self, ch: u8) -> Result<(), PushError> {
        let len = self.len();
        if len >= 254 {
            return Err(PushError::LongName);
        }
        if let Some(head) = self.head {
            if len - head > Label::MAX_LEN {
                return Err(PushError::LongLabel);
            }
            self._append_slice(&[ch])?;
        } else {
            self.head = Some(len);
            self._append_slice(&[0, ch])?;
        }
        Ok(())
    }

    /// Pushes a symbol to the end of the domain name.
    ///
    /// The symbol is iterpreted as part of the presentation format of a
    /// domain name, i.e., an unescaped dot is considered a label separator.
    pub fn push_symbol(&mut self, sym: Symbol) -> Result<(), FromStrError> {
        if matches!(sym, Symbol::Char('.')) {
            if !self.in_label() {
                return Err(PresentationErrorEnum::EmptyLabel.into());
            }
            self.end_label();
            Ok(())
        } else if matches!(sym, Symbol::SimpleEscape(b'['))
            && !self.in_label()
        {
            Err(LabelFromStrErrorEnum::BinaryLabel.into())
        } else {
            self.push(sym.into_octet()?).map_err(Into::into)
        }
    }

    /// Appends the content of an octets slice to the end of the domain name.
    ///
    /// Starts a new label if necessary. Returns an error if pushing
    /// would exceed the size limits for labels or domain names.
    ///
    /// If `slice` is empty, does absolutely nothing.
    pub fn append_slice(&mut self, slice: &[u8]) -> Result<(), PushError> {
        if slice.is_empty() {
            return Ok(());
        }
        if let Some(head) = self.head {
            if slice.len() > Label::MAX_LEN - (self.len() - head) {
                return Err(PushError::LongLabel);
            }
        } else {
            if slice.len() > Label::MAX_LEN {
                return Err(PushError::LongLabel);
            }
            if self.len() + slice.len() > 254 {
                return Err(PushError::LongName);
            }
            self.head = Some(self.len());
            self._append_slice(&[0])?;
        }
        self._append_slice(slice)?;
        Ok(())
    }

    /// Ends the current label.
    ///
    /// If there isn’t a current label, does nothing.
    pub fn end_label(&mut self) {
        if let Some(head) = self.head {
            let len = self.len() - head - 1;
            self.builder.as_mut()[head] = len as u8;
            self.head = None;
        }
    }

    /// Appends an octets slice as a complete label.
    ///
    /// If there currently is a label under construction, it will be ended
    /// before appending `label`.
    ///
    /// Returns an error if `label` exceeds the label size limit of 63 bytes
    /// or appending the label would exceed the domain name size limit of
    /// 255 bytes.
    pub fn append_label(&mut self, label: &[u8]) -> Result<(), PushError> {
        let head = self.head;
        self.end_label();
        if let Err(err) = self.append_slice(label) {
            self.head = head;
            return Err(err);
        }
        self.end_label();
        Ok(())
    }

    /// Appends a label with the decimal representation of `u8`.
    ///
    /// If there currently is a label under construction, it will be ended
    /// before appending `label`.
    ///
    /// Returns an error if appending would result in a name longer than 254
    /// bytes.
    pub fn append_dec_u8_label(
        &mut self,
        value: u8,
    ) -> Result<(), PushError> {
        self.end_label();
        let hecto = value / 100;
        if hecto > 0 {
            self.push(hecto + b'0')?;
        }
        let deka = (value / 10) % 10;
        if hecto > 0 || deka > 0 {
            self.push(deka + b'0')?;
        }
        self.push(value % 10 + b'0')?;
        self.end_label();
        Ok(())
    }

    /// Appends a label with the hex digit.
    ///
    /// If there currently is a label under construction, it will be ended
    /// before appending `label`.
    ///
    /// Returns an error if appending would result in a name longer than 254
    /// bytes.
    pub fn append_hex_digit_label(
        &mut self,
        nibble: u8,
    ) -> Result<(), PushError> {
        fn hex_digit(nibble: u8) -> u8 {
            match nibble & 0x0F {
                0 => b'0',
                1 => b'1',
                2 => b'2',
                3 => b'3',
                4 => b'4',
                5 => b'5',
                6 => b'6',
                7 => b'7',
                8 => b'8',
                9 => b'9',
                10 => b'A',
                11 => b'B',
                12 => b'C',
                13 => b'D',
                14 => b'E',
                15 => b'F',
                _ => unreachable!(),
            }
        }

        self.end_label();
        self.push(hex_digit(nibble))?;
        self.end_label();
        Ok(())
    }

    /// Appends a relative domain name.
    ///
    /// If there currently is a label under construction, it will be ended
    /// before appending `name`.
    ///
    /// Returns an error if appending would result in a name longer than 254
    /// bytes.
    //
    //  XXX NEEDS TESTS
    pub fn append_name<N: ToRelativeName>(
        &mut self,
        name: &N,
    ) -> Result<(), PushNameError> {
        let head = self.head.take();
        self.end_label();
        if self.len() + usize::from(name.compose_len()) > 254 {
            self.head = head;
            return Err(PushNameError::LongName);
        }
        for label in name.iter_labels() {
            label
                .compose(&mut self.builder)
                .map_err(|_| PushNameError::ShortBuf)?;
        }
        Ok(())
    }

    /// Appends a name from a sequence of symbols.
    ///
    /// If there currently is a label under construction, it will be ended
    /// before appending `chars`.
    ///
    /// The character sequence must result in a domain name in representation
    /// format. That is, its labels should be separated by dots,
    /// actual dots, white space, backslashes  and byte values that are not
    /// printable ASCII characters should be escaped.
    ///
    /// The last label will only be ended if the last character was a dot.
    /// Thus, you can determine if that was the case via
    /// [`in_label`][Self::in_label].
    pub fn append_symbols<Sym: IntoIterator<Item = Symbol>>(
        &mut self,
        symbols: Sym,
    ) -> Result<(), FromStrError> {
        symbols
            .into_iter()
            .try_for_each(|symbol| self.push_symbol(symbol))
    }

    /// Appends a name from a sequence of characters.
    ///
    /// If there currently is a label under construction, it will be ended
    /// before appending `chars`.
    ///
    /// The character sequence must result in a domain name in representation
    /// format. That is, its labels should be separated by dots,
    /// actual dots, white space and backslashes should be escaped by a
    /// preceeding backslash, and any byte value that is not a printable
    /// ASCII character should be encoded by a backslash followed by its
    /// three digit decimal value.
    ///
    /// The last label will only be ended if the last character was a dot.
    /// Thus, you can determine if that was the case via
    /// [`in_label`][Self::in_label].
    pub fn append_chars<C: IntoIterator<Item = char>>(
        &mut self,
        chars: C,
    ) -> Result<(), FromStrError> {
        Symbols::with(chars.into_iter(), |symbols| {
            self.append_symbols(symbols)
        })
    }

    /// Finishes building the name and returns the resulting relative name.
    ///
    /// If there currently is a label being built, ends the label first
    /// before returning the name. I.e., you don’t have to call [`end_label`]
    /// explicitely.
    ///
    /// This method converts the builder into a relative name. If you would
    /// like to turn it into an absolute name, use [`into_name`] which
    /// appends the root label before finishing.
    ///
    /// [`end_label`]: NameBuilder::end_label
    /// [`into_name`]: NameBuilder::into_name
    pub fn finish(mut self) -> RelativeName<Builder::Octets>
    where
        Builder: FreezeBuilder,
    {
        self.end_label();
        unsafe { RelativeName::from_octets_unchecked(self.builder.freeze()) }
    }

    /// Appends the root label to the name and returns it as a [`Name`].
    ///
    /// If there currently is a label under construction, ends the label.
    /// Then adds the empty root label and transforms the name into a
    /// [`Name`].
    pub fn into_name(mut self) -> Result<Name<Builder::Octets>, PushError>
    where
        Builder: FreezeBuilder,
    {
        self.end_label();
        self._append_slice(&[0])?;
        Ok(unsafe { Name::from_octets_unchecked(self.builder.freeze()) })
    }

    /// Appends an origin and returns the resulting [`Name`].
    ///
    /// If there currently is a label under construction, ends the label.
    /// Then adds the `origin` and transforms the name into a
    /// [`Name`].
    //
    //  XXX NEEDS TESTS
    pub fn append_origin<N: ToName>(
        mut self,
        origin: &N,
    ) -> Result<Name<Builder::Octets>, PushNameError>
    where
        Builder: FreezeBuilder,
    {
        self.end_label();
        if self.len() + usize::from(origin.compose_len()) > Name::MAX_LEN {
            return Err(PushNameError::LongName);
        }
        for label in origin.iter_labels() {
            label
                .compose(&mut self.builder)
                .map_err(|_| PushNameError::ShortBuf)?;
        }
        Ok(unsafe { Name::from_octets_unchecked(self.builder.freeze()) })
    }
}

//--- Default

impl<Builder: EmptyBuilder> Default for NameBuilder<Builder> {
    fn default() -> Self {
        Self::new()
    }
}

//--- AsRef

impl<Builder: AsRef<[u8]>> AsRef<[u8]> for NameBuilder<Builder> {
    fn as_ref(&self) -> &[u8] {
        self.builder.as_ref()
    }
}

//------------ Santa’s Little Helpers ----------------------------------------

/// Parses the contents of an escape sequence from `chars`.
///
/// The backslash should already have been taken out of `chars`.
pub(super) fn parse_escape<C>(
    chars: &mut C,
    in_label: bool,
) -> Result<u8, LabelFromStrError>
where
    C: Iterator<Item = char>,
{
    let ch = chars.next().ok_or(SymbolCharsError::short_input())?;
    if ch.is_ascii_digit() {
        let v = ch.to_digit(10).unwrap() * 100
            + chars
                .next()
                .ok_or(SymbolCharsError::short_input())
                .and_then(|c| {
                    c.to_digit(10).ok_or(SymbolCharsError::bad_escape())
                })?
                * 10
            + chars
                .next()
                .ok_or(SymbolCharsError::short_input())
                .and_then(|c| {
                    c.to_digit(10).ok_or(SymbolCharsError::bad_escape())
                })?;
        if v > 255 {
            return Err(SymbolCharsError::bad_escape().into());
        }
        Ok(v as u8)
    } else if ch == '[' {
        // `\[` at the start of a label marks a binary label which we don’t
        // support. Within a label, the sequence is fine.
        if in_label {
            Ok(b'[')
        } else {
            Err(LabelFromStrErrorEnum::BinaryLabel.into())
        }
    } else {
        Ok(ch as u8)
    }
}

//============ Error Types ===================================================

//------------ PushError -----------------------------------------------------

/// An error happened while trying to push data to a domain name builder.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PushError {
    /// The current label would exceed the limit of 63 bytes.
    LongLabel,

    /// The name would exceed the limit of 255 bytes.
    LongName,

    /// The buffer is too short to contain the name.
    ShortBuf,
}

//--- From

impl From<ShortBuf> for PushError {
    fn from(_: ShortBuf) -> PushError {
        PushError::ShortBuf
    }
}

//--- Display and Error

impl fmt::Display for PushError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            PushError::LongLabel => f.write_str("long label"),
            PushError::LongName => f.write_str("long domain name"),
            PushError::ShortBuf => ShortBuf.fmt(f),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for PushError {}

//------------ PushNameError -------------------------------------------------

/// An error happened while trying to push a name to a domain name builder.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PushNameError {
    /// The name would exceed the limit of 255 bytes.
    LongName,

    /// The buffer is too short to contain the name.
    ShortBuf,
}

//--- From

impl From<ShortBuf> for PushNameError {
    fn from(_: ShortBuf) -> Self {
        PushNameError::ShortBuf
    }
}

//--- Display and Error

impl fmt::Display for PushNameError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            PushNameError::LongName => f.write_str("long domain name"),
            PushNameError::ShortBuf => ShortBuf.fmt(f),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for PushNameError {}

//------------ LabelFromStrError ---------------------------------------------

/// An error occured while reading a label from a string.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct LabelFromStrError(LabelFromStrErrorEnum);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum LabelFromStrErrorEnum {
    SymbolChars(SymbolCharsError),

    BadSymbol(BadSymbol),

    /// A binary label was encountered.
    BinaryLabel,

    /// The label would exceed the limit of 63 bytes.
    LongLabel,
}

//--- From

impl From<LabelFromStrErrorEnum> for LabelFromStrError {
    fn from(inner: LabelFromStrErrorEnum) -> Self {
        Self(inner)
    }
}

impl From<SymbolCharsError> for LabelFromStrError {
    fn from(err: SymbolCharsError) -> Self {
        Self(LabelFromStrErrorEnum::SymbolChars(err))
    }
}

impl From<BadSymbol> for LabelFromStrError {
    fn from(err: BadSymbol) -> Self {
        Self(LabelFromStrErrorEnum::BadSymbol(err))
    }
}

//--- Display and Error

impl fmt::Display for LabelFromStrError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            LabelFromStrErrorEnum::SymbolChars(err) => err.fmt(f),
            LabelFromStrErrorEnum::BadSymbol(err) => err.fmt(f),
            LabelFromStrErrorEnum::BinaryLabel => {
                f.write_str("a binary label was encountered")
            }
            LabelFromStrErrorEnum::LongLabel => {
                f.write_str("label length limit exceeded")
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for LabelFromStrError {}

//------------ FromStrError --------------------------------------------------

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum FromStrError {
    /// The string content was wrongly formatted.
    Presentation(PresentationError),

    /// The buffer is too short to contain the name.
    ShortBuf,
}

impl FromStrError {
    pub(super) fn empty_label() -> Self {
        Self::Presentation(PresentationErrorEnum::EmptyLabel.into())
    }
}

//--- From

impl From<PushError> for FromStrError {
    fn from(err: PushError) -> FromStrError {
        match err {
            PushError::LongLabel => LabelFromStrErrorEnum::LongLabel.into(),
            PushError::LongName => PresentationErrorEnum::LongName.into(),
            PushError::ShortBuf => FromStrError::ShortBuf,
        }
    }
}

impl From<PushNameError> for FromStrError {
    fn from(err: PushNameError) -> FromStrError {
        match err {
            PushNameError::LongName => PresentationErrorEnum::LongName.into(),
            PushNameError::ShortBuf => FromStrError::ShortBuf,
        }
    }
}

impl<T: Into<PresentationError>> From<T> for FromStrError {
    fn from(err: T) -> Self {
        Self::Presentation(err.into())
    }
}

//--- Display and Error

impl fmt::Display for FromStrError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            FromStrError::Presentation(err) => err.fmt(f),
            FromStrError::ShortBuf => ShortBuf.fmt(f),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for FromStrError {}

//------------ PresentationError ---------------------------------------------

/// An illegal presentation format was encountered.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PresentationError(PresentationErrorEnum);

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum PresentationErrorEnum {
    BadLabel(LabelFromStrError),

    /// An empty label was encountered.
    EmptyLabel,

    /// The name has more than 255 characters.
    LongName,
}

//--- From

impl From<PresentationErrorEnum> for PresentationError {
    fn from(err: PresentationErrorEnum) -> Self {
        Self(err)
    }
}

impl<T: Into<LabelFromStrError>> From<T> for PresentationError {
    fn from(err: T) -> Self {
        Self(PresentationErrorEnum::BadLabel(err.into()))
    }
}

//--- Display and Error

impl fmt::Display for PresentationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.0 {
            PresentationErrorEnum::BadLabel(ref err) => err.fmt(f),
            PresentationErrorEnum::EmptyLabel => f.write_str("empty label"),
            PresentationErrorEnum::LongName => {
                f.write_str("long domain name")
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for PresentationError {}

//============ Testing =======================================================

#[cfg(test)]
#[cfg(feature = "std")]
mod test {
    use super::*;

    #[test]
    fn compose() {
        let mut builder = NameBuilder::new_vec();
        builder.push(b'w').unwrap();
        builder.append_slice(b"ww").unwrap();
        builder.end_label();
        builder.append_slice(b"exa").unwrap();
        builder.push(b'm').unwrap();
        builder.push(b'p').unwrap();
        builder.append_slice(b"le").unwrap();
        builder.end_label();
        builder.append_slice(b"com").unwrap();
        assert_eq!(builder.finish().as_slice(), b"\x03www\x07example\x03com");
    }

    #[test]
    fn build_by_label() {
        let mut builder = NameBuilder::new_vec();
        builder.append_label(b"www").unwrap();
        builder.append_label(b"example").unwrap();
        builder.append_label(b"com").unwrap();
        assert_eq!(builder.finish().as_slice(), b"\x03www\x07example\x03com");
    }

    #[test]
    fn build_mixed() {
        let mut builder = NameBuilder::new_vec();
        builder.push(b'w').unwrap();
        builder.append_slice(b"ww").unwrap();
        builder.append_label(b"example").unwrap();
        builder.append_slice(b"com").unwrap();
        assert_eq!(builder.finish().as_slice(), b"\x03www\x07example\x03com");
    }

    #[test]
    fn name_limit() {
        let mut builder = NameBuilder::new_vec();
        for _ in 0..25 {
            // 9 bytes label is 10 bytes in total
            builder.append_label(b"123456789").unwrap();
        }

        assert_eq!(builder.append_label(b"12345"), Err(PushError::LongName));
        assert_eq!(builder.clone().append_label(b"1234"), Ok(()));

        assert_eq!(builder.append_slice(b"12345"), Err(PushError::LongName));
        assert_eq!(builder.clone().append_slice(b"1234"), Ok(()));

        assert_eq!(builder.append_slice(b"12"), Ok(()));
        assert_eq!(builder.push(b'3'), Ok(()));
        assert_eq!(builder.push(b'4'), Err(PushError::LongName))
    }

    #[test]
    fn label_limit() {
        let mut builder = NameBuilder::new_vec();
        builder.append_label(&[0u8; 63][..]).unwrap();
        assert_eq!(
            builder.append_label(&[0u8; 64][..]),
            Err(PushError::LongLabel)
        );
        assert_eq!(
            builder.append_label(&[0u8; 164][..]),
            Err(PushError::LongLabel)
        );

        builder.append_slice(&[0u8; 60][..]).unwrap();
        builder.clone().append_label(b"123").unwrap();
        assert_eq!(builder.append_slice(b"1234"), Err(PushError::LongLabel));
        builder.append_slice(b"12").unwrap();
        builder.push(b'3').unwrap();
        assert_eq!(builder.push(b'4'), Err(PushError::LongLabel));
    }

    #[test]
    fn finish() {
        let mut builder = NameBuilder::new_vec();
        builder.append_label(b"www").unwrap();
        builder.append_label(b"example").unwrap();
        builder.append_slice(b"com").unwrap();
        assert_eq!(builder.finish().as_slice(), b"\x03www\x07example\x03com");
    }

    #[test]
    fn into_name() {
        let mut builder = NameBuilder::new_vec();
        builder.append_label(b"www").unwrap();
        builder.append_label(b"example").unwrap();
        builder.append_slice(b"com").unwrap();
        assert_eq!(
            builder.into_name().unwrap().as_slice(),
            b"\x03www\x07example\x03com\x00"
        );
    }
}