Add decorator
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
use std::path::PathBuf;
|
||||
use pest::iterators::{Pair, Pairs};
|
||||
use crate::parser::Rule;
|
||||
|
||||
pub const FILE_EXTENSION: &str = "mg";
|
||||
|
||||
@@ -14,4 +16,72 @@ pub(crate) fn expand_tilde(path: PathBuf) -> color_eyre::Result<PathBuf> {
|
||||
return Ok(home.join(relative));
|
||||
}
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
pub(crate) fn expand_next_rule(mut rule: Pairs<Rule>) -> Option<Pairs<Rule>> {
|
||||
if let Some(inner) = rule.next() {
|
||||
Some(inner.into_inner())
|
||||
} else { None }
|
||||
}
|
||||
|
||||
pub(crate) fn unbox_string(string: Pair<Rule>) -> String {
|
||||
let str = string.as_str();
|
||||
str[1..str.len()-1].to_string()
|
||||
}
|
||||
|
||||
pub(crate) fn format_pair(pair: Pair<Rule>, indent_level: usize, is_newline: bool) -> String {
|
||||
let indent = if is_newline {
|
||||
" ".repeat(indent_level)
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
|
||||
let children: Vec<_> = pair.clone().into_inner().collect();
|
||||
let len = children.len();
|
||||
let children: Vec<_> = children
|
||||
.into_iter()
|
||||
.map(|pair| {
|
||||
format_pair(
|
||||
pair,
|
||||
if len > 1 {
|
||||
indent_level + 1
|
||||
} else {
|
||||
indent_level
|
||||
},
|
||||
len > 1,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let dash = if is_newline { "- " } else { "" };
|
||||
let pair_tag = match pair.as_node_tag() {
|
||||
Some(tag) => format!("(#{}) ", tag),
|
||||
None => String::new(),
|
||||
};
|
||||
match len {
|
||||
0 => format!(
|
||||
"{}{}{}{:?}: {:?}",
|
||||
indent,
|
||||
dash,
|
||||
pair_tag,
|
||||
pair.as_rule(),
|
||||
pair.as_span().as_str()
|
||||
),
|
||||
1 => format!(
|
||||
"{}{}{}{:?} > {}",
|
||||
indent,
|
||||
dash,
|
||||
pair_tag,
|
||||
pair.as_rule(),
|
||||
children[0]
|
||||
),
|
||||
_ => format!(
|
||||
"{}{}{}{:?}\n{}",
|
||||
indent,
|
||||
dash,
|
||||
pair_tag,
|
||||
pair.as_rule(),
|
||||
children.join("\n")
|
||||
),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user