use super::Token; pub(crate) fn maybe_token_to_usize( token: Option<&Token<'_>>, ) -> Result, Box> { Ok(token .map(|token| token.as_atom()) .map_or(Ok(None), |r| r.map(Some))? .and_then(|val| { if val == "nil" { None } else { Some(val.parse::()) } }) .map_or(Ok(None), |r| r.map(Some))?) } pub(crate) struct EmacsStandardProperties { pub(crate) begin: Option, #[allow(dead_code)] pub(crate) post_affiliated: Option, #[allow(dead_code)] pub(crate) contents_begin: Option, #[allow(dead_code)] pub(crate) contents_end: Option, pub(crate) end: Option, #[allow(dead_code)] pub(crate) post_blank: Option, } pub(crate) fn get_emacs_standard_properties( emacs: &Token<'_>, ) -> Result> { let children = emacs.as_list()?; let attributes_child = children.get(1).ok_or("Should have an attributes child.")?; let attributes_map = attributes_child.as_map()?; let standard_properties = attributes_map.get(":standard-properties"); Ok(if standard_properties.is_some() { let mut std_props = standard_properties .expect("if statement proves its Some") .as_vector()? .iter(); let begin = maybe_token_to_usize(std_props.next())?; let post_affiliated = maybe_token_to_usize(std_props.next())?; let contents_begin = maybe_token_to_usize(std_props.next())?; let contents_end = maybe_token_to_usize(std_props.next())?; let end = maybe_token_to_usize(std_props.next())?; let post_blank = maybe_token_to_usize(std_props.next())?; EmacsStandardProperties { begin, post_affiliated, contents_begin, contents_end, end, post_blank, } } else { let begin = maybe_token_to_usize(attributes_map.get(":begin").copied())?; let end = maybe_token_to_usize(attributes_map.get(":end").copied())?; let contents_begin = maybe_token_to_usize(attributes_map.get(":contents-begin").copied())?; let contents_end = maybe_token_to_usize(attributes_map.get(":contents-end").copied())?; let post_blank = maybe_token_to_usize(attributes_map.get(":post-blank").copied())?; let post_affiliated = maybe_token_to_usize(attributes_map.get(":post-affiliated").copied())?; EmacsStandardProperties { begin, post_affiliated, contents_begin, contents_end, end, post_blank, } }) }