nimbus_fml/backends/kotlin/gen_structs/
common.rsuse heck::{CamelCase, MixedCase, ShoutySnakeCase};
use std::fmt::Display;
pub fn class_name(nm: &dyn Display) -> String {
nm.to_string().to_camel_case()
}
pub fn var_name(nm: &dyn Display) -> String {
nm.to_string().to_mixed_case()
}
pub fn enum_variant_name(nm: &dyn Display) -> String {
nm.to_string().to_shouty_snake_case()
}
pub fn quoted(string: &dyn Display) -> String {
let string = string.to_string();
if string.contains('"') || string.contains('\n') {
format!(r#""""{}""""#, string)
} else {
format!(r#""{}""#, string)
}
}
pub(crate) mod code_type {
use std::fmt::Display;
use crate::backends::{CodeOracle, CodeType};
pub(crate) fn property_getter(
ct: &dyn CodeType,
oracle: &dyn CodeOracle,
vars: &dyn Display,
prop: &dyn Display,
default: &dyn Display,
) -> String {
let getter = ct.value_getter(oracle, vars, prop);
let mapper = ct.value_mapper(oracle);
let default = ct
.defaults_mapper(oracle, &default, vars)
.unwrap_or_else(|| default.to_string());
let merger = ct.value_merger(oracle, &default);
let getter = match (mapper, merger) {
(Some(mapper), Some(merger)) => format!("{}?.{}?.{}", getter, mapper, merger),
(Some(mapper), None) => format!("{}?.{}", getter, mapper),
(None, Some(merger)) => format!("{}?.{}", getter, merger),
(None, None) => getter,
};
format!(
"{getter} ?: {fallback}",
getter = getter,
fallback = default
)
}
pub(crate) fn value_getter(
ct: &dyn CodeType,
oracle: &dyn CodeOracle,
vars: &dyn Display,
prop: &dyn Display,
) -> String {
let vt = ct.variables_type(oracle);
format!(
"{vars}.get{vt}(\"{prop}\")",
vars = vars,
vt = vt,
prop = prop
)
}
pub(crate) fn value_mapper(ct: &dyn CodeType, oracle: &dyn CodeOracle) -> Option<String> {
let transform = ct.create_transform(oracle)?;
Some(format!("let({})", transform))
}
}
#[cfg(test)]
mod unit_tests {
use super::*;
#[test]
fn test_quoted() {
assert_eq!(
quoted(&"no-quotes".to_string()),
"\"no-quotes\"".to_string()
);
assert_eq!(
quoted(&"a \"quoted\" string".to_string()),
"\"\"\"a \"quoted\" string\"\"\"".to_string()
);
}
}