Skip to content

Commit c008654

Browse files
committed
pem: move from_slice() to SliceIter::read_section()
1 parent d9f1404 commit c008654

File tree

1 file changed

+39
-42
lines changed

1 file changed

+39
-42
lines changed

src/pem.rs

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,40 @@ impl<'a, T: PemObject> SliceIter<'a, T> {
148148
}
149149
}
150150

151+
/// Extract and decode the next supported PEM section from `input`
152+
///
153+
/// - `Ok(None)` is returned if there is no PEM section to read from `input`
154+
/// - Syntax errors and decoding errors produce a `Err(...)`
155+
/// - Otherwise each decoded section is returned with a `Ok(Some((..., remainder)))` where
156+
/// `remainder` is the part of the `input` that follows the returned section
157+
fn read_section(&mut self) -> Result<Option<(SectionKind, Vec<u8>)>, Error> {
158+
let mut b64buf = Vec::with_capacity(1024);
159+
let mut section = None;
160+
161+
loop {
162+
let next_line = if let Some(index) = self
163+
.current
164+
.iter()
165+
.position(|byte| *byte == b'\n' || *byte == b'\r')
166+
{
167+
let (line, newline_plus_remainder) = self.current.split_at(index);
168+
self.current = &newline_plus_remainder[1..];
169+
Some(line)
170+
} else if !self.current.is_empty() {
171+
let next_line = self.current;
172+
self.current = &[];
173+
Some(next_line)
174+
} else {
175+
None
176+
};
177+
178+
match read(next_line, &mut section, &mut b64buf)? {
179+
ControlFlow::Continue(()) => continue,
180+
ControlFlow::Break(item) => return Ok(item),
181+
}
182+
}
183+
}
184+
151185
/// Returns the rest of the unparsed data.
152186
///
153187
/// This is the slice immediately following the most
@@ -163,14 +197,11 @@ impl<T: PemObject> Iterator for SliceIter<'_, T> {
163197

164198
fn next(&mut self) -> Option<Self::Item> {
165199
loop {
166-
return match from_slice(self.current) {
167-
Ok(Some(((sec, item), rest))) => {
168-
self.current = rest;
169-
match T::from_pem(sec, item) {
170-
Some(res) => Some(Ok(res)),
171-
None => continue,
172-
}
173-
}
200+
return match self.read_section() {
201+
Ok(Some((sec, item))) => match T::from_pem(sec, item) {
202+
Some(res) => Some(Ok(res)),
203+
None => continue,
204+
},
174205
Ok(None) => return None,
175206
Err(err) => Some(Err(err)),
176207
};
@@ -184,40 +215,6 @@ impl PemObject for (SectionKind, Vec<u8>) {
184215
}
185216
}
186217

187-
/// Extract and decode the next supported PEM section from `input`
188-
///
189-
/// - `Ok(None)` is returned if there is no PEM section to read from `input`
190-
/// - Syntax errors and decoding errors produce a `Err(...)`
191-
/// - Otherwise each decoded section is returned with a `Ok(Some((..., remainder)))` where
192-
/// `remainder` is the part of the `input` that follows the returned section
193-
#[allow(clippy::type_complexity)]
194-
fn from_slice(mut input: &[u8]) -> Result<Option<((SectionKind, Vec<u8>), &[u8])>, Error> {
195-
let mut b64buf = Vec::with_capacity(1024);
196-
let mut section = None;
197-
198-
loop {
199-
let next_line = if let Some(index) = input
200-
.iter()
201-
.position(|byte| *byte == b'\n' || *byte == b'\r')
202-
{
203-
let (line, newline_plus_remainder) = input.split_at(index);
204-
input = &newline_plus_remainder[1..];
205-
Some(line)
206-
} else if !input.is_empty() {
207-
let next_line = input;
208-
input = &[];
209-
Some(next_line)
210-
} else {
211-
None
212-
};
213-
214-
match read(next_line, &mut section, &mut b64buf)? {
215-
ControlFlow::Continue(()) => continue,
216-
ControlFlow::Break(item) => return Ok(item.map(|item| (item, input))),
217-
}
218-
}
219-
}
220-
221218
/// Extract and decode the next supported PEM section from `rd`.
222219
///
223220
/// - Ok(None) is returned if there is no PEM section read from `rd`.

0 commit comments

Comments
 (0)