@@ -74,7 +74,7 @@ impl UPTransportZenoh {
7474 /// or has a non-zero resource ID.
7575 pub fn builder < U : Into < String > > (
7676 local_authority : U ,
77- ) -> Result < UPTransportZenohBuilder , UStatus > {
77+ ) -> Result < UPTransportZenohBuilder < InitialBuilderState > , UStatus > {
7878 let authority_name = local_authority. into ( ) ;
7979 if authority_name. is_empty ( ) || & authority_name == "*" {
8080 return Err ( UStatus :: fail_with_code (
@@ -95,7 +95,7 @@ impl UPTransportZenoh {
9595 local_authority : authority_name,
9696 max_listeners : DEFAULT_MAX_LISTENERS ,
9797 } ) ,
98- extra : BuilderState :: Initial ,
98+ extra : InitialBuilderState ,
9999 } )
100100 }
101101
@@ -140,19 +140,23 @@ struct CommonProperties {
140140 max_listeners : usize ,
141141}
142142
143- pub enum BuilderState {
144- Initial ,
143+ pub struct InitialBuilderState ;
144+ pub enum ConfiguredBuilderState {
145145 Config ( Box < zenoh_config:: Config > ) ,
146146 ConfigPath ( String ) ,
147147 Session ( Session ) ,
148148}
149149
150- pub struct UPTransportZenohBuilder {
150+ pub trait BuilderState { }
151+ impl BuilderState for InitialBuilderState { }
152+ impl BuilderState for ConfiguredBuilderState { }
153+
154+ pub struct UPTransportZenohBuilder < S : BuilderState > {
151155 common : Box < CommonProperties > ,
152- extra : BuilderState ,
156+ extra : S ,
153157}
154158
155- impl UPTransportZenohBuilder {
159+ impl UPTransportZenohBuilder < InitialBuilderState > {
156160 /// Sets the Zenoh configuration to use for the transport.
157161 ///
158162 /// Please refer to the [Zenoh documentation](https://zenoh.io/docs/manual/configuration/) for details.
@@ -174,10 +178,13 @@ impl UPTransportZenohBuilder {
174178 /// # }
175179 /// ```
176180 #[ must_use]
177- pub fn with_config ( self , config : zenoh_config:: Config ) -> UPTransportZenohBuilder {
181+ pub fn with_config (
182+ self ,
183+ config : zenoh_config:: Config ,
184+ ) -> UPTransportZenohBuilder < ConfiguredBuilderState > {
178185 UPTransportZenohBuilder {
179186 common : self . common ,
180- extra : BuilderState :: Config ( Box :: new ( config) ) ,
187+ extra : ConfiguredBuilderState :: Config ( Box :: new ( config) ) ,
181188 }
182189 }
183190
@@ -201,10 +208,13 @@ impl UPTransportZenohBuilder {
201208 /// # }
202209 /// ```
203210 #[ must_use]
204- pub fn with_config_path ( self , config_path : String ) -> UPTransportZenohBuilder {
211+ pub fn with_config_path (
212+ self ,
213+ config_path : String ,
214+ ) -> UPTransportZenohBuilder < ConfiguredBuilderState > {
205215 UPTransportZenohBuilder {
206216 common : self . common ,
207- extra : BuilderState :: ConfigPath ( config_path) ,
217+ extra : ConfiguredBuilderState :: ConfigPath ( config_path) ,
208218 }
209219 }
210220
@@ -229,14 +239,17 @@ impl UPTransportZenohBuilder {
229239 /// # }
230240 /// ```
231241 #[ must_use]
232- pub fn with_session ( self , zenoh_session : Session ) -> UPTransportZenohBuilder {
242+ pub fn with_session (
243+ self ,
244+ zenoh_session : Session ,
245+ ) -> UPTransportZenohBuilder < ConfiguredBuilderState > {
233246 UPTransportZenohBuilder {
234247 common : self . common ,
235- extra : BuilderState :: Session ( zenoh_session) ,
248+ extra : ConfiguredBuilderState :: Session ( zenoh_session) ,
236249 }
237250 }
238251
239- /// Creates the Zenoh transport.
252+ /// Creates the Zenoh transport with the default configuration .
240253 ///
241254 /// # Returns
242255 ///
@@ -261,25 +274,38 @@ impl UPTransportZenohBuilder {
261274 /// .is_ok());
262275 /// # }
263276 /// ```
277+ pub async fn build ( self ) -> Result < UPTransportZenoh , UStatus > {
278+ UPTransportZenoh :: init_with_config (
279+ zenoh_config:: Config :: default ( ) ,
280+ self . common . local_authority ,
281+ self . common . max_listeners ,
282+ )
283+ . await
284+ }
285+ }
286+
287+ impl UPTransportZenohBuilder < ConfiguredBuilderState > {
288+ /// Creates the Zenoh transport with the provided configuration properties, configuration file or Zenoh Session.
289+ ///
290+ /// # Returns
291+ ///
292+ /// The newly created transport instance. Note that the builder consumes itself.
293+ ///
294+ /// # Errors
295+ ///
296+ /// Returns an error if the transport cannot be created.
297+ ///
264298 pub async fn build ( self ) -> Result < UPTransportZenoh , UStatus > {
265299 match self . extra {
266- BuilderState :: Initial => {
267- UPTransportZenoh :: init_with_config (
268- zenoh_config:: Config :: default ( ) ,
269- self . common . local_authority ,
270- self . common . max_listeners ,
271- )
272- . await
273- }
274- BuilderState :: Config ( config) => {
300+ ConfiguredBuilderState :: Config ( config) => {
275301 UPTransportZenoh :: init_with_config (
276302 * config,
277303 self . common . local_authority ,
278304 self . common . max_listeners ,
279305 )
280306 . await
281307 }
282- BuilderState :: ConfigPath ( config_path) => {
308+ ConfiguredBuilderState :: ConfigPath ( config_path) => {
283309 let config = zenoh_config:: Config :: from_file ( config_path) . map_err ( |e| {
284310 error ! ( "Failed to load Zenoh config from file: {e}" ) ;
285311 UStatus :: fail_with_code ( UCode :: INVALID_ARGUMENT , e. to_string ( ) )
@@ -291,14 +317,18 @@ impl UPTransportZenohBuilder {
291317 )
292318 . await
293319 }
294- BuilderState :: Session ( zenoh_session) => Ok ( UPTransportZenoh :: init_with_session (
295- zenoh_session,
296- self . common . local_authority ,
297- self . common . max_listeners ,
298- ) ) ,
320+ ConfiguredBuilderState :: Session ( zenoh_session) => {
321+ Ok ( UPTransportZenoh :: init_with_session (
322+ zenoh_session,
323+ self . common . local_authority ,
324+ self . common . max_listeners ,
325+ ) )
326+ }
299327 }
300328 }
329+ }
301330
331+ impl < S : BuilderState > UPTransportZenohBuilder < S > {
302332 /// Sets the maximum number of listeners that can be registered with this transport.
303333 /// If not set explicitly, the default value is 100.
304334 #[ must_use]
0 commit comments