1use crate::Error;
2use std::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
3
4pub trait ByteRangeBounds {
7 fn start_bound_bytes(&self) -> Bound<&[u8]>;
10
11 fn end_bound_bytes(&self) -> Bound<&[u8]>;
14}
15
16impl<T: AsRef<[u8]>> ByteRangeBounds for Range<T> {
17 fn start_bound_bytes(&self) -> Bound<&[u8]> {
18 Bound::Included(self.start.as_ref())
19 }
20
21 fn end_bound_bytes(&self) -> Bound<&[u8]> {
22 Bound::Excluded(self.end.as_ref())
23 }
24}
25
26impl<T: AsRef<[u8]>> ByteRangeBounds for RangeInclusive<T> {
27 fn start_bound_bytes(&self) -> Bound<&[u8]> {
28 Bound::Included(self.start().as_ref())
29 }
30
31 fn end_bound_bytes(&self) -> Bound<&[u8]> {
32 Bound::Included(self.end().as_ref())
33 }
34}
35
36impl<T: AsRef<[u8]>> ByteRangeBounds for RangeFrom<T> {
37 fn start_bound_bytes(&self) -> Bound<&[u8]> {
38 Bound::Included(self.start.as_ref())
39 }
40
41 fn end_bound_bytes(&self) -> Bound<&[u8]> {
42 Bound::Unbounded
43 }
44}
45
46impl<T: AsRef<[u8]>> ByteRangeBounds for RangeTo<T> {
47 fn start_bound_bytes(&self) -> Bound<&[u8]> {
48 Bound::Unbounded
49 }
50
51 fn end_bound_bytes(&self) -> Bound<&[u8]> {
52 Bound::Excluded(self.end.as_ref())
53 }
54}
55
56impl<T: AsRef<[u8]>> ByteRangeBounds for RangeToInclusive<T> {
57 fn start_bound_bytes(&self) -> Bound<&[u8]> {
58 Bound::Unbounded
59 }
60
61 fn end_bound_bytes(&self) -> Bound<&[u8]> {
62 Bound::Included(self.end.as_ref())
63 }
64}
65
66impl ByteRangeBounds for RangeFull {
67 fn start_bound_bytes(&self) -> Bound<&[u8]> {
68 Bound::Unbounded
69 }
70
71 fn end_bound_bytes(&self) -> Bound<&[u8]> {
72 Bound::Unbounded
73 }
74}
75
76impl<T: AsRef<[u8]>> ByteRangeBounds for (Bound<T>, Bound<T>) {
77 fn start_bound_bytes(&self) -> Bound<&[u8]> {
78 match &self.0 {
79 Bound::Included(value) => Bound::Included(value.as_ref()),
80 Bound::Excluded(value) => Bound::Excluded(value.as_ref()),
81 Bound::Unbounded => Bound::Unbounded,
82 }
83 }
84
85 fn end_bound_bytes(&self) -> Bound<&[u8]> {
86 match &self.1 {
87 Bound::Included(value) => Bound::Included(value.as_ref()),
88 Bound::Excluded(value) => Bound::Excluded(value.as_ref()),
89 Bound::Unbounded => Bound::Unbounded,
90 }
91 }
92}
93
94pub trait FixedRecord {
100 type Field: Copy + Eq;
103
104 const TOTAL_LEN: usize;
107
108 fn parse(src: &[u8]) -> Result<Self, Error>
111 where
112 Self: Sized;
113
114 fn to_bytes(&self) -> Vec<u8>;
117
118 fn field_name(field: Self::Field) -> &'static str;
121
122 fn field_bytes(&self, field: Self::Field) -> &[u8];
125}
126
127#[cfg(feature = "list")]
133pub trait RecordWithList: FixedRecord + Sized {
134 type List: From<Vec<Self>>;
137}
138
139pub trait SequenceFields<T: FixedRecord> {
142 fn to_sequence_fields(self) -> Vec<T::Field>;
145}