witx_codegen/doc/
common.rs

1use crate::astype::*;
2
3pub trait Normalize {
4    fn as_str(&self) -> &str;
5
6    fn as_link(&self) -> String {
7        let s = self.as_str().trim().to_lowercase();
8        let s = s.trim_matches(|c: char| !(c.is_alphanumeric() || c == '_' || c == '-'));
9        format!("#{}", s)
10    }
11
12    fn as_type(&self) -> String {
13        format!("_[`{}`]({})_", self.as_str(), self.as_link())
14    }
15
16    fn as_fn(&self) -> String {
17        format!("[`{}()`]({})", self.as_str(), self.as_link())
18    }
19
20    fn as_fn_suffix(&self) -> String {
21        self.as_str().to_string()
22    }
23
24    fn as_var(&self) -> String {
25        format!("**`{}`**", self.as_str())
26    }
27
28    fn as_const(&self) -> String {
29        format!("**`{}`**", self.as_str())
30    }
31
32    fn as_namespace(&self) -> String {
33        format!("**[`{}`]({})**", self.as_str(), self.as_link())
34    }
35}
36
37impl<T: AsRef<str>> Normalize for T {
38    fn as_str(&self) -> &str {
39        self.as_ref()
40    }
41}
42
43pub trait ToLanguageRepresentation {
44    fn as_astype(&self) -> &ASType;
45
46    fn to_string(&self) -> String {
47        self.as_lang()
48    }
49
50    fn as_lang(&self) -> String {
51        match self.as_astype() {
52            ASType::Alias(alias) => alias.name.as_type(),
53            ASType::Bool => "`bool`".to_string(),
54            ASType::Char32 => "`char32`".to_string(),
55            ASType::Char8 => "`char8`".to_string(),
56            ASType::F32 => "`f32`".to_string(),
57            ASType::F64 => "`f64`".to_string(),
58            ASType::Handle(_resource_name) => "`handle`".to_string(),
59            ASType::ConstPtr(pointee) => format!("{} pointer", pointee.to_string()),
60            ASType::MutPtr(pointee) => format!("{} mutable pointer", pointee.to_string()),
61            ASType::Option(_) => todo!(),
62            ASType::Result(_) => todo!(),
63            ASType::S8 => "`i8`".to_string(),
64            ASType::S16 => "`i16`".to_string(),
65            ASType::S32 => "`i32`".to_string(),
66            ASType::S64 => "`i64`".to_string(),
67            ASType::U8 => "`u8`".to_string(),
68            ASType::U16 => "`u16`".to_string(),
69            ASType::U32 => "`u32`".to_string(),
70            ASType::U64 => "`u64`".to_string(),
71            ASType::USize => "`usize`".to_string(),
72            ASType::Void => "_(empty)_".to_string(),
73            ASType::Constants(_) => unimplemented!(),
74            ASType::Enum(enum_) => {
75                format!("{} enumeration", enum_.repr.as_ref().as_lang())
76            }
77            ASType::Struct(_) => unimplemented!(),
78            ASType::Tuple(tuple_members) => {
79                let tuple_types: Vec<_> =
80                    tuple_members.iter().map(|x| x.type_.to_string()).collect();
81                format!("({})", tuple_types.join(", "))
82            }
83            ASType::Union(_) => unimplemented!(),
84            ASType::Slice(element_type) => format!("{} mutable slice", element_type.as_lang()),
85            ASType::String(_) => "`string`".to_string(),
86            ASType::ReadBuffer(element_type) => format!("{} slice", element_type.as_lang()),
87            ASType::WriteBuffer(element_type) => {
88                format!("{} mutable slice", element_type.to_string())
89            }
90        }
91    }
92}
93
94impl ToLanguageRepresentation for ASType {
95    fn as_astype(&self) -> &ASType {
96        self
97    }
98}