use crate::TypeName;
use lazy_static::lazy_static;
use std::collections::HashMap;
#[derive(Debug)]
pub(crate) struct TypeDetails {
pub(crate) cxx_replacement: Option<TypeName>,
pub(crate) cxx_name: Option<String>,
}
impl TypeDetails {
fn new(cxx_replacement: Option<TypeName>, cxx_name: Option<String>) -> Self {
TypeDetails {
cxx_replacement,
cxx_name,
}
}
}
lazy_static! {
pub(crate) static ref KNOWN_TYPES: HashMap<TypeName, TypeDetails> = {
let mut map = HashMap::new();
map.insert(
TypeName::new("std_unique_ptr"),
TypeDetails::new(Some(TypeName::new("UniquePtr")), None),
);
map.insert(
TypeName::new("std_string"),
TypeDetails::new(Some(TypeName::new("CxxString")), None),
);
for (cpp_type, rust_type) in (3..7)
.map(|x| 2i32.pow(x))
.map(|x| {
vec![
(format!("uint{}_t", x), format!("u{}", x)),
(format!("int{}_t", x), format!("i{}", x)),
]
})
.flatten()
{
map.insert(
TypeName::new(&rust_type),
TypeDetails::new(None, Some(cpp_type)),
);
}
map
};
}
#[cfg(test)]
mod tests {
use crate::TypeName;
#[test]
fn test_ints() {
assert_eq!(
super::KNOWN_TYPES
.get(&TypeName::new("i8"))
.unwrap()
.cxx_name
.as_ref()
.unwrap(),
"int8_t"
);
assert_eq!(
super::KNOWN_TYPES
.get(&TypeName::new("u64"))
.unwrap()
.cxx_name
.as_ref()
.unwrap(),
"uint64_t"
);
}
}