heim_common/utils/
iter.rs1use std::convert::TryFrom;
4use std::io;
5use std::str::FromStr;
6
7use crate::{Error, Result};
8
9pub trait TryIterator: Iterator {
13 fn try_next(&mut self) -> Result<<Self as Iterator>::Item>;
16
17 fn try_from_next<R, E>(&mut self) -> Result<R>
22 where
23 R: TryFrom<<Self as Iterator>::Item, Error = E>,
24 Error: From<E>;
25}
26
27pub trait ParseIterator<I>: TryIterator<Item = I>
31where
32 I: AsRef<str>,
33{
34 fn try_parse_next<R, E>(&mut self) -> Result<R>
39 where
40 R: FromStr<Err = E>,
41 Error: From<E>;
42}
43
44impl<T> TryIterator for T
45where
46 T: Iterator,
47{
48 fn try_next(&mut self) -> Result<<Self as Iterator>::Item> {
49 self.next()
50 .ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))
51 .map_err(Into::into)
52 }
53
54 fn try_from_next<R, E>(&mut self) -> Result<R>
55 where
56 R: TryFrom<<Self as Iterator>::Item, Error = E>,
57 Error: From<E>,
58 {
59 let value = self.try_next()?;
60
61 TryFrom::try_from(value).map_err(Into::into)
62 }
63}
64
65impl<T, I> ParseIterator<I> for T
66where
67 T: TryIterator<Item = I>,
68 I: AsRef<str>,
69{
70 fn try_parse_next<R, E>(&mut self) -> Result<R>
71 where
72 R: FromStr<Err = E>,
73 Error: From<E>,
74 {
75 let value = self.try_next()?;
76
77 FromStr::from_str(value.as_ref()).map_err(Into::into)
78 }
79}