xenon_codegen/
type.rs

1use core::fmt;
2
3use crate::identifier::IdentifierAccess;
4
5#[derive(Debug, Clone, Default)]
6pub struct Type {
7    pub name: IdentifierAccess,
8    pub typetype: TypeType,
9    pub generic_child: Option<Box<Type>>,
10}
11impl Type {
12    pub fn new(name: IdentifierAccess, typetype: TypeType) -> Type {
13        Type {
14            name,
15            typetype,
16            generic_child: None,
17        }
18    }
19
20    pub fn is_valid(&self) -> bool {
21        if !self.name.is_valid() {
22            return false;
23        }
24        if let Some(g) = self.generic_child.clone() {
25            if !g.is_valid() {
26                return false;
27            }
28        }
29        true
30    }
31}
32impl fmt::Display for Type {
33    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        if self.typetype == TypeType::Pointer {
35            match write!(fmt, "*") {
36                Ok(_) => (),
37                Err(e) => return Err(e),
38            }
39        }
40        match write!(fmt, "{}", self.name) {
41            Ok(_) => (),
42            Err(e) => return Err(e),
43        }
44        if let Some(g) = self.generic_child.clone() {
45            match write!(fmt, "<{}>", g) {
46                Ok(_) => (),
47                Err(e) => return Err(e),
48            }
49        }
50
51        Ok(())
52    }
53}
54
55#[derive(Debug, Clone, Default, PartialEq, Eq)]
56pub enum TypeType {
57    #[default]
58    Normal,
59    Pointer,
60}