76 lines
2.7 KiB
Rust
76 lines
2.7 KiB
Rust
![]() |
use super::Token;
|
||
|
|
||
|
pub(crate) fn maybe_token_to_usize(
|
||
|
token: Option<&Token<'_>>,
|
||
|
) -> Result<Option<usize>, Box<dyn std::error::Error>> {
|
||
|
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::<usize>())
|
||
|
}
|
||
|
})
|
||
|
.map_or(Ok(None), |r| r.map(Some))?)
|
||
|
}
|
||
|
|
||
|
pub(crate) struct EmacsStandardProperties {
|
||
|
pub(crate) begin: Option<usize>,
|
||
|
#[allow(dead_code)]
|
||
|
pub(crate) post_affiliated: Option<usize>,
|
||
|
#[allow(dead_code)]
|
||
|
pub(crate) contents_begin: Option<usize>,
|
||
|
#[allow(dead_code)]
|
||
|
pub(crate) contents_end: Option<usize>,
|
||
|
pub(crate) end: Option<usize>,
|
||
|
#[allow(dead_code)]
|
||
|
pub(crate) post_blank: Option<usize>,
|
||
|
}
|
||
|
|
||
|
pub(crate) fn get_emacs_standard_properties(
|
||
|
emacs: &Token<'_>,
|
||
|
) -> Result<EmacsStandardProperties, Box<dyn std::error::Error>> {
|
||
|
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,
|
||
|
}
|
||
|
})
|
||
|
}
|