rusty_bind_parser/
binding_types.rs

1//
2// Wildland Project
3//
4// Copyright © 2022 Golem Foundation,
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License version 3 as published by
8// the Free Software Foundation.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
18use std::hash::Hash;
19
20use derivative::Derivative;
21use proc_macro2::{Ident, TokenStream};
22use syn::Type;
23
24#[derive(Hash, PartialEq, Eq, Clone, Debug)]
25pub enum Exceptions {
26    Primitive(Vec<Ident>),
27    NonPrimitive(Vec<Ident>),
28}
29
30#[derive(Hash, PartialEq, Eq, Clone, Debug)]
31pub enum RustWrapperType {
32    Result(Box<WrapperType>, Box<WrapperType>),
33    Option(Box<WrapperType>),
34    Vector(Box<WrapperType>),
35    Ptr(Box<WrapperType>),
36    ArcMutex,
37    Arc,
38    Custom,
39    String,
40    Primitive,
41    Trait,
42    FieldlessEnum,
43    DataEnum,
44    Exceptions(Exceptions),
45    ExceptionTrait,
46}
47
48#[derive(Clone, Eq, PartialEq, Hash, Debug)]
49pub struct ReferenceParameters {
50    pub is_static: bool,
51    pub is_mut: bool,
52    pub boxed: bool,
53}
54
55impl ReferenceParameters {
56    pub fn shared() -> ReferenceParameters {
57        ReferenceParameters {
58            is_static: false,
59            is_mut: false,
60            boxed: false,
61        }
62    }
63}
64
65#[derive(Clone, Debug, Hash, PartialEq)]
66pub struct RustTrait {
67    pub name: String,
68    // True if a trait does not implement any methods or implements all methods implicitly and those methods are not exposed by the ffi
69    pub has_methods: bool,
70}
71
72#[derive(Clone, Debug, Derivative)]
73#[derivative(Eq, PartialEq, Hash)]
74pub struct WrapperType {
75    pub original_type_name: Type,
76    pub wrapper_name: String,
77    pub rust_type: RustWrapperType,
78    // Vec<(rust_trait_name, is_extension)>
79    pub impl_traits: Vec<RustTrait>,
80    #[derivative(PartialEq = "ignore", Hash = "ignore")]
81    pub reference_parameters: Option<ReferenceParameters>,
82}
83
84impl WrapperType {
85    pub fn boxed(self) -> Box<WrapperType> {
86        Box::new(self)
87    }
88}
89
90#[derive(Clone, Debug, PartialEq, Eq, Hash)]
91pub struct Arg {
92    pub arg_name: String,
93    pub typ: WrapperType,
94}
95
96impl Arg {
97    pub fn new(arg_name: &str, type_: &str, ref_params: Option<ReferenceParameters>) -> Arg {
98        Self {
99            arg_name: arg_name.to_owned(),
100            typ: WrapperType {
101                original_type_name: syn::parse_str(type_).unwrap(),
102                wrapper_name: type_.to_owned(),
103                rust_type: RustWrapperType::Custom,
104                reference_parameters: ref_params,
105                impl_traits: vec![],
106            },
107        }
108    }
109
110    pub fn primitive(arg_name: &str, type_: &str, ref_params: Option<ReferenceParameters>) -> Arg {
111        Self {
112            arg_name: arg_name.to_owned(),
113            typ: WrapperType {
114                original_type_name: syn::parse_str(type_).unwrap(),
115                wrapper_name: type_.to_owned(),
116                rust_type: RustWrapperType::Primitive,
117                reference_parameters: ref_params,
118                impl_traits: vec![],
119            },
120        }
121    }
122
123    pub fn _default(arg_name: &str, type_: &str) -> Arg {
124        Self::new(
125            arg_name,
126            type_,
127            Some(ReferenceParameters {
128                is_static: true,
129                is_mut: false,
130                boxed: false,
131            }),
132        )
133    }
134
135    pub fn _self(type_: &str) -> Arg {
136        Self::_default("self", type_)
137    }
138
139    pub fn _mut_self(type_: &str) -> Arg {
140        Self::new(
141            "self",
142            type_,
143            Some(ReferenceParameters {
144                is_static: true,
145                is_mut: true,
146                boxed: false,
147            }),
148        )
149    }
150}
151
152#[derive(Clone, Debug, Hash, Eq, PartialEq)]
153pub struct Function {
154    pub arguments: Vec<Arg>,
155    pub return_type: Option<WrapperType>,
156    pub name: String,
157}
158
159#[derive(Clone)]
160pub struct ExternFunction {
161    pub arguments: Vec<WrapperType>,
162    pub return_type: Option<WrapperType>,
163    pub name: String,
164    pub tokens: TokenStream,
165}