ice_rs/slice/
interface.rs1use crate::slice::function::Function;
2use quote::{__private::TokenStream, format_ident, quote};
3
4
5#[derive(Clone, Debug)]
6pub struct Interface {
7 pub id: TokenStream,
8 pub ice_id: String,
9 pub functions: Vec<Function>
10}
11
12impl Interface {
13 pub fn empty() -> Interface {
14 Interface {
15 id: TokenStream::new(),
16 ice_id: String::from(""),
17 functions: Vec::new()
18 }
19 }
20
21 pub fn add_function(&mut self, function: Function) {
22 self.functions.push(function);
23 }
24
25 pub fn generate(&self, mod_path: &str) -> Result<TokenStream, Box<dyn std::error::Error>> {
26 let mut decl_tokens = TokenStream::new();
27 for function in &self.functions {
28 let token = function.generate_decl()?;
29 decl_tokens = quote! {
30 #decl_tokens
31 #token
32 };
33 }
34 let mut impl_tokens = TokenStream::new();
35 for function in &self.functions {
36 let token = function.generate_impl()?;
37 impl_tokens = quote! {
38 #impl_tokens
39 #token
40 };
41 }
42 let mut server_decl_tokens = TokenStream::new();
43 for function in &self.functions {
44 let token = function.generate_server_decl()?;
45 server_decl_tokens = quote! {
46 #server_decl_tokens
47 #token
48 };
49 }
50 let mut server_handler_tokens = TokenStream::new();
51 for function in &self.functions {
52 let token = function.generate_server_handler()?;
53 server_handler_tokens = quote! {
54 #server_handler_tokens
55 #token
56 };
57 }
58
59 let id_token = &self.id;
60 let id_proxy_token = format_ident!("{}Prx", self.id.to_string());
61 let id_server_trait_token = format_ident!("{}I", self.id.to_string());
62 let id_server_token = format_ident!("{}Server", self.id.to_string());
63 let type_id_token = format!("{}::{}", mod_path, self.ice_id);
64 Ok(quote! {
65 #[async_trait]
66 pub trait #id_token : IceObject {
67 #decl_tokens
68 }
69
70 #[async_trait]
71 pub trait #id_server_trait_token {
72 #server_decl_tokens
73 }
74
75 pub struct #id_server_token {
76 server_impl: Box<dyn #id_server_trait_token + Send + Sync>
77 }
78
79 impl #id_server_token {
80 #[allow(dead_code)]
81 pub fn new(server_impl: Box<dyn #id_server_trait_token + Send + Sync>) -> #id_server_token {
82 #id_server_token {
83 server_impl
84 }
85 }
86
87 async fn ice_is_a(&self, param: &str) -> bool {
88 param == #type_id_token
89 }
90 }
92
93 #[async_trait]
94 impl IceObjectServer for #id_server_token {
95 async fn handle_request(&mut self, request: &RequestData) -> Result<ReplyData, Box<dyn std::error::Error + Sync + Send>> {
96 match request.operation.as_ref() {
97 "ice_isA" => {
98 let mut read = 0;
99 let param = String::from_bytes(&request.params.data, &mut read)?;
100 Ok(ReplyData {
101 request_id: request.request_id,
102 status: 0,
103 body: Encapsulation::from(self.ice_is_a(¶m).await.to_bytes()?)
104 })
105 },
106 #server_handler_tokens
107 _ => Err(Box::new(ProtocolError::new("Operation not found")))
108 }
109 }
110 }
111
112 pub struct #id_proxy_token {
113 pub proxy: Proxy
114 }
115
116 #[async_trait]
117 impl IceObject for #id_proxy_token {
118 async fn ice_ping(&mut self) -> Result<(), Box<dyn std::error::Error + Sync + Send>>
119 {
120 self.proxy.dispatch::<ProtocolError>(&String::from("ice_ping"), 1, &Encapsulation::empty(), None).await?;
121 Ok(())
122 }
123
124 async fn ice_is_a(&mut self) -> Result<bool, Box<dyn std::error::Error + Sync + Send>> {
125 let reply = self.proxy.dispatch::<ProtocolError>(&String::from("ice_isA"), 1, &Encapsulation::from(String::from(#type_id_token).to_bytes()?), None).await?;
126 let mut read_bytes: i32 = 0;
127 bool::from_bytes(&reply.body.data, &mut read_bytes)
128 }
129
130 async fn ice_id(&mut self) -> Result<String, Box<dyn std::error::Error + Sync + Send>>
131 {
132 let reply = self.proxy.dispatch::<ProtocolError>(&String::from("ice_id"), 1, &Encapsulation::empty(), None).await?;
133 let mut read_bytes: i32 = 0;
134 String::from_bytes(&reply.body.data, &mut read_bytes)
135 }
136
137 async fn ice_ids(&mut self) -> Result<Vec<String>, Box<dyn std::error::Error + Sync + Send>>
138 {
139 let reply = self.proxy.dispatch::<ProtocolError>(&String::from("ice_ids"), 1, &Encapsulation::empty(), None).await?;
140 let mut read_bytes: i32 = 0;
141 Vec::from_bytes(&reply.body.data, &mut read_bytes)
142 }
143 }
144
145 #[async_trait]
146 impl #id_token for #id_proxy_token {
147 #impl_tokens
148 }
149
150 impl #id_proxy_token {
151 #[allow(dead_code)]
152 pub async fn unchecked_cast(proxy: Proxy) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
153 Ok(Self {
154 proxy: proxy,
155 })
156 }
157
158 #[allow(dead_code)]
159 pub async fn checked_cast(proxy: Proxy) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
160 let mut my_proxy = Self::unchecked_cast(proxy).await?;
161
162 if !my_proxy.ice_is_a().await? {
163 return Err(Box::new(ProtocolError::new("ice_is_a() failed")));
164 }
165 Ok(my_proxy)
166 }
167 }
168 })
169 }
170}