Files
magma-mc/src/helpers/mod.rs
2026-02-19 18:23:20 +08:00

87 lines
2.2 KiB
Rust

use std::path::PathBuf;
use pest::iterators::{Pair, Pairs};
use crate::parser::Rule;
pub const FILE_EXTENSION: &str = "mg";
pub(crate) fn expand_tilde(path: PathBuf) -> color_eyre::Result<PathBuf> {
if path.starts_with("~") {
let Some(home) = dirs::home_dir() else {
return Err(color_eyre::eyre::eyre!("No home directory found"));
};
if path == PathBuf::from("~") {
return Ok(home);
}
let relative = path.strip_prefix("~")?;
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")
),
}
}