use super::{Answer, Builtin, Kind};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Transform {
UnderlyingType,
MakeIntegerSeq,
TypePackElement,
}
const TRANSFORMS: &[(&str, Transform)] = &[
("__underlying_type", Transform::UnderlyingType),
("__make_integer_seq", Transform::MakeIntegerSeq),
("__type_pack_element", Transform::TypePackElement),
];
pub fn named(name: &str) -> Option<Builtin> {
TRANSFORMS
.iter()
.find(|(spelling, _)| *spelling == name)
.map(|(spelling, transform)| Builtin {
name: spelling,
arity: None,
kind: Kind::Transform(*transform),
})
}
pub fn apply(transform: Transform, arguments: &[String]) -> Answer {
match transform {
Transform::TypePackElement => {
let Some((index, rest)) = arguments.split_first() else {
return Answer::Unmodelled;
};
let Ok(index) = index.trim().parse::<usize>() else {
return Answer::Unmodelled;
};
match rest.get(index) {
Some(written) => Answer::Type(crate::sema::types::parse_type_spelling(written)),
None => Answer::Unmodelled,
}
}
Transform::UnderlyingType | Transform::MakeIntegerSeq => Answer::Unmodelled,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_two_real_transforms_are_recognised_and_not_answered_for() {
for (spelling, _) in TRANSFORMS {
let builtin = named(spelling).expect("the name is in the table");
assert_eq!(
builtin.kind,
Kind::Transform(
TRANSFORMS
.iter()
.find(|(name, _)| name == spelling)
.expect("just found")
.1
)
);
assert_eq!(
apply(Transform::UnderlyingType, &["E".to_string()]),
Answer::Unmodelled
);
}
assert!(
named("__decay").is_none(),
"an expression builtin is not a type-producing one"
);
}
#[test]
fn the_pack_element_a_literal_index_names_is_that_type() {
let arguments = |parts: &[&str]| {
parts
.iter()
.map(|one| one.to_string())
.collect::<Vec<String>>()
};
assert_eq!(
apply(
Transform::TypePackElement,
&arguments(&["1", "int", "double"])
),
Answer::Type(crate::sema::types::parse_type_spelling("double"))
);
assert_eq!(
apply(Transform::TypePackElement, &arguments(&["0", "const int*"])),
Answer::Type(crate::sema::types::parse_type_spelling("const int*")),
"and the element keeps its own spelling, qualifiers and all"
);
assert_eq!(
apply(Transform::TypePackElement, &arguments(&["_Idx", "int"])),
Answer::Unmodelled
);
assert_eq!(
apply(Transform::TypePackElement, &arguments(&["4", "int"])),
Answer::Unmodelled
);
assert_eq!(
apply(Transform::TypePackElement, &arguments(&[])),
Answer::Unmodelled
);
}
}