74 lines
2.2 KiB
Rust
74 lines
2.2 KiB
Rust
//! Utility methods for transpiling
|
|
|
|
use shulkerbox::util::{MacroString, MacroStringPart};
|
|
|
|
fn normalize_program_identifier<S>(identifier: S) -> String
|
|
where
|
|
S: AsRef<str>,
|
|
{
|
|
identifier
|
|
.as_ref()
|
|
.split('/')
|
|
.fold(Vec::new(), |mut acc, el| match el {
|
|
"." | "" => acc,
|
|
".." => {
|
|
acc.pop();
|
|
acc
|
|
}
|
|
_ => {
|
|
acc.push(el);
|
|
acc
|
|
}
|
|
})
|
|
.join("/")
|
|
}
|
|
|
|
/// Calculate the identifier to import the function based on the current identifier and the import path
|
|
#[must_use]
|
|
pub fn calculate_import_identifier<S, T>(current_identifier: S, import_path: T) -> String
|
|
where
|
|
S: AsRef<str>,
|
|
T: AsRef<str>,
|
|
{
|
|
if import_path.as_ref().starts_with('/') {
|
|
normalize_program_identifier(&import_path.as_ref()[1..])
|
|
} else {
|
|
let mut identifier_elements = current_identifier.as_ref().split('/').collect::<Vec<_>>();
|
|
identifier_elements.pop();
|
|
normalize_program_identifier(identifier_elements.join("/") + "/" + import_path.as_ref())
|
|
}
|
|
}
|
|
|
|
/// Join multiple macro strings into one
|
|
#[must_use]
|
|
pub fn join_macro_strings<I>(strings: I) -> MacroString
|
|
where
|
|
I: IntoIterator<Item = MacroString>,
|
|
{
|
|
strings
|
|
.into_iter()
|
|
.fold(MacroString::String(String::new()), |acc, cur| match acc {
|
|
MacroString::String(mut s) => match cur {
|
|
MacroString::String(cur) => {
|
|
s.push_str(&cur);
|
|
MacroString::String(s)
|
|
}
|
|
MacroString::MacroString(cur) => {
|
|
let mut parts = vec![MacroStringPart::String(s)];
|
|
parts.extend(cur);
|
|
MacroString::MacroString(parts)
|
|
}
|
|
},
|
|
MacroString::MacroString(mut parts) => match cur {
|
|
MacroString::String(cur) => {
|
|
parts.push(MacroStringPart::String(cur));
|
|
MacroString::MacroString(parts)
|
|
}
|
|
MacroString::MacroString(cur) => {
|
|
parts.extend(cur);
|
|
MacroString::MacroString(parts)
|
|
}
|
|
},
|
|
})
|
|
}
|