comparing begin and end offsets for top-level sections and headlines.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use nom::branch::alt;
|
||||
use nom::bytes::complete::escaped;
|
||||
use nom::bytes::complete::tag;
|
||||
@@ -44,6 +46,31 @@ impl<'s> Token<'s> {
|
||||
_ => Err("wrong token type"),
|
||||
}?)
|
||||
}
|
||||
|
||||
pub fn as_map<'p>(
|
||||
&'p self,
|
||||
) -> Result<HashMap<&'s str, &'p Token<'s>>, Box<dyn std::error::Error>> {
|
||||
let mut hashmap = HashMap::new();
|
||||
|
||||
let children = self.as_list()?;
|
||||
if children.len() % 2 != 0 {
|
||||
return Err("Expecting an even number of children".into());
|
||||
}
|
||||
let mut key: Option<&str> = None;
|
||||
for child in children.iter() {
|
||||
match key {
|
||||
None => {
|
||||
key = Some(child.as_atom()?);
|
||||
}
|
||||
Some(key_val) => {
|
||||
key = None;
|
||||
hashmap.insert(key_val, child);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Ok(hashmap)
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(ret, level = "debug")]
|
||||
|
||||
Reference in New Issue
Block a user