Skip to main content

fixed_record/
traits.rs

1use crate::Error;
2use std::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
3
4/// Converts standard Rust range types with byte-like bounds into borrowed byte bounds.
5/// バイト列として参照できる境界を持つ標準 Rust range 型を、借用バイト境界へ変換します。
6pub trait ByteRangeBounds {
7    /// Returns the start bound as borrowed bytes.
8    /// 開始境界を借用バイト列として返します。
9    fn start_bound_bytes(&self) -> Bound<&[u8]>;
10
11    /// Returns the end bound as borrowed bytes.
12    /// 終了境界を借用バイト列として返します。
13    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
94/// Common interface for types that can be handled as fixed-width records.
95/// 固定長レコードとして扱える型の共通インターフェースです。
96///
97/// This is usually implemented by the `#[fixed_record]` attribute macro.
98/// 通常は `#[fixed_record]` attribute macro によって自動実装されます。
99pub trait FixedRecord {
100    /// The field enum type that can be used for sequence checks.
101    /// シーケンスチェックで指定できるフィールド enum の型です。
102    type Field: Copy + Eq;
103
104    /// Total byte length of the record.
105    /// レコード全体のバイト長です。
106    const TOTAL_LEN: usize;
107
108    /// Creates a record from a byte slice.
109    /// バイト列からレコードを作成します。
110    fn parse(src: &[u8]) -> Result<Self, Error>
111    where
112        Self: Sized;
113
114    /// Copies the record into its fixed-width byte representation.
115    /// レコードを固定長バイト列へコピーして返します。
116    fn to_bytes(&self) -> Vec<u8>;
117
118    /// Returns the declaration name of the specified field.
119    /// 指定フィールドの定義名を返します。
120    fn field_name(field: Self::Field) -> &'static str;
121
122    /// Returns the bytes stored in the specified field.
123    /// 指定フィールドのバイト列を返します。
124    fn field_bytes(&self, field: Self::Field) -> &[u8];
125}
126
127/// Associates a fixed-width record with its generated List type.
128/// 固定長レコードと、そのレコード用に生成された List 型を関連付けます。
129///
130/// This is implemented automatically by `#[fixed_record]` when the `list` feature is enabled.
131/// `list` feature が有効な場合に `#[fixed_record]` が自動実装します。
132#[cfg(feature = "list")]
133pub trait RecordWithList: FixedRecord + Sized {
134    /// Generated List type that can be built from these records.
135    /// このレコード列から構築できる生成済み List 型です。
136    type List: From<Vec<Self>>;
137}
138
139/// Field arrays that can be passed to `Reader` sequence checks.
140/// `Reader` のシーケンスチェックに指定できるフィールド配列です。
141pub trait SequenceFields<T: FixedRecord> {
142    /// Converts the sequence-check field list into a `Vec`.
143    /// シーケンスチェック対象フィールドを `Vec` にして返します。
144    fn to_sequence_fields(self) -> Vec<T::Field>;
145}