ffiber/
types.rs

1#[derive(Clone, PartialEq, Eq, Hash, Debug)]
2pub enum ArgType {
3    Primitive(String),
4    Struct { name: String, params: Vec<Box<ArgType>> },
5    Ref(Box<ArgType>),
6    RefMut(Box<ArgType>),
7    Buffer(Box<ArgType>),
8    Enum { name: String, variants: Vec<String> },
9}
10
11#[derive(Clone, PartialEq, Eq, Hash, Debug)]
12pub enum SelfArgType {
13    None,
14    Value,
15    Ref,
16    RefMut,
17    Mut,
18}
19
20#[derive(Clone, PartialEq, Eq, Hash, Debug)]
21pub enum DerivedTrait {
22    Default,
23    Clone,
24    PartialEq,
25    Eq,
26}
27
28impl SelfArgType {
29    pub fn is_ref(&self) -> bool {
30        match self {
31            SelfArgType::None => false,
32            SelfArgType::Value => false,
33            SelfArgType::Ref => true,
34            SelfArgType::RefMut => true,
35            SelfArgType::Mut => false,
36        }
37    }
38
39    pub fn is_none(&self) -> bool {
40        match self {
41            SelfArgType::None => true,
42            _ => false,
43        }
44    }
45
46    pub fn is_some(&self) -> bool {
47        !self.is_none()
48    }
49}
50
51impl ArgType {
52    pub fn new_struct(struct_name: &str) -> Self {
53        ArgType::Struct {
54            name: struct_name.to_string(),
55            params: vec![],
56        }
57    }
58
59    pub fn new_ref(struct_name: &str) -> Self {
60        ArgType::Ref(Box::new(ArgType::new_struct(struct_name)))
61    }
62
63    pub fn new_ref_mut(struct_name: &str) -> Self {
64        ArgType::RefMut(Box::new(ArgType::new_struct(struct_name)))
65    }
66
67    pub fn new_u8_buffer() -> Self {
68        ArgType::Buffer(Box::new(ArgType::Primitive("u8".to_string())))
69    }
70
71    pub fn is_buffer(&self) -> bool {
72        match self {
73            ArgType::Buffer(_) => true,
74            _ => false,
75        }
76    }
77
78    pub fn is_struct(&self) -> bool {
79        match self {
80            ArgType::Struct{..} => true,
81            _ => false,
82        }
83    }
84
85    pub fn to_c_str(&self) -> String {
86        match self {
87            ArgType::Primitive(ty) => ty.clone(),
88            ArgType::Struct{..} => "*mut ::std::os::raw::c_void".to_string(),
89            ArgType::Ref(_) => "*mut ::std::os::raw::c_void".to_string(),
90            ArgType::RefMut(_) => "*mut ::std::os::raw::c_void".to_string(),
91            ArgType::Buffer(ty) => format!("*const {}", ty.to_c_str()),
92            ArgType::Enum{..} => "usize".to_string(),
93        }
94    }
95
96    pub fn to_rust_str(&self) -> String {
97        match self {
98            ArgType::Primitive(ty) => ty.clone(),
99            ArgType::Struct { name, params } => if params.is_empty() {
100                name.clone()
101            } else {
102                format!("{}<{}>", &name, params.iter().map(|p| p.to_rust_str())
103                    .collect::<Vec<_>>().join(", "))
104            },
105            ArgType::Ref(ty) => format!("&{}", &ty.to_rust_str()),
106            ArgType::RefMut(ty) => format!("&mut {}", &ty.to_rust_str()),
107            ArgType::Buffer(ty) => format!("*const {}", match &**ty {
108                ArgType::Ref(inner_ty) => format!("*const {}", inner_ty.to_rust_str()),
109                ArgType::RefMut(inner_ty) => format!("*mut {}", inner_ty.to_rust_str()),
110                ArgType::Buffer(_) => unimplemented!(),
111                ty => ty.to_rust_str(),
112            }),
113            ArgType::Enum { name, .. } => name.clone(),
114        }
115    }
116}