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
use bstr::{BStr, ByteSlice};
#[derive(Debug, PartialEq, Ord, PartialOrd, Eq, Hash, Clone, Copy)]
pub struct Key<'a> {
pub section_name: &'a str,
pub subsection_name: Option<&'a BStr>,
pub value_name: &'a str,
}
pub fn parse_unvalidated<'a>(input: impl Into<&'a BStr>) -> Option<Key<'a>> {
let input = input.into();
let mut tokens = input.splitn(2, |b| *b == b'.');
let section_name = tokens.next()?;
let subsection_or_key = tokens.next()?;
let mut tokens = subsection_or_key.rsplitn(2, |b| *b == b'.');
let (subsection_name, value_name) = match (tokens.next(), tokens.next()) {
(Some(key), Some(subsection)) => (Some(subsection.into()), key),
(Some(key), None) => (None, key),
(None, Some(_)) => unreachable!("iterator can't restart producing items"),
(None, None) => return None,
};
Some(Key {
section_name: section_name.to_str().ok()?,
subsection_name,
value_name: value_name.to_str().ok()?,
})
}