phoxal 0.6.0

Phoxal — production-oriented autonomous robot framework (engine, model, typed bus, contracts).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#![allow(clippy::module_name_repetitions)]

use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;

/// Profile identifier, e.g. "r640x480_h15_rgb8" or the reserved native id.
/// Validated syntax (see ProfileId::new / TryFrom<String>).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct ProfileId(String);

impl ProfileId {
    pub const DEFAULT: &str = "default";

    pub fn new(value: impl Into<String>) -> Result<Self, InvalidProfileIdentifier> {
        let value = value.into();
        validate_profile_identifier(&value)?;
        Ok(Self(value))
    }

    /// The reserved implicit "default" profile id (the capability's native stream).
    pub fn default_profile() -> Self {
        Self(Self::DEFAULT.to_string())
    }
}

impl AsRef<str> for ProfileId {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for ProfileId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl FromStr for ProfileId {
    type Err = InvalidProfileIdentifier;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        Self::new(value)
    }
}

impl TryFrom<String> for ProfileId {
    type Error = InvalidProfileIdentifier;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

impl From<ProfileId> for String {
    fn from(value: ProfileId) -> Self {
        value.0
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct InvalidProfileIdentifier {
    value: String,
}

impl fmt::Display for InvalidProfileIdentifier {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "invalid profile identifier '{}', expected a non-empty non-reserved lowercase ASCII profile id using letters, digits, '.', '_' or '-' with no leading, trailing, or repeated '.'",
            self.value
        )
    }
}

impl std::error::Error for InvalidProfileIdentifier {}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImageCrop {
    pub origin_x_px: u32,
    pub origin_y_px: u32,
    pub width_px: u32,
    pub height_px: u32,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AngularRoi {
    pub start_rad: f32,
    pub end_rad: f32,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CameraProfileEncoding {
    L8,
    Rgb8,
    Rgba8,
    Jpeg,
    Png,
}

impl CameraProfileEncoding {
    #[must_use]
    pub const fn profile_token(&self) -> &'static str {
        match self {
            Self::L8 => "l8",
            Self::Rgb8 => "rgb8",
            Self::Rgba8 => "rgba8",
            Self::Jpeg => "jpeg",
            Self::Png => "png",
        }
    }

    fn from_profile_token(value: &str) -> Option<Self> {
        match value {
            "l8" => Some(Self::L8),
            "rgb8" => Some(Self::Rgb8),
            "rgba8" => Some(Self::Rgba8),
            "jpeg" => Some(Self::Jpeg),
            "png" => Some(Self::Png),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DepthProfileEncoding {
    U16Millimeters,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CameraProfileSpec {
    pub width_px: u32,
    pub height_px: u32,
    pub publish_rate_hz: f64,
    pub encoding: CameraProfileEncoding,
}

impl CameraProfileSpec {
    pub fn to_profile_id(&self) -> Result<ProfileId, ProfileSpecError> {
        validate_spec_dimensions(self.width_px, self.height_px)?;
        let rate_hz = integer_rate_hz(self.publish_rate_hz)?;
        ProfileId::new(format!(
            "r{}x{}_h{}_{}",
            self.width_px,
            self.height_px,
            rate_hz,
            self.encoding.profile_token()
        ))
        .map_err(ProfileSpecError::InvalidIdentifier)
    }

    pub fn from_profile_id(
        profile_id: &ProfileId,
    ) -> Result<ParsedCameraProfileSpec, ProfileSpecError> {
        let value = profile_id.as_ref();
        if value == ProfileId::DEFAULT {
            return Ok(ParsedCameraProfileSpec::Native);
        }

        let (width_px, height_px, publish_rate_hz, encoding_token) =
            parse_profile_spec_parts(value)?;
        let Some(encoding) = CameraProfileEncoding::from_profile_token(encoding_token) else {
            return Err(ProfileSpecError::InvalidFormat {
                value: value.to_string(),
            });
        };

        Ok(ParsedCameraProfileSpec::Spec(Self {
            width_px,
            height_px,
            publish_rate_hz,
            encoding,
        }))
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ParsedCameraProfileSpec {
    Native,
    Spec(CameraProfileSpec),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DepthProfileSpec {
    pub width_px: u32,
    pub height_px: u32,
    pub publish_rate_hz: f64,
}

impl DepthProfileSpec {
    pub fn to_profile_id(&self) -> Result<ProfileId, ProfileSpecError> {
        validate_spec_dimensions(self.width_px, self.height_px)?;
        let rate_hz = integer_rate_hz(self.publish_rate_hz)?;
        ProfileId::new(format!(
            "r{}x{}_h{}",
            self.width_px, self.height_px, rate_hz
        ))
        .map_err(ProfileSpecError::InvalidIdentifier)
    }

    pub fn from_profile_id(
        profile_id: &ProfileId,
    ) -> Result<ParsedDepthProfileSpec, ProfileSpecError> {
        let value = profile_id.as_ref();
        if value == ProfileId::DEFAULT {
            return Ok(ParsedDepthProfileSpec::Native);
        }

        let (width_px, height_px, publish_rate_hz, encoding_token) =
            parse_profile_spec_parts(value)?;
        if !encoding_token.is_empty() {
            return Err(ProfileSpecError::InvalidFormat {
                value: value.to_string(),
            });
        }

        Ok(ParsedDepthProfileSpec::Spec(Self {
            width_px,
            height_px,
            publish_rate_hz,
        }))
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ParsedDepthProfileSpec {
    Native,
    Spec(DepthProfileSpec),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RateProfileSpec {
    pub publish_rate_hz: f64,
}

impl RateProfileSpec {
    pub fn to_profile_id(&self) -> Result<ProfileId, ProfileSpecError> {
        let rate_hz = integer_rate_hz(self.publish_rate_hz)?;
        ProfileId::new(format!("h{rate_hz}")).map_err(ProfileSpecError::InvalidIdentifier)
    }

    pub fn from_profile_id(
        profile_id: &ProfileId,
    ) -> Result<ParsedRateProfileSpec, ProfileSpecError> {
        let value = profile_id.as_ref();
        if value == ProfileId::DEFAULT {
            return Ok(ParsedRateProfileSpec::Native);
        }

        Ok(ParsedRateProfileSpec::Spec(Self {
            publish_rate_hz: parse_rate_profile_spec(value)?,
        }))
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ParsedRateProfileSpec {
    Native,
    Spec(RateProfileSpec),
}

#[derive(Debug, Clone, PartialEq)]
pub enum ProfileSpecError {
    InvalidIdentifier(InvalidProfileIdentifier),
    InvalidFormat { value: String },
    InvalidDimensions { width_px: u32, height_px: u32 },
    InvalidPublishRate { publish_rate_hz: f64 },
}

impl fmt::Display for ProfileSpecError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidIdentifier(error) => error.fmt(f),
            Self::InvalidFormat { value } => write!(
                f,
                "invalid profile spec '{}', expected camera form r{{w}}x{{h}}_h{{rate}}_{{enc}}, depth form r{{w}}x{{h}}_h{{rate}}, or rate form h{{rate}}",
                value
            ),
            Self::InvalidDimensions {
                width_px,
                height_px,
            } => write!(
                f,
                "invalid profile spec dimensions {}x{}, width and height must be > 0",
                width_px, height_px
            ),
            Self::InvalidPublishRate { publish_rate_hz } => write!(
                f,
                "invalid profile spec publish_rate_hz {}, expected a positive integer Hz value",
                publish_rate_hz
            ),
        }
    }
}

impl std::error::Error for ProfileSpecError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::InvalidIdentifier(error) => Some(error),
            Self::InvalidFormat { .. }
            | Self::InvalidDimensions { .. }
            | Self::InvalidPublishRate { .. } => None,
        }
    }
}

fn validate_profile_identifier(value: &str) -> Result<(), InvalidProfileIdentifier> {
    let valid = !value.is_empty()
        && value != "default"
        && !value.starts_with('.')
        && !value.ends_with('.')
        && !value.contains("..")
        && value.chars().all(|character| {
            character.is_ascii_lowercase()
                || character.is_ascii_digit()
                || character == '.'
                || character == '_'
                || character == '-'
        });

    if valid {
        Ok(())
    } else {
        Err(InvalidProfileIdentifier {
            value: value.to_string(),
        })
    }
}

fn validate_spec_dimensions(width_px: u32, height_px: u32) -> Result<(), ProfileSpecError> {
    if width_px == 0 || height_px == 0 {
        Err(ProfileSpecError::InvalidDimensions {
            width_px,
            height_px,
        })
    } else {
        Ok(())
    }
}

fn integer_rate_hz(publish_rate_hz: f64) -> Result<u64, ProfileSpecError> {
    if !publish_rate_hz.is_finite() || publish_rate_hz <= 0.0 || publish_rate_hz.fract() != 0.0 {
        return Err(ProfileSpecError::InvalidPublishRate { publish_rate_hz });
    }
    Ok(publish_rate_hz as u64)
}

fn parse_profile_spec_parts(value: &str) -> Result<(u32, u32, f64, &str), ProfileSpecError> {
    let Some(after_resolution_marker) = value.strip_prefix('r') else {
        return Err(ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        });
    };
    let Some((resolution, after_rate_marker)) = after_resolution_marker.split_once("_h") else {
        return Err(ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        });
    };
    let Some((width, height)) = resolution.split_once('x') else {
        return Err(ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        });
    };
    let (rate, encoding_token) = after_rate_marker
        .split_once('_')
        .unwrap_or((after_rate_marker, ""));
    if width.is_empty() || height.is_empty() || rate.is_empty() {
        return Err(ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        });
    }

    let width_px = width
        .parse::<u32>()
        .map_err(|_| ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        })?;
    let height_px = height
        .parse::<u32>()
        .map_err(|_| ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        })?;
    let rate_hz = rate
        .parse::<u64>()
        .map_err(|_| ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        })?;
    validate_spec_dimensions(width_px, height_px)?;
    if rate_hz == 0 {
        return Err(ProfileSpecError::InvalidPublishRate {
            publish_rate_hz: 0.0,
        });
    }

    Ok((width_px, height_px, rate_hz as f64, encoding_token))
}

fn parse_rate_profile_spec(value: &str) -> Result<f64, ProfileSpecError> {
    let Some(rate) = value.strip_prefix('h') else {
        return Err(ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        });
    };
    if rate.is_empty() {
        return Err(ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        });
    }

    let rate_hz = rate
        .parse::<u64>()
        .map_err(|_| ProfileSpecError::InvalidFormat {
            value: value.to_string(),
        })?;
    if rate_hz == 0 {
        return Err(ProfileSpecError::InvalidPublishRate {
            publish_rate_hz: 0.0,
        });
    }

    Ok(rate_hz as f64)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn profile_path_uses_source_and_profile_without_kind_or_data_suffix() {
        let profile_id = ProfileId::new("localize.rgbd_color").unwrap();

        assert_eq!(
            super::super::profile_path("front_camera", "rgb", &profile_id),
            "component/front_camera/rgb/profile/localize.rgbd_color"
        );
    }

    #[test]
    fn default_profile_uses_reserved_implicit_profile_path() {
        assert_eq!(
            super::super::profile_path("c", "cap", &ProfileId::default_profile()),
            "component/c/cap/profile/default"
        );
        assert!(ProfileId::new("default").is_err());
    }

    #[test]
    fn profile_id_accepts_topic_segment_shape() {
        for value in [
            "localize.rgbd_color",
            "operator.preview",
            "native",
            "rerun.debug_preview",
        ] {
            assert_eq!(ProfileId::new(value).unwrap().as_ref(), value);
        }
    }

    #[test]
    fn profile_id_rejects_reserved_default() {
        assert!(ProfileId::new("default").is_err());
    }

    #[test]
    fn profile_id_rejects_invalid_topic_segment_shape() {
        for value in [
            "",
            "Foo",
            "a..b",
            ".lead",
            "trail.",
            "has space",
            "has/slash",
        ] {
            assert!(
                ProfileId::new(value).is_err(),
                "expected '{value}' to be rejected"
            );
        }
    }

    #[test]
    fn camera_profile_spec_round_trips_through_profile_id() {
        let spec = CameraProfileSpec {
            width_px: 640,
            height_px: 480,
            publish_rate_hz: 15.0,
            encoding: CameraProfileEncoding::Rgb8,
        };

        let profile_id = spec.to_profile_id().unwrap();
        let parsed = CameraProfileSpec::from_profile_id(&profile_id).unwrap();

        assert_eq!(profile_id.as_ref(), "r640x480_h15_rgb8");
        assert_eq!(parsed, ParsedCameraProfileSpec::Spec(spec));
    }

    #[test]
    fn depth_profile_spec_round_trips_through_profile_id() {
        let spec = DepthProfileSpec {
            width_px: 320,
            height_px: 240,
            publish_rate_hz: 5.0,
        };

        let profile_id = spec.to_profile_id().unwrap();
        let parsed = DepthProfileSpec::from_profile_id(&profile_id).unwrap();

        assert_eq!(profile_id.as_ref(), "r320x240_h5");
        assert_eq!(parsed, ParsedDepthProfileSpec::Spec(spec));
    }

    #[test]
    fn rate_profile_spec_round_trips_through_profile_id() {
        let spec = RateProfileSpec {
            publish_rate_hz: 50.0,
        };

        let profile_id = spec.to_profile_id().unwrap();
        let parsed = RateProfileSpec::from_profile_id(&profile_id).unwrap();

        assert_eq!(profile_id.as_ref(), "h50");
        assert_eq!(parsed, ParsedRateProfileSpec::Spec(spec));
    }

    #[test]
    fn default_profile_parses_as_native_sentinel() {
        assert_eq!(
            CameraProfileSpec::from_profile_id(&ProfileId::default_profile()).unwrap(),
            ParsedCameraProfileSpec::Native
        );
        assert_eq!(
            DepthProfileSpec::from_profile_id(&ProfileId::default_profile()).unwrap(),
            ParsedDepthProfileSpec::Native
        );
        assert_eq!(
            RateProfileSpec::from_profile_id(&ProfileId::default_profile()).unwrap(),
            ParsedRateProfileSpec::Native
        );
    }

    #[test]
    fn out_of_form_profile_id_errors() {
        let profile_id = ProfileId::new("localize.rgb").unwrap();

        assert!(CameraProfileSpec::from_profile_id(&profile_id).is_err());
        assert!(DepthProfileSpec::from_profile_id(&profile_id).is_err());
        assert!(RateProfileSpec::from_profile_id(&profile_id).is_err());
    }

    #[test]
    fn malformed_rate_profile_ids_error() {
        for value in ["h", "h0", "h5.5", "h50_extra", "r320x240_h5"] {
            let profile_id = ProfileId::new(value).unwrap();
            assert!(
                RateProfileSpec::from_profile_id(&profile_id).is_err(),
                "expected '{value}' to be rejected"
            );
        }
    }
}