babbel_core 0.1.2

Shared core I/O, encoding, numeric, error, and AST primitives for the Babbel data format family
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
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
//! Concrete implementations of output destinations adhering to SOLID principles.

use super::traits::{IByteWriter, IClearable, IDestination, ITailInspectable};
#[cfg(feature = "file-io")]
use super::traits::IFlushable;
use crate::error::ErrorCode;
#[cfg(not(feature = "std"))]
use alloc::string::String;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// In-memory byte buffer destination (`Vec<u8>`).
#[derive(Debug, Clone, Default)]
pub struct Buffer {
    /// Internal vector storing the raw bytes
    pub buffer: Vec<u8>,
}

impl Buffer {
    /// Creates a new buffer destination.
    pub fn new() -> Self {
        Self { buffer: Vec::new() }
    }

    /// Creates a new buffer with initial capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            buffer: Vec::with_capacity(capacity),
        }
    }

    /// Appends a single byte.
    pub fn add_byte(&mut self, byte: u8) {
        self.buffer.push(byte);
    }

    /// Appends a string of bytes.
    pub fn add_bytes(&mut self, bytes: &str) {
        self.buffer.extend_from_slice(bytes.as_bytes());
    }

    /// Clears all content from the buffer.
    pub fn clear(&mut self) {
        self.buffer.clear();
    }

    /// Returns the last written byte.
    pub fn last(&self) -> Option<u8> {
        self.buffer.last().copied()
    }

    /// Converts the buffer content to a String.
    pub fn to_string(&self) -> String {
        #[cfg(feature = "std")]
        return String::from_utf8_lossy(&self.buffer).into_owned();
        #[cfg(not(feature = "std"))]
        {
            if let Ok(s) = core::str::from_utf8(&self.buffer) {
                alloc::string::ToString::to_string(s)
            } else {
                alloc::string::ToString::to_string(&alloc::string::String::from_utf8_lossy(&self.buffer))
            }
        }
    }

    /// Converts the buffer content into an owned String without reallocating if valid UTF-8.
    pub fn into_string(self) -> Result<String, alloc::string::FromUtf8Error> {
        String::from_utf8(self.buffer)
    }

    /// Returns a string slice of the buffer if it contains valid UTF-8 without allocating.
    pub fn as_str(&self) -> Result<&str, core::str::Utf8Error> {
        core::str::from_utf8(&self.buffer)
    }

    /// Returns byte slice of written content.
    pub fn as_bytes(&self) -> &[u8] {
        &self.buffer
    }

    /// Consumes and returns the inner byte vector.
    pub fn into_vec(self) -> Vec<u8> {
        self.buffer
    }
}

impl IByteWriter for Buffer {
    fn write_byte(&mut self, byte: u8) -> Result<(), ErrorCode> {
        self.add_byte(byte);
        Ok(())
    }

    fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), ErrorCode> {
        self.buffer.extend_from_slice(bytes);
        Ok(())
    }
}

impl ITailInspectable for Buffer {
    fn last_byte(&self) -> Option<u8> {
        self.last()
    }
}

impl IClearable for Buffer {
    fn clear(&mut self) {
        self.clear();
    }
}

impl IDestination for Buffer {
    fn add_byte(&mut self, byte: u8) {
        self.add_byte(byte);
    }
    fn add_bytes(&mut self, bytes: &str) {
        self.add_bytes(bytes);
    }
    fn clear(&mut self) {
        self.clear();
    }
    fn last(&self) -> Option<u8> {
        self.last()
    }
}

/// Type alias for backward compatibility.
pub type BufferDestination = Buffer;

/// In-memory UTF-8 string destination (`String`).
#[derive(Debug, Clone, Default)]
pub struct StringDestination {
    buffer: String,
}

impl StringDestination {
    /// Creates a new empty string destination.
    pub fn new() -> Self {
        Self {
            buffer: String::new(),
        }
    }

    /// Creates a string destination with initial capacity.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            buffer: String::with_capacity(capacity),
        }
    }

    /// Appends a single byte as a character.
    pub fn add_byte(&mut self, byte: u8) {
        self.buffer.push(byte as char);
    }

    /// Appends multiple bytes from a string slice.
    pub fn add_bytes(&mut self, bytes: &str) {
        self.buffer.push_str(bytes);
    }

    /// Clears all content from the string.
    pub fn clear(&mut self) {
        self.buffer.clear();
    }

    /// Returns the last written byte.
    pub fn last(&self) -> Option<u8> {
        self.buffer.as_bytes().last().copied()
    }

    /// Returns a string slice reference.
    pub fn as_str(&self) -> &str {
        &self.buffer
    }

    /// Consumes and returns the inner string.
    pub fn into_string(self) -> String {
        self.buffer
    }
}

impl IByteWriter for StringDestination {
    fn write_byte(&mut self, byte: u8) -> Result<(), ErrorCode> {
        self.add_byte(byte);
        Ok(())
    }

    fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), ErrorCode> {
        if let Ok(s) = core::str::from_utf8(bytes) {
            self.buffer.push_str(s);
            Ok(())
        } else {
            for &b in bytes {
                self.buffer.push(b as char);
            }
            Ok(())
        }
    }
}

impl ITailInspectable for StringDestination {
    fn last_byte(&self) -> Option<u8> {
        self.last()
    }
}

impl IClearable for StringDestination {
    fn clear(&mut self) {
        self.clear();
    }
}

impl IDestination for StringDestination {
    fn add_byte(&mut self, byte: u8) {
        self.add_byte(byte);
    }
    fn add_bytes(&mut self, bytes: &str) {
        self.add_bytes(bytes);
    }
    fn clear(&mut self) {
        self.clear();
    }
    fn last(&self) -> Option<u8> {
        self.last()
    }
}

#[cfg(feature = "file-io")]
/// File output destination writing to a buffered file on disk (32KB write buffer).
pub struct FileDestination {
    writer: std::io::BufWriter<std::fs::File>,
    path: std::path::PathBuf,
    last_byte: Option<u8>,
    bytes_written: usize,
}

#[cfg(feature = "file-io")]
impl FileDestination {
    /// 32KB buffer for optimal disk write throughput
    const BUFFER_CAPACITY: usize = 32 * 1024;

    /// Creates or opens a file for writing with 32KB buffer.
    pub fn new(path: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
        Self::create(path)
    }

    /// Creates or opens a file for writing with 32KB buffer.
    pub fn create(path: impl AsRef<std::path::Path>) -> std::io::Result<Self> {
        let p = path.as_ref().to_path_buf();
        let file = std::fs::File::create(&p)?;
        let writer = std::io::BufWriter::with_capacity(Self::BUFFER_CAPACITY, file);
        Ok(Self {
            writer,
            path: p,
            last_byte: None,
            bytes_written: 0,
        })
    }

    /// Writes a single byte to the buffered destination.
    pub fn add_byte(&mut self, byte: u8) {
        use std::io::Write;
        let _ = self.writer.write_all(&[byte]);
        self.last_byte = Some(byte);
        self.bytes_written += 1;
    }

    /// Writes multiple bytes from a string slice to the buffered destination.
    pub fn add_bytes(&mut self, bytes: &str) {
        use std::io::Write;
        let _ = self.writer.write_all(bytes.as_bytes());
        self.last_byte = bytes.as_bytes().last().copied();
        self.bytes_written += bytes.len();
    }

    /// Clears all content from the file.
    pub fn clear(&mut self) {
        use std::io::{Seek, SeekFrom, Write};
        let _ = self.writer.flush();
        let file = self.writer.get_mut();
        let _ = file.set_len(0);
        let _ = file.seek(SeekFrom::Start(0));
        self.last_byte = None;
        self.bytes_written = 0;
    }

    /// Returns the last written byte.
    pub fn last(&self) -> Option<u8> {
        self.last_byte
    }

    /// Returns the number of bytes written.
    pub fn file_length(&self) -> usize {
        self.bytes_written
    }

    /// Returns the file path string.
    pub fn file_name(&self) -> &str {
        self.path.to_str().unwrap_or("")
    }

    /// Flushes the underlying buffered writer and syncs file data.
    pub fn flush(&mut self) -> std::io::Result<()> {
        use std::io::Write;
        self.writer.flush()?;
        self.writer.get_ref().sync_data()
    }

    /// Closes the file handle (auto-flushed on drop).
    pub fn close(&self) -> std::io::Result<()> {
        Ok(())
    }

    /// Writes all bytes to the buffered file.
    pub fn write_all_bytes(&mut self, data: &[u8]) -> std::io::Result<()> {
        use std::io::Write;
        self.writer.write_all(data)?;
        self.last_byte = data.last().copied();
        self.bytes_written += data.len();
        Ok(())
    }

    /// Writes a string slice to the buffered file.
    pub fn write_str(&mut self, data: &str) -> std::io::Result<()> {
        self.write_all_bytes(data.as_bytes())
    }
}

#[cfg(feature = "file-io")]
impl Drop for FileDestination {
    fn drop(&mut self) {
        use std::io::Write;
        let _ = self.writer.flush();
    }
}

#[cfg(feature = "file-io")]
impl IByteWriter for FileDestination {
    fn write_byte(&mut self, byte: u8) -> Result<(), ErrorCode> {
        use std::io::Write;
        self.writer.write_all(&[byte]).map_err(|_| ErrorCode::IoError)?;
        self.last_byte = Some(byte);
        self.bytes_written += 1;
        Ok(())
    }

    fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), ErrorCode> {
        use std::io::Write;
        self.writer.write_all(bytes).map_err(|_| ErrorCode::IoError)?;
        self.last_byte = bytes.last().copied();
        self.bytes_written += bytes.len();
        Ok(())
    }
}

#[cfg(feature = "file-io")]
impl ITailInspectable for FileDestination {
    fn last_byte(&self) -> Option<u8> {
        self.last()
    }
}

#[cfg(feature = "file-io")]
impl IClearable for FileDestination {
    fn clear(&mut self) {
        self.clear();
    }
}

#[cfg(feature = "file-io")]
impl IFlushable for FileDestination {
    fn flush(&mut self) -> Result<(), ErrorCode> {
        self.flush().map_err(|_| ErrorCode::IoError)
    }
}

#[cfg(feature = "file-io")]
impl IDestination for FileDestination {
    fn add_byte(&mut self, byte: u8) {
        self.add_byte(byte);
    }
    fn add_bytes(&mut self, bytes: &str) {
        self.add_bytes(bytes);
    }
    fn clear(&mut self) {
        self.clear();
    }
    fn last(&self) -> Option<u8> {
        self.last()
    }
}

// ==========================================
// Zero-Allocation / Stack Destinations
// ==========================================

/// Zero-allocation in-memory destination writing directly into a caller-supplied byte slice.
///
/// Ideal for embedded systems and real-time environments where heap allocation is forbidden.
#[derive(Debug)]
pub struct SliceDestination<'a> {
    buffer: &'a mut [u8],
    pos: usize,
    truncated: bool,
}

impl<'a> SliceDestination<'a> {
    /// Creates a new `SliceDestination` borrowing a mutable byte slice.
    pub fn new(buffer: &'a mut [u8]) -> Self {
        Self {
            buffer,
            pos: 0,
            truncated: false,
        }
    }

    /// Returns the number of bytes written so far.
    pub fn len(&self) -> usize {
        self.pos
    }

    /// Returns true if no bytes have been written.
    pub fn is_empty(&self) -> bool {
        self.pos == 0
    }

    /// Returns the total capacity of the borrowed slice.
    pub fn capacity(&self) -> usize {
        self.buffer.len()
    }

    /// Returns remaining space in the slice.
    pub fn remaining(&self) -> usize {
        self.buffer.len().saturating_sub(self.pos)
    }

    /// Returns true if an attempted write exceeded capacity.
    pub fn is_truncated(&self) -> bool {
        self.truncated
    }

    /// Returns a byte slice of the valid written content.
    pub fn as_bytes(&self) -> &[u8] {
        &self.buffer[..self.pos]
    }

    /// Returns a mutable byte slice of the valid written content.
    pub fn as_mut_bytes(&mut self) -> &mut [u8] {
        &mut self.buffer[..self.pos]
    }

    /// Returns a string slice of the written content if valid UTF-8.
    pub fn as_str(&self) -> Result<&str, core::str::Utf8Error> {
        core::str::from_utf8(self.as_bytes())
    }

    /// Appends a single byte.
    pub fn add_byte(&mut self, byte: u8) {
        if self.pos < self.buffer.len() {
            self.buffer[self.pos] = byte;
            self.pos += 1;
        } else {
            self.truncated = true;
        }
    }

    /// Appends a string slice of bytes.
    pub fn add_bytes(&mut self, bytes: &str) {
        let b = bytes.as_bytes();
        let avail = self.buffer.len().saturating_sub(self.pos);
        let to_copy = b.len().min(avail);
        if to_copy > 0 {
            self.buffer[self.pos..self.pos + to_copy].copy_from_slice(&b[..to_copy]);
            self.pos += to_copy;
        }
        if b.len() > avail {
            self.truncated = true;
        }
    }

    /// Resets the write position to 0 and clears truncated flag.
    pub fn clear(&mut self) {
        self.pos = 0;
        self.truncated = false;
    }
}

impl<'a> IDestination for SliceDestination<'a> {
    fn add_byte(&mut self, byte: u8) {
        if self.pos < self.buffer.len() {
            self.buffer[self.pos] = byte;
            self.pos += 1;
        } else {
            self.truncated = true;
        }
    }

    fn add_bytes(&mut self, bytes: &str) {
        let b = bytes.as_bytes();
        let avail = self.buffer.len().saturating_sub(self.pos);
        let to_copy = b.len().min(avail);
        if to_copy > 0 {
            self.buffer[self.pos..self.pos + to_copy].copy_from_slice(&b[..to_copy]);
            self.pos += to_copy;
        }
        if b.len() > avail {
            self.truncated = true;
        }
    }

    fn clear(&mut self) {
        self.clear();
    }

    fn last(&self) -> Option<u8> {
        if self.pos > 0 {
            Some(self.buffer[self.pos - 1])
        } else {
            None
        }
    }
}

impl<'a> IByteWriter for SliceDestination<'a> {
    fn write_byte(&mut self, byte: u8) -> Result<(), ErrorCode> {
        if self.pos < self.buffer.len() {
            self.buffer[self.pos] = byte;
            self.pos += 1;
            Ok(())
        } else {
            self.truncated = true;
            Err(ErrorCode::IoError)
        }
    }

    fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), ErrorCode> {
        let avail = self.buffer.len().saturating_sub(self.pos);
        if bytes.len() <= avail {
            self.buffer[self.pos..self.pos + bytes.len()].copy_from_slice(bytes);
            self.pos += bytes.len();
            Ok(())
        } else {
            self.buffer[self.pos..self.pos + avail].copy_from_slice(&bytes[..avail]);
            self.pos += avail;
            self.truncated = true;
            Err(ErrorCode::IoError)
        }
    }
}

impl<'a> IClearable for SliceDestination<'a> {
    fn clear(&mut self) {
        self.clear();
    }
}

impl<'a> ITailInspectable for SliceDestination<'a> {
    fn last_byte(&self) -> Option<u8> {
        self.last()
    }
}

/// Stack-allocated fixed-capacity byte buffer destination using const generics.
///
/// Allocates exclusively on the call stack without requiring any heap allocations (`no_alloc`).
#[derive(Debug, Clone)]
pub struct ArrayVecDestination<const N: usize> {
    data: [u8; N],
    pos: usize,
    truncated: bool,
}

impl<const N: usize> ArrayVecDestination<N> {
    /// Creates a new empty stack buffer destination.
    pub const fn new() -> Self {
        Self {
            data: [0u8; N],
            pos: 0,
            truncated: false,
        }
    }

    /// Returns the number of bytes written.
    pub fn len(&self) -> usize {
        self.pos
    }

    /// Returns true if no bytes have been written.
    pub fn is_empty(&self) -> bool {
        self.pos == 0
    }

    /// Returns the total capacity `N`.
    pub const fn capacity(&self) -> usize {
        N
    }

    /// Returns remaining space in the buffer.
    pub fn remaining(&self) -> usize {
        N.saturating_sub(self.pos)
    }

    /// Returns true if a write exceeded capacity.
    pub fn is_truncated(&self) -> bool {
        self.truncated
    }

    /// Returns written bytes as a slice.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data[..self.pos]
    }

    /// Returns written content as a string slice if valid UTF-8.
    pub fn as_str(&self) -> Result<&str, core::str::Utf8Error> {
        core::str::from_utf8(self.as_bytes())
    }

    /// Appends a single byte.
    pub fn add_byte(&mut self, byte: u8) {
        if self.pos < N {
            self.data[self.pos] = byte;
            self.pos += 1;
        } else {
            self.truncated = true;
        }
    }

    /// Appends a string slice of bytes.
    pub fn add_bytes(&mut self, bytes: &str) {
        let b = bytes.as_bytes();
        let avail = N.saturating_sub(self.pos);
        let to_copy = b.len().min(avail);
        if to_copy > 0 {
            self.data[self.pos..self.pos + to_copy].copy_from_slice(&b[..to_copy]);
            self.pos += to_copy;
        }
        if b.len() > avail {
            self.truncated = true;
        }
    }

    /// Clears written content.
    pub fn clear(&mut self) {
        self.pos = 0;
        self.truncated = false;
    }
}

impl<const N: usize> Default for ArrayVecDestination<N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const N: usize> IDestination for ArrayVecDestination<N> {
    fn add_byte(&mut self, byte: u8) {
        if self.pos < N {
            self.data[self.pos] = byte;
            self.pos += 1;
        } else {
            self.truncated = true;
        }
    }

    fn add_bytes(&mut self, bytes: &str) {
        let b = bytes.as_bytes();
        let avail = N.saturating_sub(self.pos);
        let to_copy = b.len().min(avail);
        if to_copy > 0 {
            self.data[self.pos..self.pos + to_copy].copy_from_slice(&b[..to_copy]);
            self.pos += to_copy;
        }
        if b.len() > avail {
            self.truncated = true;
        }
    }

    fn clear(&mut self) {
        self.clear();
    }

    fn last(&self) -> Option<u8> {
        if self.pos > 0 {
            Some(self.data[self.pos - 1])
        } else {
            None
        }
    }
}

impl<const N: usize> IByteWriter for ArrayVecDestination<N> {
    fn write_byte(&mut self, byte: u8) -> Result<(), ErrorCode> {
        if self.pos < N {
            self.data[self.pos] = byte;
            self.pos += 1;
            Ok(())
        } else {
            self.truncated = true;
            Err(ErrorCode::IoError)
        }
    }

    fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), ErrorCode> {
        let avail = N.saturating_sub(self.pos);
        if bytes.len() <= avail {
            self.data[self.pos..self.pos + bytes.len()].copy_from_slice(bytes);
            self.pos += bytes.len();
            Ok(())
        } else {
            self.data[self.pos..self.pos + avail].copy_from_slice(&bytes[..avail]);
            self.pos += avail;
            self.truncated = true;
            Err(ErrorCode::IoError)
        }
    }
}

impl<const N: usize> IClearable for ArrayVecDestination<N> {
    fn clear(&mut self) {
        self.clear();
    }
}

impl<const N: usize> ITailInspectable for ArrayVecDestination<N> {
    fn last_byte(&self) -> Option<u8> {
        self.last()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_buffer_destination() {
        let mut dest = BufferDestination::new();
        dest.add_byte(b'h');
        dest.add_bytes("ello");
        assert_eq!(dest.as_bytes(), b"hello");
        assert_eq!(dest.last(), Some(b'o'));
        dest.clear();
        assert_eq!(dest.last(), None);
    }

    #[test]
    fn test_string_destination() {
        let mut dest = StringDestination::new();
        dest.add_bytes("testing");
        assert_eq!(dest.as_str(), "testing");
        assert_eq!(dest.into_string(), "testing");
    }

    #[test]
    fn test_slice_destination() {
        let mut raw = [0u8; 16];
        let mut dest = SliceDestination::new(&mut raw);
        assert_eq!(dest.capacity(), 16);
        assert_eq!(dest.remaining(), 16);
        assert!(dest.is_empty());

        dest.add_bytes("12345");
        assert_eq!(dest.len(), 5);
        assert_eq!(dest.remaining(), 11);
        assert_eq!(dest.as_str().unwrap(), "12345");
        assert_eq!(dest.last(), Some(b'5'));

        dest.add_bytes("67890abcde");
        assert_eq!(dest.len(), 15);
        assert!(!dest.is_truncated());

        // Exceed capacity
        dest.add_bytes("xyz");
        assert!(dest.is_truncated());
        assert_eq!(dest.len(), 16);

        dest.clear();
        assert_eq!(dest.len(), 0);
        assert!(!dest.is_truncated());
    }

    #[test]
    fn test_array_vec_destination() {
        let mut dest = ArrayVecDestination::<32>::new();
        assert_eq!(dest.capacity(), 32);
        assert_eq!(dest.remaining(), 32);

        dest.add_bytes("embedded");
        dest.add_byte(b'_');
        dest.add_bytes("rust");

        assert_eq!(dest.as_str().unwrap(), "embedded_rust");
        assert_eq!(dest.last(), Some(b't'));
        assert_eq!(dest.len(), 13);
        assert_eq!(dest.remaining(), 19);

        dest.clear();
        assert_eq!(dest.len(), 0);
        assert_eq!(dest.remaining(), 32);
    }
}