mercy 1.1.1

Owned pair of immutable strings.
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
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
//! [`Act`] and friends.

use core::{
    error::Error as StdError,
    fmt, hash,
    marker::PhantomData,
    mem::discriminant,
    ops::{Deref, DerefMut},
};

use constr::Constr;

use crate::{
    spair::Spair,
    traits::{Stringable, Stringy},
};

/// State of actor.
#[derive(Copy, Clone)]
struct State(usize);
impl State {
    /// Bitshift for preset states.
    const SHIFT: u32 = usize::BITS - 2;

    /// State when only first string is built.
    const FIRST: State = State(0b01 << State::SHIFT);

    /// State when only second string is built.
    const SECOND: State = State(0b10 << State::SHIFT);

    /// State when neither string is built.
    const NEITHER: State = State(0b11 << State::SHIFT);

    /// Finished state.
    ///
    /// Note: will fail if split is more than `FIRST`, but this would probably be an allocation
    /// failure, so, we just don't worry about it.
    fn new(split: usize) -> State {
        State(split)
    }

    /// If the builder has the first string.
    fn has_first(&self) -> bool {
        self.0 & State::FIRST.0 != State::FIRST.0
    }

    /// If the builder has the second string.
    fn has_second(&self) -> bool {
        self.0 & State::SECOND.0 != State::SECOND.0
    }

    /// The split index, if the builder has both strings.
    fn as_split(&self) -> Option<usize> {
        (self.0 & State::NEITHER.0 == 0).then_some(self.0)
    }
}

/// Wrapper for [`IncompleteError`] and [`DuplicateError`] that allows naming the two strings.
pub struct Custom<E, K1: Constr, K2: Constr> {
    /// Inner error.
    err: E,

    /// Phantom data marker.
    marker: PhantomData<(K1, K2)>,
}
impl<E, K1: Constr, K2: Constr> Custom<E, K1, K2> {
    /// Get inner error.
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    pub fn into_inner(self) -> E {
        self.err
    }
}
impl<E, K1: Constr, K2: Constr> Deref for Custom<E, K1, K2> {
    type Target = E;
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn deref(&self) -> &E {
        &self.err
    }
}
impl<E, K1: Constr, K2: Constr> DerefMut for Custom<E, K1, K2> {
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn deref_mut(&mut self) -> &mut E {
        &mut self.err
    }
}
impl<E: Copy, K1: Constr, K2: Constr> Copy for Custom<E, K1, K2> {}
impl<E: Clone, K1: Constr, K2: Constr> Clone for Custom<E, K1, K2> {
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn clone(&self) -> Self {
        Custom {
            err: self.err.clone(),
            marker: PhantomData,
        }
    }
}
impl<L: PartialEq<R>, R, K1: Constr, K2: Constr> PartialEq<Custom<R, K1, K2>>
    for Custom<L, K1, K2>
{
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn eq(&self, other: &Custom<R, K1, K2>) -> bool {
        self.err == other.err
    }
}
impl<E: Eq, K1: Constr, K2: Constr> Eq for Custom<E, K1, K2> {}
impl<E: hash::Hash, K1: Constr, K2: Constr> hash::Hash for Custom<E, K1, K2> {
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.err.hash(state);
    }
}
impl<S: AsRef<str>, K1: Constr, K2: Constr> StdError for CustomIncompleteError<S, K1, K2> {}
impl<S: AsRef<str>, K1: Constr, K2: Constr> fmt::Debug for CustomIncompleteError<S, K1, K2> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (first, second) = self.pair();
        let mut err = f.debug_struct("IncompleteError");
        match first {
            Some(first) => {
                err.field(K1::STR, &first);
            }
            None => {
                err.field(K1::STR, &format_args!("_"));
            }
        }
        match second {
            Some(second) => {
                err.field(K2::STR, &second);
            }
            None => {
                err.field(K2::STR, &format_args!("_"));
            }
        }
        err.finish()
    }
}
impl<S: AsRef<str>, K1: Constr, K2: Constr> StdError for CustomDuplicateError<S, K1, K2> {}
impl<S: AsRef<str>, K1: Constr, K2: Constr> fmt::Debug for CustomDuplicateError<S, K1, K2> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DuplicateError")
            .field("pair", &self.pair())
            .field(
                "which",
                &format_args!(
                    "{}",
                    match self.which() {
                        Duplicated::First => K1::STR,
                        Duplicated::Second => K2::STR,
                    }
                ),
            )
            .finish()
    }
}
impl<S: AsRef<str>, K1: Constr, K2: Constr> fmt::Display for CustomIncompleteError<S, K1, K2> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let k1 = K1::STR;
        let k2 = K2::STR;
        match &self.err {
            IncompleteError::Empty => {
                write!(f, "missing {k1} and {k2}")
            }
            IncompleteError::First(first) => {
                let first = first.as_ref();
                write!(f, "got {k1} {first:?}, but missing {k2}")
            }
            IncompleteError::Second(second) => {
                let second = second.as_ref();
                write!(f, "got {k2} {second:?}, but missing {k1}")
            }
        }
    }
}
impl<S: AsRef<str>, K1: Constr, K2: Constr> fmt::Display for CustomDuplicateError<S, K1, K2> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (first, second) = self.pair();
        let which = match self.which {
            Duplicated::First => K1::STR,
            Duplicated::Second => K2::STR,
        };
        write!(f, "got duplicate {which}: {first:?} and {second:?}")
    }
}

/// Alias for <code>[Custom]&lt;[DuplicateError]&lt;S&gt;, K1, K2&gt;</code>.
pub type CustomDuplicateError<S, K1, K2> = Custom<DuplicateError<S>, K1, K2>;

/// Alias for <code>[Custom]&lt;[IncompleteError]&lt;S&gt;, K1, K2&gt;</code>.
pub type CustomIncompleteError<S, K1, K2> = Custom<IncompleteError<S>, K1, K2>;

/// Error when building an incomplete [`Spair`] via [`Act::finish`].
#[derive(Copy, Clone)]
pub enum IncompleteError<S: AsRef<str>> {
    /// Empty pair.
    Empty,

    /// Only first item in pair.
    First(S),

    /// Only second item in pair.
    Second(S),
}
impl<S: AsRef<str>> IncompleteError<S> {
    /// The partially-formed pair as a pair of options.
    ///
    /// Note that exhaustive matching should be done on the type itself, since the
    /// `(Some(_), Some(_))` variant will never be possible here.
    ///
    /// # Examples
    ///
    /// ```
    /// let err = mercy::act::<String, constr::Empty, false>()
    ///     .first("hello")
    ///     .unwrap()
    ///     .finish()
    ///     .unwrap_err();
    ///
    /// assert_eq!(err.pair(), (Some("hello"), None));
    /// ```
    pub fn pair(&self) -> (Option<&str>, Option<&str>) {
        match self {
            IncompleteError::Empty => (None, None),
            IncompleteError::First(buf) => (Some(buf.as_ref()), None),
            IncompleteError::Second(buf) => (None, Some(buf.as_ref())),
        }
    }

    /// Customize the name of the two keys in the error message.
    ///
    /// To avoid taking up extra space, the keys are provided using [`constr`]s.
    ///
    /// # Examples
    ///
    /// ```
    /// let err = mercy::act::<String, constr::Empty, false>()
    ///     .finish()
    ///     .unwrap_err();
    /// println!("{err}"); // unfinished pair: (_, _)
    ///
    /// constr::constr! {
    ///     type K1 = "greeting";
    ///     type K2 = "entity";
    /// }
    ///
    /// let err: mercy::CustomIncompleteError<String, K1, K2> = err.custom();
    /// println!("{err}"); // missing greeting and entity
    /// ```
    pub fn custom<K1: Constr, K2: Constr>(self) -> CustomIncompleteError<S, K1, K2> {
        Custom {
            err: self,
            marker: PhantomData,
        }
    }
}
impl<S1: AsRef<str>, S2: AsRef<str>> PartialEq<IncompleteError<S2>> for IncompleteError<S1> {
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn eq(&self, other: &IncompleteError<S2>) -> bool {
        match (self, other) {
            (IncompleteError::Empty, IncompleteError::Empty) => true,
            (IncompleteError::First(lhs), IncompleteError::First(rhs))
            | (IncompleteError::Second(lhs), IncompleteError::Second(rhs)) => {
                lhs.as_ref() == rhs.as_ref()
            }
            _ => false,
        }
    }
}
impl<S: AsRef<str>> Eq for IncompleteError<S> {}
impl<S: AsRef<str>> hash::Hash for IncompleteError<S> {
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        let (first, second) = self.pair();
        first.as_ref().hash(state);
        second.as_ref().hash(state);
    }
}
impl<S: AsRef<str>> fmt::Debug for IncompleteError<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (first, second) = self.pair();
        let mut tuple = f.debug_tuple("IncompleteError");
        match first {
            Some(first) => {
                tuple.field(&first);
            }
            None => {
                tuple.field(&format_args!("_"));
            }
        }
        match second {
            Some(second) => {
                tuple.field(&second);
            }
            None => {
                tuple.field(&format_args!("_"));
            }
        }
        tuple.finish()
    }
}
impl<S: AsRef<str>> StdError for IncompleteError<S> {}
impl<S: AsRef<str>> fmt::Display for IncompleteError<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (first, second) = self.pair();
        write!(f, "unfinished pair: (")?;
        match first {
            Some(first) => {
                write!(f, "{first:?}")?;
            }
            None => {
                write!(f, "_")?;
            }
        }
        write!(f, ", ")?;
        match second {
            Some(second) => {
                write!(f, "{second:?}")?;
            }
            None => {
                write!(f, "_")?;
            }
        }
        write!(f, ")")
    }
}

/// Duplicated key returned by [`DuplicateError::which`].
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Duplicated {
    /// First key duplicated.
    First,

    /// Second key duplicated.
    Second,
}

/// Error when there were duplicate keys provided to a [`Spair`].
///
/// Returned by both [`Act::first`] and [`Act::second`].
pub struct DuplicateError<S: AsRef<str>> {
    /// Pair of duplicates.
    pub(crate) pair: Spair<S>,

    /// Which key was duplicated
    pub(crate) which: Duplicated,
}
impl<S: AsRef<str>> DuplicateError<S> {
    /// The pair of duplicate keys as a [`Spair`].
    ///
    /// # Examples
    ///
    /// ```
    /// let mercy::BuildError::Duplicate(err) = mercy::act::<String, constr::Empty, false>()
    ///     .first("hello")
    ///     .unwrap()
    ///     .first("oops!")
    ///     .unwrap_err();
    ///
    /// assert_eq!(err.spair().pair(), ("hello", "oops!"));
    /// ```
    pub fn spair(&self) -> &Spair<S> {
        &self.pair
    }

    /// The pair of duplicate keys as a pair of strings.
    ///
    /// # Examples
    ///
    /// ```
    /// let mercy::BuildError::Duplicate(err) = mercy::act::<String, constr::Empty, false>()
    ///     .first("hello")
    ///     .unwrap()
    ///     .first("oops!")
    ///     .unwrap_err();
    ///
    /// assert_eq!(err.pair(), ("hello", "oops!"));
    /// ```
    pub fn pair(&self) -> (&str, &str) {
        self.pair.pair()
    }

    /// Which key was duplicated.
    ///
    /// Indicates whether the error was returned by [`Act::first`] or [`Act::second`].
    ///
    /// # Examples
    ///
    /// ```
    /// let mercy::BuildError::Duplicate(err) = mercy::act::<String, constr::Empty, false>()
    ///     .first("hello")
    ///     .unwrap()
    ///     .first("oops!")
    ///     .unwrap_err();
    ///
    /// assert_eq!(err.which(), mercy::Duplicated::First);
    /// ```
    pub fn which(&self) -> Duplicated {
        self.which
    }

    /// Customize the name of the two keys in the error message.
    ///
    /// To avoid taking up extra space, the keys are provided using [`constr`]s.
    ///
    /// # Examples
    ///
    /// ```
    /// let mercy::BuildError::Duplicate(err) = mercy::act::<String, constr::Empty, false>()
    ///     .first("hello")
    ///     .unwrap()
    ///     .first("oops!")
    ///     .unwrap_err();
    /// println!("{err}"); // got duplicate first key: "hello" and "oops!"
    ///
    /// constr::constr! {
    ///     type K1 = "greeting";
    ///     type K2 = "entity";
    /// }
    ///
    /// let err: mercy::CustomDuplicateError<String, K1, K2> = err.custom();
    /// println!("{err}"); // got duplicate greeting: "hello" and "oops!"
    /// ```
    pub fn custom<K1: Constr, K2: Constr>(self) -> CustomDuplicateError<S, K1, K2> {
        Custom {
            err: self,
            marker: PhantomData,
        }
    }
}
impl<S: AsRef<str> + Clone> Clone for DuplicateError<S> {
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn clone(&self) -> Self {
        DuplicateError {
            pair: self.pair.clone(),
            which: self.which,
        }
    }
}
impl<S1: AsRef<str>, S2: AsRef<str>> PartialEq<DuplicateError<S2>> for DuplicateError<S1> {
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn eq(&self, other: &DuplicateError<S2>) -> bool {
        self.which() == other.which() && self.spair() == other.spair()
    }
}
impl<S: AsRef<str>> Eq for DuplicateError<S> {}
impl<S: AsRef<str>> hash::Hash for DuplicateError<S> {
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.spair().hash(state);
        self.which().hash(state);
    }
}
impl<S: AsRef<str> + Copy> Copy for DuplicateError<S> {}
impl<S: AsRef<str>> fmt::Debug for DuplicateError<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DuplicateError")
            .field("pair", &self.pair())
            .field("which", &self.which)
            .finish()
    }
}
impl<S: AsRef<str>> StdError for DuplicateError<S> {}
impl<S: AsRef<str>> fmt::Display for DuplicateError<S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (first, second) = self.pair();
        let which = match self.which {
            Duplicated::First => "first",
            Duplicated::Second => "second",
        };
        write!(f, "got duplicate {which} key: {first:?} and {second:?}")
    }
}

/// Error when building a [`Spair`] with [`Act::finish`].
pub enum BuildError<S: Stringable> {
    /// Wrote two strings to the same slot.
    Duplicate(DuplicateError<S>),

    /// Insufficient capacity when building pair.
    Capacity(<S::Builder as Stringy>::Error),
}
impl<S: Stringable + Clone> Clone for BuildError<S>
where
    <S::Builder as Stringy>::Error: Clone,
{
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn clone(&self) -> Self {
        match self {
            BuildError::Duplicate(err) => BuildError::Duplicate(err.clone()),
            BuildError::Capacity(err) => BuildError::Capacity(err.clone()),
        }
    }
}
impl<S: Stringable + Copy> Copy for BuildError<S> where <S::Builder as Stringy>::Error: Copy {}
impl<S1: Stringable, S2: Stringable> PartialEq<BuildError<S2>> for BuildError<S1>
where
    <S1::Builder as Stringy>::Error: PartialEq<<S2::Builder as Stringy>::Error>,
{
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn eq(&self, other: &BuildError<S2>) -> bool {
        match (self, other) {
            (BuildError::Duplicate(lhs), BuildError::Duplicate(rhs)) => lhs == rhs,
            (BuildError::Capacity(lhs), BuildError::Capacity(rhs)) => lhs == rhs,
            _ => false,
        }
    }
}
impl<S: Stringable> hash::Hash for BuildError<S>
where
    <S::Builder as Stringy>::Error: hash::Hash,
{
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        discriminant(self).hash(state);
        match self {
            BuildError::Duplicate(err) => err.hash(state),
            BuildError::Capacity(err) => err.hash(state),
        }
    }
}
impl<S: Stringable> Eq for BuildError<S> where <S::Builder as Stringy>::Error: Eq {}
impl<S: Stringable> fmt::Debug for BuildError<S>
where
    <S::Builder as Stringy>::Error: fmt::Debug,
{
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BuildError::Duplicate(err) => fmt::Debug::fmt(err, f),
            BuildError::Capacity(err) => fmt::Debug::fmt(err, f),
        }
    }
}
impl<S: 'static + Stringable> StdError for BuildError<S>
where
    <S::Builder as Stringy>::Error: 'static + StdError,
{
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            BuildError::Duplicate(err) => Some(err),
            BuildError::Capacity(err) => Some(err),
        }
    }
}
impl<S: Stringable> fmt::Display for BuildError<S>
where
    <S::Builder as Stringy>::Error: fmt::Display,
{
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BuildError::Duplicate(err) => fmt::Display::fmt(err, f),
            BuildError::Capacity(err) => fmt::Display::fmt(err, f),
        }
    }
}

/// Lets you build a [`Spair`].
///
/// See [`Act`] for more information on usage.
///
/// # Examples
///
/// ```
/// constr::constr! { type Sep = "-"; }
///
/// let mut builder = mercy::act::<String, Sep, false>();
///
/// println!("{:?}", builder); // Act(_, _)
/// ```
pub fn act<S: Stringable, Sep: Constr, const SPLIT_SEP: bool>() -> Act<S, Sep, SPLIT_SEP> {
    Act {
        buf: S::Builder::new(),
        state: State::NEITHER,
        phantom: PhantomData,
    }
}

/// Builder made with [`act`].
///
/// [`Act`]ing lets you set individual sides of the pair using a builder. If you don't care about
/// the order of the strings in the pair, you should probably just use [`Spair::new`].
pub struct Act<S: Stringable, Sep: Constr = constr::Empty, const SPLIT_SEP: bool = false> {
    /// Buffer containing string-in-progress.
    buf: S::Builder,

    /// State of builder.
    state: State,

    /// Phantom marker for separator.
    phantom: PhantomData<Sep>,
}
impl<S: Stringable, Sep: Constr, const SPLIT_SEP: bool> Act<S, Sep, SPLIT_SEP> {
    /// Add the first string to the pair.
    ///
    /// # Examples
    ///
    /// ```
    /// constr::constr! { type Sep = "-"; }
    ///
    /// let builder = mercy::act::<String, Sep, false>()
    ///     .first("hello")
    ///     .unwrap();
    ///
    /// println!("{builder:?}"); // Act("hello", _)
    /// ```
    pub fn first<F: AsRef<str>>(mut self, s: F) -> Result<Act<S, Sep, SPLIT_SEP>, BuildError<S>> {
        let s = s.as_ref();
        if self.state.has_first() {
            if let Some(mut split) = self.state.as_split() {
                if SPLIT_SEP {
                    split -= Sep::STR.len();
                }
                self.buf.truncate_back(split);
            }
            let split = self.buf.len();
            self.buf.push_back(s).map_err(BuildError::Capacity)?;

            Err(BuildError::Duplicate(DuplicateError {
                // SAFETY: We formed the pair correctly.
                pair: unsafe { Spair::split_unchecked(self.buf.into(), split) },
                which: Duplicated::First,
            }))
        } else if self.state.has_second() {
            let mut split = self.buf.len();
            if SPLIT_SEP {
                split += Sep::STR.len();
            }
            self.buf
                .push_front_many(&[s, Sep::STR])
                .map_err(BuildError::Capacity)?;
            self.state = State::new(split);
            Ok(self)
        } else {
            self.buf.push_back(s).map_err(BuildError::Capacity)?;
            self.state = State::SECOND;
            Ok(self)
        }
    }

    /// Add the second string to the pair.
    ///
    /// # Examples
    ///
    /// ```
    /// constr::constr! { type Sep = "-"; }
    ///
    /// let builder = mercy::act::<String, Sep, false>()
    ///     .second("world")
    ///     .unwrap();
    ///
    /// println!("{builder:?}"); // Act(_, "world")
    /// ```
    pub fn second<F: AsRef<str>>(mut self, s: F) -> Result<Act<S, Sep, SPLIT_SEP>, BuildError<S>> {
        let s = s.as_ref();
        if self.state.has_second() {
            if let Some(mut split) = self.state.as_split() {
                if !SPLIT_SEP {
                    split += Sep::STR.len();
                }
                self.buf.remove_front(split);
            }
            let split = self.buf.len();
            self.buf.push_back(s).map_err(BuildError::Capacity)?;

            Err(BuildError::Duplicate(DuplicateError {
                // SAFETY: We formed the pair correctly.
                pair: unsafe { Spair::split_unchecked(self.buf.into(), split) },
                which: Duplicated::Second,
            }))
        } else if self.state.has_first() {
            let mut split = self.buf.len();
            if SPLIT_SEP {
                split += Sep::STR.len();
            }
            self.buf
                .push_back_many(&[Sep::STR, s])
                .map_err(BuildError::Capacity)?;
            self.state = State::new(split);
            Ok(self)
        } else {
            self.buf.push_back(s).map_err(BuildError::Capacity)?;
            self.state = State::FIRST;
            Ok(self)
        }
    }

    /// Finish building the pair if complete.
    ///
    /// # Examples
    ///
    /// ```
    /// constr::constr! { type Sep = "-"; }
    ///
    /// let builder = mercy::act::<String, Sep, false>()
    ///     .first("hello")
    ///     .unwrap()
    ///     .second("world")
    ///     .unwrap();
    ///
    /// println!("{builder:?}"); // Act("hello", "world")
    ///
    /// assert_eq!(builder.finish().unwrap().full(), "hello-world");
    /// ```
    pub fn finish(self) -> Result<Spair<S, Sep, SPLIT_SEP>, IncompleteError<S>> {
        if let Some(split) = self.state.as_split() {
            // SAFETY: We formed the pair correctly.
            Ok(unsafe { Spair::split_unchecked(self.buf.into(), split) })
        } else if self.state.has_first() {
            Err(IncompleteError::First(self.buf.into()))
        } else if self.state.has_second() {
            Err(IncompleteError::Second(self.buf.into()))
        } else {
            Err(IncompleteError::Empty)
        }
    }
}
impl<S: Stringable<Builder: Copy>, Sep: Constr, const SPLIT_SEP: bool> Copy
    for Act<S, Sep, SPLIT_SEP>
{
}
impl<S: Stringable<Builder: Clone>, Sep: Constr, const SPLIT_SEP: bool> Clone
    for Act<S, Sep, SPLIT_SEP>
{
    #[cfg_attr(any(coverage_nightly, feature = "nightly"), coverage(off))]
    fn clone(&self) -> Self {
        Act {
            buf: self.buf.clone(),
            state: self.state,
            phantom: PhantomData,
        }
    }
}
impl<S: Stringable, Sep: Constr, const SPLIT_SEP: bool> fmt::Debug for Act<S, Sep, SPLIT_SEP> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(split) = self.state.as_split() {
            // SAFETY: We formed the pair correctly.
            let pair =
                unsafe { <Spair<&str, Sep, SPLIT_SEP>>::split_unchecked(self.buf.as_ref(), split) };
            let (first, second) = pair.pair();
            f.debug_tuple("Act").field(&first).field(&second).finish()
        } else if self.state.has_first() {
            f.debug_tuple("Act")
                .field(&self.buf.as_ref())
                .field(&format_args!("_"))
                .finish()
        } else if self.state.has_second() {
            f.debug_tuple("Act")
                .field(&format_args!("_"))
                .field(&self.buf.as_ref())
                .finish()
        } else {
            f.debug_tuple("Act")
                .field(&format_args!("_"))
                .field(&format_args!("_"))
                .finish()
        }
    }
}