//! Utility methods for transpiling use shulkerbox::util::{MacroString, MacroStringPart}; fn normalize_program_identifier(identifier: S) -> String where S: AsRef, { 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(current_identifier: S, import_path: T) -> String where S: AsRef, T: AsRef, { 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::>(); 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(strings: I) -> MacroString where I: IntoIterator, { 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) } }, }) }