1
- use std:: str:: FromStr as _ ;
1
+ use std:: str:: FromStr ;
2
2
3
3
use re_log_types:: { TimeCell , TimeInt } ;
4
4
@@ -27,10 +27,10 @@ impl std::fmt::Display for TimeRange {
27
27
}
28
28
}
29
29
30
- impl TryFrom < & str > for TimeRange {
31
- type Error = Error ;
30
+ impl FromStr for TimeRange {
31
+ type Err = Error ;
32
32
33
- fn try_from ( value : & str ) -> Result < Self , crate :: Error > {
33
+ fn from_str ( value : & str ) -> Result < Self , Self :: Err > {
34
34
let ( timeline, range) = value
35
35
. split_once ( '@' )
36
36
. ok_or_else ( || Error :: InvalidTimeRange ( "Missing @" . to_owned ( ) ) ) ?;
@@ -111,10 +111,10 @@ impl Scheme {
111
111
}
112
112
}
113
113
114
- impl TryFrom < & str > for Scheme {
115
- type Error = Error ;
114
+ impl FromStr for Scheme {
115
+ type Err = Error ;
116
116
117
- fn try_from ( url : & str ) -> Result < Self , crate :: Error > {
117
+ fn from_str ( url : & str ) -> Result < Self , Self :: Err > {
118
118
if url. starts_with ( "rerun://" ) {
119
119
Ok ( Self :: Rerun )
120
120
} else if url. starts_with ( "rerun+http://" ) {
@@ -158,13 +158,18 @@ impl crate::Origin {
158
158
/// Parses a URL and returns the [`crate::Origin`] and the canonical URL (i.e. one that
159
159
/// starts with `http://` or `https://`).
160
160
fn replace_and_parse ( value : & str ) -> Result < ( crate :: Origin , url:: Url ) , Error > {
161
- let scheme = Scheme :: try_from ( value) ?;
161
+ let scheme = Scheme :: from_str ( value) ?;
162
162
let rewritten = scheme. canonical_url ( value) ;
163
163
164
164
// We have to first rewrite the endpoint, because `Url` does not allow
165
165
// `.set_scheme()` for non-opaque origins, nor does it return a proper
166
166
// `Origin` in that case.
167
- let http_url = url:: Url :: parse ( & rewritten) ?;
167
+ let mut http_url = url:: Url :: parse ( & rewritten) ?;
168
+
169
+ if http_url. port ( ) . is_none ( ) {
170
+ // If no port is specified, we assume the default redap port:
171
+ http_url. set_port ( Some ( 51234 ) ) . ok ( ) ;
172
+ }
168
173
169
174
let url:: Origin :: Tuple ( _, host, port) = http_url. origin ( ) else {
170
175
return Err ( Error :: UnexpectedOpaqueOrigin ( value. to_owned ( ) ) ) ;
@@ -175,10 +180,10 @@ fn replace_and_parse(value: &str) -> Result<(crate::Origin, url::Url), Error> {
175
180
Ok ( ( origin, http_url) )
176
181
}
177
182
178
- impl TryFrom < & str > for crate :: Origin {
179
- type Error = Error ;
183
+ impl FromStr for crate :: Origin {
184
+ type Err = Error ;
180
185
181
- fn try_from ( value : & str ) -> Result < Self , crate :: Error > {
186
+ fn from_str ( value : & str ) -> Result < Self , Self :: Err > {
182
187
replace_and_parse ( value) . map ( |( origin, _) | origin)
183
188
}
184
189
}
@@ -213,15 +218,7 @@ impl std::fmt::Display for RedapUri {
213
218
impl std:: str:: FromStr for RedapUri {
214
219
type Err = Error ;
215
220
216
- fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
217
- Self :: try_from ( s)
218
- }
219
- }
220
-
221
- impl TryFrom < & str > for RedapUri {
222
- type Error = Error ;
223
-
224
- fn try_from ( value : & str ) -> Result < Self , crate :: Error > {
221
+ fn from_str ( value : & str ) -> Result < Self , Self :: Err > {
225
222
let ( origin, http_url) = replace_and_parse ( value) ?;
226
223
227
224
// :warning: We limit the amount of segments, which might need to be
@@ -236,7 +233,7 @@ impl TryFrom<&str> for RedapUri {
236
233
let time_range = http_url
237
234
. query_pairs ( )
238
235
. find ( |( key, _) | key == TimeRange :: QUERY_KEY )
239
- . map ( |( _, value) | TimeRange :: try_from ( value. as_ref ( ) ) ) ;
236
+ . map ( |( _, value) | TimeRange :: from_str ( value. as_ref ( ) ) ) ;
240
237
241
238
match segments. as_slice ( ) {
242
239
[ "proxy" ] => Ok ( Self :: Proxy ( ProxyEndpoint :: new ( origin) ) ) ,
@@ -306,7 +303,7 @@ mod tests {
306
303
fn test_dataset_data_url_to_address ( ) {
307
304
let url =
308
305
"rerun://127.0.0.1:1234/dataset/1830B33B45B963E7774455beb91701ae/data?partition_id=pid" ;
309
- let address: RedapUri = url. try_into ( ) . unwrap ( ) ;
306
+ let address: RedapUri = url. parse ( ) . unwrap ( ) ;
310
307
311
308
let RedapUri :: DatasetData ( DatasetDataEndpoint {
312
309
origin,
@@ -332,7 +329,7 @@ mod tests {
332
329
#[ test]
333
330
fn test_dataset_data_url_time_range_sequence_to_address ( ) {
334
331
let url =
"rerun://127.0.0.1:1234/dataset/1830B33B45B963E7774455beb91701ae/data?partition_id=pid&[email protected] " ;
335
- let address: RedapUri = url. try_into ( ) . unwrap ( ) ;
332
+ let address: RedapUri = url. parse ( ) . unwrap ( ) ;
336
333
337
334
let RedapUri :: DatasetData ( DatasetDataEndpoint {
338
335
origin,
@@ -367,7 +364,7 @@ mod tests {
367
364
#[ test]
368
365
fn test_dataset_data_url_time_range_timepoint_to_address ( ) {
369
366
let url = "rerun://127.0.0.1:1234/dataset/1830B33B45B963E7774455beb91701ae/data?partition_id=pid&time_range=log_time@2022-01-01T00:00:03.123456789Z..2022-01-01T00:00:13.123456789Z" ;
370
- let address: RedapUri = url. try_into ( ) . unwrap ( ) ;
367
+ let address: RedapUri = url. parse ( ) . unwrap ( ) ;
371
368
372
369
let RedapUri :: DatasetData ( DatasetDataEndpoint {
373
370
origin,
@@ -409,7 +406,7 @@ mod tests {
409
406
"rerun://127.0.0.1:1234/dataset/1830B33B45B963E7774455beb91701ae/data?partition_id=pid&[email protected] " ,
410
407
"rerun://127.0.0.1:1234/dataset/1830B33B45B963E7774455beb91701ae/data?partition_id=pid&[email protected] " ,
411
408
] {
412
- let address: RedapUri = url. try_into ( ) . unwrap ( ) ;
409
+ let address: RedapUri = url. parse ( ) . unwrap ( ) ;
413
410
414
411
let RedapUri :: DatasetData ( DatasetDataEndpoint {
415
412
origin,
@@ -446,13 +443,13 @@ mod tests {
446
443
fn test_dataset_data_url_missing_partition_id ( ) {
447
444
let url = "rerun://127.0.0.1:1234/dataset/1830B33B45B963E7774455beb91701ae/data" ;
448
445
449
- assert ! ( RedapUri :: try_from ( url) . is_err( ) ) ;
446
+ assert ! ( RedapUri :: from_str ( url) . is_err( ) ) ;
450
447
}
451
448
452
449
#[ test]
453
450
fn test_http_catalog_url_to_address ( ) {
454
451
let url = "rerun+http://127.0.0.1:50051/catalog" ;
455
- let address: RedapUri = url. try_into ( ) . unwrap ( ) ;
452
+ let address: RedapUri = url. parse ( ) . unwrap ( ) ;
456
453
assert ! ( matches!(
457
454
address,
458
455
RedapUri :: Catalog ( CatalogEndpoint {
@@ -468,7 +465,7 @@ mod tests {
468
465
#[ test]
469
466
fn test_https_catalog_url_to_address ( ) {
470
467
let url = "rerun+https://127.0.0.1:50051/catalog" ;
471
- let address: RedapUri = url. try_into ( ) . unwrap ( ) ;
468
+ let address: RedapUri = url. parse ( ) . unwrap ( ) ;
472
469
473
470
assert ! ( matches!(
474
471
address,
@@ -485,7 +482,7 @@ mod tests {
485
482
#[ test]
486
483
fn test_localhost_url ( ) {
487
484
let url = "rerun+http://localhost:51234/catalog" ;
488
- let address = RedapUri :: try_from ( url) . unwrap ( ) ;
485
+ let address = RedapUri :: from_str ( url) . unwrap ( ) ;
489
486
490
487
assert_eq ! (
491
488
address,
@@ -502,7 +499,7 @@ mod tests {
502
499
#[ test]
503
500
fn test_invalid_url ( ) {
504
501
let url = "http://wrong-scheme:1234/recording/12345" ;
505
- let address: Result < RedapUri , _ > = url. try_into ( ) ;
502
+ let address: Result < RedapUri , _ > = url. parse ( ) ;
506
503
507
504
assert ! ( matches!(
508
505
address. unwrap_err( ) ,
@@ -513,7 +510,7 @@ mod tests {
513
510
#[ test]
514
511
fn test_invalid_path ( ) {
515
512
let url = "rerun://0.0.0.0:51234/redap/recordings/12345" ;
516
- let address: Result < RedapUri , _ > = url. try_into ( ) ;
513
+ let address: Result < RedapUri , _ > = url. parse ( ) ;
517
514
518
515
assert ! ( matches!(
519
516
address. unwrap_err( ) ,
@@ -524,7 +521,7 @@ mod tests {
524
521
#[ test]
525
522
fn test_proxy_endpoint ( ) {
526
523
let url = "rerun://localhost:51234/proxy" ;
527
- let address: Result < RedapUri , _ > = url. try_into ( ) ;
524
+ let address: Result < RedapUri , _ > = url. parse ( ) ;
528
525
529
526
let expected = RedapUri :: Proxy ( ProxyEndpoint {
530
527
origin : Origin {
@@ -537,15 +534,15 @@ mod tests {
537
534
assert_eq ! ( address. unwrap( ) , expected) ;
538
535
539
536
let url = "rerun://localhost:51234/proxy/" ;
540
- let address: Result < RedapUri , _ > = url. try_into ( ) ;
537
+ let address: Result < RedapUri , _ > = url. parse ( ) ;
541
538
542
539
assert_eq ! ( address. unwrap( ) , expected) ;
543
540
}
544
541
545
542
#[ test]
546
543
fn test_catalog_default ( ) {
547
544
let url = "rerun://localhost:51234" ;
548
- let address: Result < RedapUri , _ > = url. try_into ( ) ;
545
+ let address: Result < RedapUri , _ > = url. parse ( ) ;
549
546
550
547
let expected = RedapUri :: Catalog ( CatalogEndpoint {
551
548
origin : Origin {
@@ -558,8 +555,38 @@ mod tests {
558
555
assert_eq ! ( address. unwrap( ) , expected) ;
559
556
560
557
let url = "rerun://localhost:51234/" ;
561
- let address: Result < RedapUri , _ > = url. try_into ( ) ;
558
+ let address: Result < RedapUri , _ > = url. parse ( ) ;
562
559
563
560
assert_eq ! ( address. unwrap( ) , expected) ;
564
561
}
562
+
563
+ #[ test]
564
+ fn test_default_port ( ) {
565
+ let url = "rerun://localhost" ;
566
+
567
+ let expected = RedapUri :: Catalog ( CatalogEndpoint {
568
+ origin : Origin {
569
+ scheme : Scheme :: Rerun ,
570
+ host : url:: Host :: Domain ( "localhost" . to_owned ( ) ) ,
571
+ port : 51234 ,
572
+ } ,
573
+ } ) ;
574
+
575
+ assert_eq ! ( url. parse:: <RedapUri >( ) . unwrap( ) , expected) ;
576
+ }
577
+
578
+ #[ test]
579
+ fn test_custom_port ( ) {
580
+ let url = "rerun://localhost:123" ;
581
+
582
+ let expected = RedapUri :: Catalog ( CatalogEndpoint {
583
+ origin : Origin {
584
+ scheme : Scheme :: Rerun ,
585
+ host : url:: Host :: Domain ( "localhost" . to_owned ( ) ) ,
586
+ port : 123 ,
587
+ } ,
588
+ } ) ;
589
+
590
+ assert_eq ! ( url. parse:: <RedapUri >( ) . unwrap( ) , expected) ;
591
+ }
565
592
}
0 commit comments