1use crate::types::RosService;
7
8pub struct ServiceServer<S: RosService> {
12 pub name: &'static str,
14 _marker: core::marker::PhantomData<S>,
16}
17
18impl<S: RosService> ServiceServer<S> {
19 pub fn new(name: &'static str) -> Self {
21 Self {
22 name,
23 _marker: core::marker::PhantomData,
24 }
25 }
26
27 pub fn name(&self) -> &str {
29 self.name
30 }
31
32 pub fn service_type(&self) -> &'static str {
34 S::SERVICE_NAME
35 }
36
37 pub fn service_hash(&self) -> &'static str {
39 S::SERVICE_HASH
40 }
41}
42
43pub struct ServiceClient<S: RosService> {
47 pub name: &'static str,
49 _marker: core::marker::PhantomData<S>,
51}
52
53impl<S: RosService> ServiceClient<S> {
54 pub fn new(name: &'static str) -> Self {
56 Self {
57 name,
58 _marker: core::marker::PhantomData,
59 }
60 }
61
62 pub fn name(&self) -> &str {
64 self.name
65 }
66
67 pub fn service_type(&self) -> &'static str {
69 S::SERVICE_NAME
70 }
71
72 pub fn service_hash(&self) -> &'static str {
74 S::SERVICE_HASH
75 }
76}
77
78pub struct ServiceRequest<'a, S: RosService> {
82 pub request: S::Request,
84 pub raw_data: &'a [u8],
86}
87
88pub type ServiceCallback<S> = fn(&<S as RosService>::Request) -> <S as RosService>::Reply;
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 struct MockService;
106
107 #[derive(Debug, Clone)]
110 struct MockRequest {
111 #[allow(dead_code)]
112 pub a: i32,
113 #[allow(dead_code)]
114 pub b: i32,
115 }
116
117 #[derive(Debug, Clone)]
118 struct MockReply {
119 #[allow(dead_code)]
120 pub sum: i32,
121 }
122
123 impl nros_serdes::Serialize for MockRequest {
125 fn serialize(
126 &self,
127 _writer: &mut nros_serdes::CdrWriter,
128 ) -> Result<(), nros_serdes::SerError> {
129 Ok(())
130 }
131 }
132
133 impl nros_serdes::Deserialize for MockRequest {
134 fn deserialize(
135 _reader: &mut nros_serdes::CdrReader,
136 ) -> Result<Self, nros_serdes::DeserError> {
137 Ok(MockRequest { a: 0, b: 0 })
138 }
139 }
140
141 impl crate::RosMessage for MockRequest {
142 const TYPE_NAME: &'static str = "test_msgs::srv::dds_::AddTwoInts_Request_";
143 const TYPE_HASH: &'static str =
144 "0000000000000000000000000000000000000000000000000000000000000000";
145 }
146
147 impl nros_serdes::Serialize for MockReply {
148 fn serialize(
149 &self,
150 _writer: &mut nros_serdes::CdrWriter,
151 ) -> Result<(), nros_serdes::SerError> {
152 Ok(())
153 }
154 }
155
156 impl nros_serdes::Deserialize for MockReply {
157 fn deserialize(
158 _reader: &mut nros_serdes::CdrReader,
159 ) -> Result<Self, nros_serdes::DeserError> {
160 Ok(MockReply { sum: 0 })
161 }
162 }
163
164 impl crate::RosMessage for MockReply {
165 const TYPE_NAME: &'static str = "test_msgs::srv::dds_::AddTwoInts_Reply_";
166 const TYPE_HASH: &'static str =
167 "0000000000000000000000000000000000000000000000000000000000000000";
168 }
169
170 impl RosService for MockService {
171 type Request = MockRequest;
172 type Reply = MockReply;
173 const SERVICE_NAME: &'static str = "test_msgs::srv::dds_::AddTwoInts_";
174 const SERVICE_HASH: &'static str =
175 "0000000000000000000000000000000000000000000000000000000000000000";
176 }
177
178 #[test]
179 fn test_service_server_creation() {
180 let server = ServiceServer::<MockService>::new("/add_two_ints");
181 assert_eq!(server.name(), "/add_two_ints");
182 assert_eq!(server.service_type(), "test_msgs::srv::dds_::AddTwoInts_");
183 }
184
185 #[test]
186 fn test_service_client_creation() {
187 let client = ServiceClient::<MockService>::new("/add_two_ints");
188 assert_eq!(client.name(), "/add_two_ints");
189 assert_eq!(client.service_type(), "test_msgs::srv::dds_::AddTwoInts_");
190 }
191}