pub mod build;
pub mod geometry;
pub mod media;
pub mod nav;
pub mod read;
pub mod scalar;
pub mod sensor;
pub mod vision;
use astrs_data::urn::layout_of_str;
use astrs_data::{ArrayRef, DataError, DataType, RecordBatch, Result};
pub use astrs_data::AstrsMessage;
pub use geometry::{Accel, Pose, Quaternion, Transform, Twist, Vector3};
pub use media::{AudioFrame, CompressedImage, Image, ImageSamples, PixelFormat, SampleFormat};
pub use nav::{OccupancyGrid, Odometry, Path, StampedPose};
pub use scalar::{
Bytes, BytesRun, Duration, Empty, Flag, FlagRun, Scalar, ScalarRun, ScalarType, Text, TextRun,
Timestamp, TimestampRun,
};
pub use sensor::{Imu, LaserScan, NavSatFix, PointCloud, Range};
pub use vision::{BoundingBox, Detections, Keypoint, Keypoints, Mask};
pub trait FromPayload: Sized {
fn from_batch(batch: &RecordBatch) -> Result<Self>;
}
pub fn payload_column(batch: &RecordBatch) -> Result<&ArrayRef> {
batch
.payload_column()
.or_else(|| batch.column(0))
.ok_or(DataError::ColumnCountMismatch {
fields: 1,
columns: 0,
})
}
pub fn single_row_column(batch: &RecordBatch) -> Result<&ArrayRef> {
if batch.num_rows() != 1 {
return Err(DataError::MessageRowCount {
actual: batch.num_rows(),
});
}
payload_column(batch)
}
pub fn assert_registry_layout<T: AstrsMessage>() -> Result<()> {
assert_layout_matches(T::URN, &T::data_type())
}
pub fn assert_registry_layout_for<T: AstrsMessage>(urn: &str) -> Result<()> {
assert_layout_matches(urn, &T::data_type())
}
pub fn assert_layout_matches(urn: &str, actual: &DataType) -> Result<()> {
let expected = layout_of_str(urn)?;
if &expected == actual {
return Ok(());
}
Err(DataError::type_mismatch(expected, actual.clone()))
}
macro_rules! impl_from_payload {
($ty:ty) => {
impl $crate::message::FromPayload for $ty {
fn from_batch(batch: &::astrs_data::RecordBatch) -> ::astrs_data::Result<Self> {
<Self as ::astrs_data::AstrsMessage>::from_record_batch(batch)
}
}
};
}
pub(crate) use impl_from_payload;
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use astrs_data::array::{Float64Array, IntoArrayRef};
#[test]
fn the_payload_column_is_found_by_name_and_by_position() {
let batch = RecordBatch::from_payload(Float64Array::from_values([1.0]).into_array_ref());
assert_eq!(payload_column(&batch).unwrap().len(), 1);
assert_eq!(single_row_column(&batch).unwrap().len(), 1);
}
#[test]
fn a_batch_without_columns_is_reported() {
let batch =
RecordBatch::try_new_empty(std::sync::Arc::new(astrs_data::Schema::new(Vec::new())))
.unwrap();
assert!(matches!(
payload_column(&batch),
Err(DataError::ColumnCountMismatch { .. })
));
}
#[test]
fn a_multi_row_batch_is_refused_where_one_row_is_required() {
let batch =
RecordBatch::from_payload(Float64Array::from_values([1.0, 2.0]).into_array_ref());
assert!(matches!(
single_row_column(&batch),
Err(DataError::MessageRowCount { actual: 2 })
));
}
#[test]
fn conformance_reports_a_layout_that_drifted() {
assert!(assert_registry_layout::<Vector3>().is_ok());
let error =
assert_layout_matches("std/geometry/v1/Vector3", &DataType::Float64).unwrap_err();
assert!(matches!(error, DataError::TypeMismatch { .. }), "{error}");
}
#[test]
fn an_unknown_urn_is_reported_by_the_registry() {
let error = assert_layout_matches("std/core/v1/Nonexistent", &DataType::Null).unwrap_err();
assert!(!error.to_string().is_empty());
}
#[test]
fn schemas_carry_the_data_column() {
let schema = Vector3::schema();
assert_eq!(schema.fields().len(), 1);
assert_eq!(schema.field(0).map(astrs_data::Field::name), Some("data"));
assert_eq!(
schema.field(0).map(|field| field.data_type().clone()),
Some(Vector3::data_type())
);
}
}