comparing begin and end offsets for top-level sections and headlines.

This commit is contained in:
Tom Alexander
2023-04-11 19:16:04 -04:00
parent 276e8abb13
commit 52b401d548
6 changed files with 109 additions and 6 deletions

View File

@@ -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")]