|
1 | 1 | use super::misc::{marshal, unmarshal}; |
2 | 2 | use bytes::BytesMut; |
3 | 3 | use rsocket_rust::error::RSocketError; |
4 | | -use rsocket_rust::extension::{ |
5 | | - CompositeMetadata, MimeType, RoutingMetadata, MIME_APPLICATION_JSON, |
6 | | - MIME_MESSAGE_X_RSOCKET_ROUTING_V0, |
7 | | -}; |
| 4 | +use rsocket_rust::extension::{CompositeMetadata, MimeType, RoutingMetadata}; |
8 | 5 | use rsocket_rust::prelude::*; |
9 | 6 | use rsocket_rust::utils::Writeable; |
10 | 7 | use serde::{Deserialize, Serialize}; |
11 | 8 | use std::collections::LinkedList; |
12 | 9 | use std::error::Error; |
| 10 | +use std::sync::Arc; |
13 | 11 |
|
14 | | -pub struct RequesterBuilder { |
15 | | - data_mime_type: MimeType, |
16 | | - route: Option<String>, |
17 | | - data: Option<Vec<u8>>, |
18 | | -} |
19 | | - |
20 | | -impl Default for RequesterBuilder { |
21 | | - fn default() -> Self { |
22 | | - Self { |
23 | | - data_mime_type: MIME_APPLICATION_JSON, |
24 | | - route: None, |
25 | | - data: None, |
26 | | - } |
27 | | - } |
28 | | -} |
29 | | - |
30 | | -impl RequesterBuilder { |
31 | | - pub fn data_mime_type<I>(mut self, mime_type: I) -> Self |
32 | | - where |
33 | | - I: Into<MimeType>, |
34 | | - { |
35 | | - self.data_mime_type = mime_type.into(); |
36 | | - self |
37 | | - } |
38 | | - |
39 | | - pub fn setup_route<I>(mut self, route: I) -> Self |
40 | | - where |
41 | | - I: Into<String>, |
42 | | - { |
43 | | - self.route = Some(route.into()); |
44 | | - self |
45 | | - } |
46 | | - |
47 | | - pub fn setup_data<D>(mut self, data: D) -> Self |
48 | | - where |
49 | | - D: Into<Vec<u8>>, |
50 | | - { |
51 | | - self.data = Some(data.into()); |
52 | | - self |
53 | | - } |
54 | | - |
55 | | - pub fn build<S>(self) -> Requester<S> |
56 | | - where |
57 | | - S: RSocket + Clone, |
58 | | - { |
59 | | - todo!("build requester") |
60 | | - } |
| 12 | +pub struct Requester { |
| 13 | + rsocket: Arc<Box<dyn RSocket>>, |
61 | 14 | } |
62 | 15 |
|
63 | | -pub struct Requester<S> |
64 | | -where |
65 | | - S: RSocket + Clone, |
66 | | -{ |
67 | | - rsocket: S, |
68 | | -} |
69 | | - |
70 | | -pub struct RequestSpec<S> |
71 | | -where |
72 | | - S: RSocket + Clone, |
73 | | -{ |
| 16 | +pub struct RequestSpec { |
74 | 17 | data: Option<Vec<u8>>, |
75 | | - rsocket: S, |
| 18 | + rsocket: Arc<Box<dyn RSocket>>, |
76 | 19 | data_mime_type: MimeType, |
77 | 20 | metadatas: LinkedList<(MimeType, Vec<u8>)>, |
78 | 21 | } |
79 | 22 |
|
80 | | -impl<S> From<S> for Requester<S> |
81 | | -where |
82 | | - S: RSocket + Clone, |
83 | | -{ |
84 | | - fn from(rsocket: S) -> Requester<S> { |
85 | | - Requester { rsocket } |
| 23 | +impl From<Box<dyn RSocket>> for Requester { |
| 24 | + fn from(rsocket: Box<dyn RSocket>) -> Requester { |
| 25 | + Requester { |
| 26 | + rsocket: Arc::new(rsocket), |
| 27 | + } |
86 | 28 | } |
87 | 29 | } |
88 | 30 |
|
89 | | -impl<C> Requester<C> |
90 | | -where |
91 | | - C: RSocket + Clone, |
92 | | -{ |
93 | | - pub fn route(&self, route: &str) -> RequestSpec<C> { |
| 31 | +impl Requester { |
| 32 | + pub fn route(&self, route: &str) -> RequestSpec { |
94 | 33 | let routing = RoutingMetadata::builder().push_str(route).build(); |
95 | 34 | let mut buf = BytesMut::new(); |
96 | 35 | routing.write_to(&mut buf); |
97 | 36 |
|
98 | 37 | let mut metadatas: LinkedList<(MimeType, Vec<u8>)> = Default::default(); |
99 | | - metadatas.push_back((MIME_MESSAGE_X_RSOCKET_ROUTING_V0, buf.to_vec())); |
| 38 | + metadatas.push_back((MimeType::MESSAGE_X_RSOCKET_ROUTING_V0, buf.to_vec())); |
100 | 39 | RequestSpec { |
101 | 40 | data: None, |
102 | 41 | rsocket: self.rsocket.clone(), |
103 | | - data_mime_type: MIME_APPLICATION_JSON, |
| 42 | + data_mime_type: MimeType::APPLICATION_JSON, |
104 | 43 | metadatas, |
105 | 44 | } |
106 | 45 | } |
107 | 46 | } |
108 | 47 |
|
109 | | -impl<C> RequestSpec<C> |
110 | | -where |
111 | | - C: RSocket + Clone, |
112 | | -{ |
| 48 | +impl RequestSpec { |
113 | 49 | pub fn metadata<T, M>(&mut self, metadata: &T, mime_type: M) -> Result<(), Box<dyn Error>> |
114 | 50 | where |
115 | 51 | T: Sized + Serialize, |
|
160 | 96 | } |
161 | 97 |
|
162 | 98 | #[inline] |
163 | | - fn preflight(self) -> (Payload, MimeType, C) { |
| 99 | + fn preflight(self) -> (Payload, MimeType, Arc<Box<dyn RSocket>>) { |
164 | 100 | let mut b = BytesMut::new(); |
165 | 101 | let mut c = CompositeMetadata::builder(); |
166 | 102 | for (mime_type, raw) in self.metadatas.into_iter() { |
|
0 commit comments