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
use Read;
use crate;
/// Reads [metadata](https://github.com/jiricekcz/fef-specification/blob/main/metadata/Metadata.md) from a byte stream and returns it as an iterator.
///
/// For most use cases, you will want to use the [`read_metadata_as_vec`] function instead.
///
/// # Example
/// ```rust
/// # use fef::v0::read::read_metadata;
/// # use fef::v0::config::DEFAULT_CONFIG;
/// # use fef::v0::metadata::MetadataRecord;
/// # use fef::v0::raw::VariableLengthEnum;
/// # use fef::v0::metadata::NameMetadataRecordObj;
/// # use fef::v0::metadata::VariableNameMetadataRecordObj;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let bytes: Vec<u8> = vec![
/// 0x02, // 2 records
/// 0x13, // together 19 bytes
/// 0x01, // Name record
/// 0x08, // Total name record length
/// 0x07, // String length
/// b'F', b'o', b'r', b'm', b'u', b'l', b'a', // "Formula"
/// 0x02, // Variable name record
/// 0x03, // Total variable name record length
/// 0x01, // Variable with ID 1
/// 0x01, // String length
/// b'x', // "x"
/// 0x00, 0x00, 0x00, 0x00 // Padding
/// ];
///
/// let mut reader = &mut bytes.as_slice();
/// let mut metadata = read_metadata(&mut reader, &DEFAULT_CONFIG)?;
///
///
/// assert_eq!(metadata.next().ok_or("first record exists")??, MetadataRecord::Name(
/// NameMetadataRecordObj::new(
/// "Formula".to_string()
/// )
/// ));
///
/// assert_eq!(metadata.next().ok_or("second record exists")??, MetadataRecord::VariableName(
/// VariableNameMetadataRecordObj::new(
/// "x".to_string(),
/// VariableLengthEnum::from(1)
/// )
/// ));
///
/// assert!(metadata.next().is_none()); // No more records
///
/// drop(metadata); // dropping the iterator to access reader again
///
/// assert!(reader.is_empty()); // Padding was read and disregarded
/// # Ok(())
/// # }
Sized + Read, C: ?Sized + Config> + use<'a, 'b, R, C>,
MetadataHeaderReadError,
>
/// Reads [metadata](https://github.com/jiricekcz/fef-specification/blob/main/metadata/Metadata.md) from a byte stream and returns it as a vector.
///
/// The generic [`read_metadata`] function reads metadata as a lazy iterator. That can be useful, however most of the time you will want to read
/// all the metadata at once. This function does exactly that.
///
/// # Example
/// ```rust
/// # use fef::v0::read::read_metadata_as_vec;
/// # use fef::v0::config::DEFAULT_CONFIG;
/// # use fef::v0::metadata::MetadataRecord;
/// # use fef::v0::raw::VariableLengthEnum;
/// # use fef::v0::metadata::NameMetadataRecordObj;
/// # use fef::v0::metadata::VariableNameMetadataRecordObj;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let bytes: Vec<u8> = vec![
/// 0x02, // 2 records
/// 0x13, // together 5 bytes
/// 0x01, // Name record
/// 0x08, // Total name record length
/// 0x07, // String length
/// b'F', b'o', b'r', b'm', b'u', b'l', b'a', // "Formula"
/// 0x02, // Variable name record
/// 0x03, // Total variable name record length
/// 0x01, // Variable with ID 1
/// 0x01, // String length
/// b'x', // "x"
/// 0x00, 0x00, 0x00, 0x00 // Padding
/// ];
///
/// let mut reader = &mut bytes.as_slice();
/// let metadata = read_metadata_as_vec(&mut reader, &DEFAULT_CONFIG)?;
///
/// assert_eq!(metadata.len(), 2);
///
/// // Metadata are in order
/// assert_eq!(metadata[0], MetadataRecord::Name(
/// NameMetadataRecordObj::new(
/// "Formula".to_string()
/// )
/// ));
///
/// assert_eq!(metadata[1], MetadataRecord::VariableName(
/// VariableNameMetadataRecordObj::new(
/// "x".to_string(),
/// VariableLengthEnum::from(1)
/// )
/// ));
///
/// assert!(reader.is_empty()); // Padding was read and disregarded
/// # Ok(())
/// # }
Sized + Read, C: ?Sized + Config>