witx_codegen/cpp/
common.rs1use super::tuple::Tuple;
2use crate::astype::*;
3use convert_case::{Case, Casing};
4
5pub trait IsNullable {
6 fn is_nullable(&self) -> bool;
7}
8
9impl IsNullable for ASType {
10 fn is_nullable(&self) -> bool {
11 matches!(
12 self,
13 ASType::ConstPtr(_)
14 | ASType::MutPtr(_)
15 | ASType::ReadBuffer(_)
16 | ASType::WriteBuffer(_)
17 | ASType::Enum(_)
18 | ASType::Struct(_)
19 | ASType::Tuple(_)
20 | ASType::Union(_)
21 )
22 }
23}
24
25pub trait Normalize {
26 fn as_str(&self) -> &str;
27
28 fn as_type(&self) -> String {
29 self.as_str().to_case(Case::Pascal)
30 }
31
32 fn as_fn(&self) -> String {
33 self.as_str().to_case(Case::Snake)
34 }
35
36 fn as_fn_suffix(&self) -> String {
37 self.as_str().to_case(Case::Snake)
38 }
39
40 fn as_var(&self) -> String {
41 self.as_str().to_case(Case::Snake)
42 }
43
44 fn as_const(&self) -> String {
45 self.as_str().to_case(Case::UpperSnake)
46 }
47
48 fn as_namespace(&self) -> String {
49 self.as_str().to_case(Case::Pascal)
50 }
51}
52
53impl<T: AsRef<str>> Normalize for T {
54 fn as_str(&self) -> &str {
55 self.as_ref()
56 }
57}
58
59pub trait ToLanguageRepresentation {
60 fn as_astype(&self) -> &ASType;
61
62 fn to_string(&self) -> String {
63 self.as_lang()
64 }
65
66 fn as_lang(&self) -> String {
67 match self.as_astype() {
68 ASType::Alias(alias) => alias.name.as_type(),
69 ASType::Bool => "bool".to_string(),
70 ASType::Char32 => "char32_t".to_string(),
71 ASType::Char8 => "unsigned char".to_string(),
72 ASType::F32 => "float".to_string(),
73 ASType::F64 => "double".to_string(),
74 ASType::Handle(_resource_name) => "WasiHandle".to_string(),
75 ASType::ConstPtr(pointee) => format!("WasiPtr<{}>", pointee.to_string()),
76 ASType::MutPtr(pointee) => format!("WasiMutPtr<{}>", pointee.to_string()),
77 ASType::Option(_) => todo!(),
78 ASType::Result(_) => todo!(),
79 ASType::S8 => "int8_t".to_string(),
80 ASType::S16 => "int16_t".to_string(),
81 ASType::S32 => "int32_t".to_string(),
82 ASType::S64 => "int64_t".to_string(),
83 ASType::U8 => "uint8_t".to_string(),
84 ASType::U16 => "uint16_t".to_string(),
85 ASType::U32 => "uint32_t".to_string(),
86 ASType::U64 => "uint64_t".to_string(),
87 ASType::USize => "size_t".to_string(),
88 ASType::Void => "void".to_string(),
89 ASType::Constants(_) => unimplemented!(),
90 ASType::Enum(enum_) => {
91 format!("{} /* Enum */", enum_.repr.as_ref().as_lang())
92 }
93 ASType::Struct(_) => unimplemented!(),
94 ASType::Tuple(tuple_members) => Tuple::name_for(tuple_members).as_type(),
95 ASType::Union(_) => unimplemented!(),
96 ASType::Slice(element_type) => format!("WasiMutSlice<{}>", element_type.as_lang()),
97 ASType::String(_) => "WasiString".to_string(),
98 ASType::ReadBuffer(element_type) => format!("WasiSlice<{}>", element_type.as_lang()),
99 ASType::WriteBuffer(element_type) => {
100 format!("WasiMutSlice<{}>", element_type.to_string())
101 }
102 }
103 }
104}
105
106impl ToLanguageRepresentation for ASType {
107 fn as_astype(&self) -> &ASType {
108 self
109 }
110}