1use std::str::SplitWhitespace;
2
3use crate::FromTokens;
4
5pub trait ToTokens<'a> {
9 type Iter: Iterator<Item = &'a str>;
10
11 fn to_tokens(&'a self) -> Self::Iter;
13
14 fn parse_tokens<T>(&'a self) -> Result<T, T::Error>
32 where
33 T: FromTokens,
34 {
35 T::from_tokens(self.to_tokens())
36 }
37}
38
39impl<'a> ToTokens<'a> for str {
40 type Iter = SplitWhitespace<'a>;
41
42 fn to_tokens(&'a self) -> Self::Iter {
44 self.split_whitespace()
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use crate::ToTokens;
51
52 #[test]
53 fn splits_string_at_whitespace() {
54 let tokens = "0 1".to_tokens();
55 let tokens: Vec<&str> = tokens.collect();
56 assert_eq!(tokens, vec!["0", "1"]);
57 }
58
59 #[test]
60 fn parse_tokens_parses_tokens() {
61 let numbers: Vec<u8> = "1 2".parse_tokens().unwrap();
62 assert_eq!(numbers, vec![1, 2]);
63 }
64}