vstp 0.2.1

VSTP - Vishu's Secure Transfer Protocol: A fast, secure, and extensible binary protocol for TCP and UDP
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
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# VSTP Detailed Code Analysis

## Line-by-Line Breakdown of Implementation

This document provides a detailed analysis of every line of code in the VSTP implementation, explaining the purpose, technical details, and design decisions.

## `src/types.rs` - Core Type Definitions

### Constants and Types

```rust
pub const VSTP_MAGIC: [u8; 2] = [0x56, 0x54]; // "VT"
```

**Purpose**: Protocol identifier to distinguish VSTP frames from other protocols
**Technical Details**:

- `0x56` = ASCII 'V' (86 decimal)
- `0x54` = ASCII 'T' (84 decimal)
- Fixed 2-byte array for efficient comparison

```rust
pub const VSTP_VERSION: u8 = 0x01;
```

**Purpose**: Protocol version for evolution and compatibility
**Technical Details**: Single byte allows 256 versions, starting at 1

```rust
pub type SessionId = u128;
```

**Purpose**: Unique identifier for connection sessions
**Technical Details**: 128-bit allows 2^128 unique sessions, practically unlimited

### Header Structure

```rust
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Header {
    pub key: Vec<u8>,
    pub value: Vec<u8>,
}
```

**Purpose**: Extensible metadata key-value pairs
**Technical Details**:

- `Debug`: Enables println!("{:?}", header)
- `Clone`: Allows copying headers
- `PartialEq, Eq`: Enables comparison and hashing
- `Vec<u8>`: Binary data, not limited to UTF-8

```rust
impl Header {
    pub fn new(key: Vec<u8>, value: Vec<u8>) -> Self {
        Self { key, value }
    }
```

**Purpose**: Constructor for binary header data
**Technical Details**: Takes ownership of vectors, no validation (done during encoding)

```rust
pub fn from_str(key: &str, value: &str) -> Self {
    Self {
        key: key.as_bytes().to_vec(),
        value: value.as_bytes().to_vec(),
    }
}
```

**Purpose**: Convenience constructor for string headers
**Technical Details**:

- `as_bytes()`: Converts &str to &[u8] UTF-8
- `to_vec()`: Creates owned Vec<u8> copy

### Frame Types

```rust
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameType {
    Hello = 0x01,
    Welcome = 0x02,
    Data = 0x03,
    Ping = 0x04,
    Pong = 0x05,
    Bye = 0x06,
    Ack = 0x07,
    Err = 0x08,
}
```

**Purpose**: Define all possible frame types
**Technical Details**:

- `#[repr(u8)]`: Ensures enum values are exactly 1 byte
- `Copy`: Allows cheap copying (no heap allocation)
- Explicit values: Prevents reordering issues

```rust
impl FrameType {
    pub fn from_u8(value: u8) -> Option<Self> {
        match value {
            0x01 => Some(FrameType::Hello),
            0x02 => Some(FrameType::Welcome),
            0x03 => Some(FrameType::Data),
            0x04 => Some(FrameType::Ping),
            0x05 => Some(FrameType::Pong),
            0x06 => Some(FrameType::Bye),
            0x07 => Some(FrameType::Ack),
            0x08 => Some(FrameType::Err),
            _ => None,
        }
    }
}
```

**Purpose**: Safe conversion from wire format to enum
**Technical Details**: Returns `Option` to handle invalid values gracefully

### Flags

```rust
bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub struct Flags: u8 {
        const REQ_ACK = 0b0000_0001;  // Request acknowledgment
        const CRC     = 0b0000_0010;  // CRC checksum present
        const FRAG    = 0b0001_0000;  // Fragmented frame
        const COMP    = 0b0010_0000;  // Compressed payload
    }
}
```

**Purpose**: Bit flags for frame properties
**Technical Details**:

- `bitflags!`: Macro generates bitwise operations
- `0b` prefix: Binary literal notation
- Bit positions: Allows multiple flags in single byte
- `Copy`: Efficient copying of single byte

### Main Frame Structure

```rust
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Frame {
    pub version: u8,
    pub typ: FrameType,
    pub flags: Flags,
    pub headers: Vec<Header>,
    pub payload: Vec<u8>,
}
```

**Purpose**: Complete VSTP frame representation
**Technical Details**:

- `version`: Protocol version for evolution
- `typ`: Frame type for routing
- `flags`: Bit flags for properties
- `headers`: Extensible metadata
- `payload`: Raw binary data

### Builder Pattern Implementation

```rust
impl Frame {
    pub fn new(typ: FrameType) -> Self {
        Self {
            version: VSTP_VERSION,
            typ,
            flags: Flags::empty(),
            headers: Vec::new(),
            payload: Vec::new(),
        }
    }
```

**Purpose**: Constructor with sensible defaults
**Technical Details**:

- Uses current protocol version
- Empty flags and collections
- Builder pattern for fluent API

```rust
pub fn with_payload(mut self, payload: Vec<u8>) -> Self {
    self.payload = payload;
    self
}
```

**Purpose**: Builder method for payload
**Technical Details**:

- `mut self`: Takes ownership and allows modification
- Returns `self` for method chaining
- No validation (done during encoding)

```rust
pub fn with_header(mut self, key: &str, value: &str) -> Self {
    self.headers.push(Header::from_str(key, value));
    self
}
```

**Purpose**: Builder method for string headers
**Technical Details**:

- `push()`: Adds to end of vector
- `from_str()`: Converts strings to binary
- Returns `self` for chaining

```rust
pub fn with_flag(mut self, flag: Flags) -> Self {
    self.flags |= flag;
    self
}
```

**Purpose**: Builder method for flags
**Technical Details**:

- `|=`: Bitwise OR assignment
- Combines multiple flags
- Returns `self` for chaining

### Error Types

```rust
#[derive(Error, Debug)]
pub enum VstpError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
```

**Purpose**: I/O error wrapper
**Technical Details**:

- `#[from]`: Enables `?` operator conversion
- `#[error]`: Custom error message formatting

```rust
#[error("Protocol error: {0}")]
Protocol(String),
```

**Purpose**: Generic protocol violations
**Technical Details**: String message for detailed error reporting

```rust
#[error("Invalid version: expected {expected}, got {got}")]
InvalidVersion { expected: u8, got: u8 },
```

**Purpose**: Version mismatch errors
**Technical Details**: Named fields for structured error information

```rust
#[error("Invalid frame type: {0}")]
InvalidFrameType(u8),
```

**Purpose**: Unknown frame type errors
**Technical Details**: Includes the invalid value for debugging

```rust
#[error("Invalid magic bytes: expected {:?}, got {:?}", VSTP_MAGIC, .0)]
InvalidMagic([u8; 2]),
```

**Purpose**: Wrong protocol identifier
**Technical Details**: Shows expected vs actual magic bytes

```rust
#[error("CRC mismatch: expected {expected}, got {got}")]
CrcMismatch { expected: u32, got: u32 },
```

**Purpose**: Data integrity errors
**Technical Details**: Shows expected vs calculated CRC values

```rust
#[error("Incomplete frame: need {needed} more bytes")]
Incomplete { needed: usize },
```

**Purpose**: Partial frame errors
**Technical Details**: Indicates how many more bytes are needed

```rust
#[error("Frame too large: {size} bytes exceeds limit of {limit}")]
FrameTooLarge { size: usize, limit: usize },
```

**Purpose**: Size limit violations
**Technical Details**: Shows actual size vs limit for debugging

## `src/frame.rs` - Frame Encoding/Decoding

### Imports

```rust
use bytes::{BufMut, Bytes, BytesMut};
use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
use crc_any::CRC;
use crate::types::{Flags, Frame, FrameType, Header, VstpError, VSTP_MAGIC, VSTP_VERSION};
```

**Purpose**: Import required dependencies
**Technical Details**:

- `bytes`: Efficient buffer management
- `byteorder`: Endianness handling
- `crc_any`: CRC calculation
- `crate::types`: Local type definitions

### Encoding Function

```rust
pub fn encode_frame(frame: &Frame) -> Result<Bytes, VstpError> {
    let mut buf = BytesMut::new();
```

**Purpose**: Convert Frame to wire format
**Technical Details**:

- `&Frame`: Borrows frame (no ownership transfer)
- `Result<Bytes, VstpError>`: Returns encoded bytes or error
- `BytesMut::new()`: Mutable buffer for building

```rust
// Fixed header: [MAGIC (2B)] [VER (1B)] [TYPE (1B)] [FLAGS (1B)]
buf.put_slice(&VSTP_MAGIC);
buf.put_u8(frame.version);
buf.put_u8(frame.typ as u8);
buf.put_u8(frame.flags.bits());
```

**Purpose**: Write fixed 5-byte header
**Technical Details**:

- `put_slice()`: Writes byte array
- `put_u8()`: Writes single byte
- `as u8`: Converts enum to byte value
- `bits()`: Extracts flag bits

```rust
// Encode headers first to calculate total header length
let mut header_data = BytesMut::new();
for header in &frame.headers {
```

**Purpose**: Build header section separately
**Technical Details**:

- Separate buffer for header calculation
- `&frame.headers`: Iterates over borrowed headers

```rust
// Validate header key length
if header.key.len() > 255 {
    return Err(VstpError::Protocol("Header key too long".to_string()));
}
if header.value.len() > 255 {
    return Err(VstpError::Protocol("Header value too long".to_string()));
}
```

**Purpose**: Enforce header size limits
**Technical Details**:

- 255 byte limit (fits in u8)
- Early return on validation failure
- `.to_string()`: Creates owned String

```rust
// Write header: [KEY_LEN (1B)] [VALUE_LEN (1B)] [KEY] [VALUE]
header_data.put_u8(header.key.len() as u8);
header_data.put_u8(header.value.len() as u8);
header_data.put_slice(&header.key);
header_data.put_slice(&header.value);
```

**Purpose**: Encode individual header
**Technical Details**:

- Length prefixes for parsing
- `as u8`: Safe due to validation above
- `put_slice()`: Writes key and value bytes

```rust
// Write header length (little-endian) and payload length (big-endian)
buf.put_u16_le(header_data.len() as u16);
```

**Purpose**: Write header section length
**Technical Details**:

- `put_u16_le()`: Little-endian 16-bit integer
- `as u16`: Safe conversion (header_data.len() <= 65535)

```rust
// Write payload length in big-endian manually
let payload_len = frame.payload.len() as u32;
buf.put_u8((payload_len >> 24) as u8);
buf.put_u8((payload_len >> 16) as u8);
buf.put_u8((payload_len >> 8) as u8);
buf.put_u8(payload_len as u8);
```

**Purpose**: Write payload length in big-endian
**Technical Details**:

- Manual big-endian encoding (network byte order)
- `>>`: Right shift for byte extraction
- `as u8`: Extracts least significant byte
- Most significant byte first (big-endian)

```rust
// Write headers and payload
buf.put_slice(&header_data);
buf.put_slice(&frame.payload);
```

**Purpose**: Write variable data sections
**Technical Details**:

- `put_slice()`: Efficient bulk write
- Headers first, then payload

```rust
// Calculate CRC over the entire frame (excluding CRC field)
let mut crc = CRC::crc32();
crc.digest(&buf);
let crc_value = crc.get_crc() as u32;
```

**Purpose**: Compute data integrity checksum
**Technical Details**:

- `CRC::crc32()`: IEEE 802.3 CRC-32 algorithm
- `digest()`: Processes all bytes in buffer
- `get_crc()`: Returns computed checksum
- `as u32`: Type conversion

```rust
// Write CRC (big-endian)
buf.put_u8((crc_value >> 24) as u8);
buf.put_u8((crc_value >> 16) as u8);
buf.put_u8((crc_value >> 8) as u8);
buf.put_u8(crc_value as u8);
```

**Purpose**: Append CRC to frame
**Technical Details**:

- Manual big-endian encoding
- Most significant byte first
- Consistent with payload length encoding

```rust
Ok(buf.freeze())
```

**Purpose**: Return immutable bytes
**Technical Details**:

- `freeze()`: Converts BytesMut to Bytes (immutable)
- `Ok()`: Wraps in Result success variant

### Decoding Function

```rust
pub fn try_decode_frame(
    buf: &mut BytesMut,
    max_frame_size: usize,
) -> Result<Option<Frame>, VstpError> {
```

**Purpose**: Parse wire format to Frame
**Technical Details**:

- `&mut BytesMut`: Mutable buffer reference
- `max_frame_size`: Prevents memory exhaustion
- `Option<Frame>`: None for incomplete frames

```rust
// Need at least 11 bytes for fixed header + lengths
if buf.len() < 11 {
    return Ok(None);
}
```

**Purpose**: Check minimum frame size
**Technical Details**:

- 5 bytes (fixed header) + 6 bytes (lengths) = 11 bytes
- `Ok(None)`: Indicates more data needed

```rust
// Check magic bytes
if buf[0] != VSTP_MAGIC[0] || buf[1] != VSTP_MAGIC[1] {
    return Err(VstpError::Protocol("Invalid magic bytes".to_string()));
}
```

**Purpose**: Validate protocol identifier
**Technical Details**:

- Direct array indexing for efficiency
- Early return on protocol mismatch
- Clear error message

```rust
// Parse fixed header
let version = buf[2];
let frame_type = buf[3];
let flags = buf[4];
```

**Purpose**: Extract header fields
**Technical Details**:

- Direct byte extraction
- No bounds checking (already verified length >= 11)

```rust
// Validate version
if version != VSTP_VERSION {
    return Err(VstpError::Protocol("Unsupported version".to_string()));
}
```

**Purpose**: Check protocol version compatibility
**Technical Details**:

- Strict version checking
- Prevents protocol confusion

```rust
// Parse lengths
let header_len = (&buf[5..7]).read_u16::<LittleEndian>().unwrap() as usize;
let payload_len = (&buf[7..11]).read_u32::<BigEndian>().unwrap() as usize;
```

**Purpose**: Extract length fields
**Technical Details**:

- `&buf[5..7]`: 2-byte slice for header length
- `&buf[7..11]`: 4-byte slice for payload length
- `read_u16::<LittleEndian>()`: Little-endian parsing
- `read_u32::<BigEndian>()`: Big-endian parsing
- `unwrap()`: Safe due to bounds checking
- `as usize`: Convert to size type

```rust
// Calculate total frame size
let total_size = 11 + header_len + payload_len + 4; // +4 for CRC
```

**Purpose**: Determine complete frame size
**Technical Details**:

- 11 bytes: Fixed header + lengths
- header_len: Variable header section
- payload_len: Variable payload section
- 4 bytes: CRC field

```rust
// Check size limits
if total_size > max_frame_size {
    return Err(VstpError::Protocol("Frame too large".to_string()));
}
```

**Purpose**: Prevent memory exhaustion
**Technical Details**:

- Early validation before allocation
- Configurable limit for different use cases

```rust
// Check if we have enough data
if buf.len() < total_size {
    return Ok(None);
}
```

**Purpose**: Verify complete frame available
**Technical Details**:

- `Ok(None)`: Indicates partial frame
- Allows streaming processing

```rust
// Extract the complete frame
let frame_data = buf.split_to(total_size);
```

**Purpose**: Remove frame from buffer
**Technical Details**:

- `split_to()`: Splits buffer at position
- Returns owned Bytes with frame data
- Removes frame from input buffer

```rust
// Verify CRC
let expected_crc = (&frame_data[total_size - 4..])
    .read_u32::<BigEndian>()
    .unwrap();
```

**Purpose**: Extract expected CRC
**Technical Details**:

- `total_size - 4`: Last 4 bytes contain CRC
- `read_u32::<BigEndian>()`: Big-endian parsing
- `unwrap()`: Safe due to bounds checking

```rust
let mut crc = CRC::crc32();
crc.digest(&frame_data[..total_size - 4]);
let calculated_crc = crc.get_crc() as u32;
```

**Purpose**: Calculate actual CRC
**Technical Details**:

- `&frame_data[..total_size - 4]`: All bytes except CRC
- Same algorithm as encoding
- Type conversion to u32

```rust
if expected_crc != calculated_crc {
    return Err(VstpError::CrcMismatch {
        expected: expected_crc,
        got: calculated_crc,
    });
}
```

**Purpose**: Validate data integrity
**Technical Details**:

- Structured error with both values
- Early return on corruption detection

```rust
// Parse frame type
let typ = match frame_type {
    0x01 => FrameType::Hello,
    0x02 => FrameType::Welcome,
    0x03 => FrameType::Data,
    0x04 => FrameType::Ping,
    0x05 => FrameType::Pong,
    0x06 => FrameType::Bye,
    0x07 => FrameType::Ack,
    0x08 => FrameType::Err,
    _ => return Err(VstpError::Protocol("Invalid frame type".to_string())),
};
```

**Purpose**: Convert byte to frame type
**Technical Details**:

- `match`: Exhaustive pattern matching
- `_`: Catch-all for invalid values
- Returns structured error

```rust
// Parse headers
let mut headers = Vec::new();
let mut header_pos = 11; // Start after fixed header
```

**Purpose**: Initialize header parsing
**Technical Details**:

- `Vec::new()`: Empty vector for headers
- `header_pos`: Track position in frame data
- Start at byte 11 (after fixed header + lengths)

```rust
while header_pos < 11 + header_len {
```

**Purpose**: Parse all headers
**Technical Details**:

- `11 + header_len`: End of header section
- `while`: Loop until all headers processed

```rust
if header_pos + 2 > frame_data.len() {
    return Err(VstpError::Protocol("Incomplete header length".to_string()));
}
```

**Purpose**: Check header length bytes available
**Technical Details**:

- Need 2 bytes for key_len and value_len
- Bounds checking for safety

```rust
let key_len = frame_data[header_pos] as usize;
let value_len = frame_data[header_pos + 1] as usize;
header_pos += 2;
```

**Purpose**: Extract header lengths
**Technical Details**:

- `as usize`: Convert u8 to size type
- `header_pos += 2`: Advance past length bytes

```rust
if header_pos + key_len + value_len > frame_data.len() {
    return Err(VstpError::Protocol("Incomplete header value".to_string()));
}
```

**Purpose**: Check header data available
**Technical Details**:

- Verify key and value bytes exist
- Prevents buffer overrun

```rust
let key = frame_data[header_pos..header_pos + key_len].to_vec();
header_pos += key_len;
let value = frame_data[header_pos..header_pos + value_len].to_vec();
header_pos += value_len;
```

**Purpose**: Extract header key and value
**Technical Details**:

- `[start..end]`: Byte slice extraction
- `to_vec()`: Create owned copy
- `header_pos +=`: Advance position

```rust
headers.push(Header { key, value });
```

**Purpose**: Add header to collection
**Technical Details**:

- `push()`: Add to end of vector
- `Header { key, value }`: Struct initialization

```rust
// Parse payload
let payload_start = 11 + header_len;
let payload_end = payload_start + payload_len;
let payload = frame_data[payload_start..payload_end].to_vec();
```

**Purpose**: Extract payload data
**Technical Details**:

- Calculate payload boundaries
- Extract byte slice
- Create owned copy

```rust
Ok(Some(Frame {
    version,
    typ,
    flags: Flags::from_bits(flags).unwrap_or(Flags::empty()),
    headers,
    payload,
}))
```

**Purpose**: Construct and return frame
**Technical Details**:

- `Flags::from_bits()`: Convert byte to flags
- `unwrap_or(Flags::empty())`: Handle invalid flags gracefully
- `Ok(Some())`: Success with frame
- Struct initialization with all fields

## `src/codec.rs` - Tokio Integration

### Codec Structure

```rust
pub struct VstpFrameCodec {
    max_frame_size: usize,
}
```

**Purpose**: Configuration for frame codec
**Technical Details**:

- Single field for size limit
- Configurable for different use cases

### Constructor Methods

```rust
impl VstpFrameCodec {
    pub fn new(max_frame_size: usize) -> Self {
        Self { max_frame_size }
    }

    pub fn default() -> Self {
        Self::new(8 * 1024 * 1024) // 8MB default
    }
}
```

**Purpose**: Create codec instances
**Technical Details**:

- `new()`: Custom size limit
- `default()`: 8MB reasonable default
- `8 * 1024 * 1024`: 8MB in bytes

### Decoder Implementation

```rust
impl Decoder for VstpFrameCodec {
    type Item = Frame;
    type Error = VstpError;

    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
        try_decode_frame(src, self.max_frame_size)
    }
}
```

**Purpose**: Implement Tokio's Decoder trait
**Technical Details**:

- `type Item = Frame`: Output type
- `type Error = VstpError`: Error type
- `decode()`: Called by Tokio for each buffer
- Delegates to `try_decode_frame()`

### Encoder Implementation

```rust
impl Encoder<Frame> for VstpFrameCodec {
    type Error = VstpError;

    fn encode(&mut self, item: Frame, dst: &mut BytesMut) -> Result<(), Self::Error> {
        let encoded = encode_frame(&item)?;
        dst.extend_from_slice(&encoded);
        Ok(())
    }
}
```

**Purpose**: Implement Tokio's Encoder trait
**Technical Details**:

- `Encoder<Frame>`: Generic over Frame type
- `encode()`: Called by Tokio to encode frames
- `encode_frame()`: Convert frame to bytes
- `extend_from_slice()`: Add to destination buffer

## `src/lib.rs` - Library Exports

### Module Declarations

```rust
pub mod types;
pub mod frame;
pub mod codec;
```

**Purpose**: Declare public modules
**Technical Details**:

- `pub mod`: Public module declaration
- Enables `crate::types::Frame` syntax

### Re-exports

```rust
pub use types::{
    Frame, FrameType, Header, Flags, VstpError, SessionId,
    VSTP_MAGIC, VSTP_VERSION,
};
```

**Purpose**: Convenient public API
**Technical Details**:

- `pub use`: Re-export for easier access
- Users can write `vstp_labs::Frame` instead of `vstp_labs::types::Frame`

```rust
pub use frame::{encode_frame, try_decode_frame};
pub use codec::VstpFrameCodec;
```

**Purpose**: Export main functions
**Technical Details**:

- Direct access to encoding/decoding functions
- Codec for async usage

## Testing Implementation

### Unit Tests

```rust
#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::Frame;
```

**Purpose**: Test module configuration
**Technical Details**:

- `#[cfg(test)]`: Only compiled in test mode
- `use super::*`: Import parent module items
- `use crate::types::Frame`: Import for testing

### Test Functions

```rust
#[test]
fn test_basic_roundtrip() {
    let frame = Frame::new(FrameType::Hello);
    let encoded = encode_frame(&frame).unwrap();
    let mut buf = BytesMut::from(&encoded[..]);
    let decoded = try_decode_frame(&mut buf, 1024).unwrap().unwrap();

    assert_eq!(frame, decoded);
}
```

**Purpose**: Test basic encoding/decoding
**Technical Details**:

- `#[test]`: Marks as test function
- `unwrap()`: Panic on error in tests
- `assert_eq!()`: Verify round-trip equality
- `BytesMut::from()`: Convert to mutable buffer

## Performance Optimizations

### Memory Management

- **Zero-copy**: Uses `Bytes` for efficient memory sharing
- **Buffer Reuse**: Minimizes allocations
- **Bounds Checking**: Prevents buffer overflows
- **Early Returns**: Avoid unnecessary work

### CPU Efficiency

- **Direct Access**: Array indexing for speed
- **Minimal Parsing**: Only parse what's needed
- **Efficient CRC**: Optimized CRC-32 calculation
- **Type Safety**: Compile-time error prevention

### Error Handling

- **Structured Errors**: Detailed error information
- **Graceful Degradation**: Handle partial data
- **Validation**: Early error detection
- **Recovery**: Continue processing after errors

This implementation provides a robust, efficient, and well-tested foundation for the VSTP protocol with comprehensive error handling, performance optimizations, and clear separation of concerns.