qualia_core_db/sensing/container/
registration.rs1use super::super::record::MeasurementRecord;
3use super::{
4 pack_sensing_index_field, validate_kind, PackedSensingIndex, SensingContainerError,
5 SensingIndexView,
6};
7use crate::container_10d::{
8 typed_payloads::{TypedPayloadError, TypedPayloadResolver},
9 TypedFieldRecord,
10};
11
12const _: () = assert!(
13 super::super::record::MEASUREMENT_INDEX_VERSION == 1
14 && super::super::record::MEASUREMENT_INDEX_RECORD_SIZE == 264
15);
16
17pub const NATIVE_SENSING_INDEX_KIND_V1: u16 = 0x0400;
20pub const SENSING_INDEX_SCHEMA_V1: &str = "urn:qualia:sensing:measurement-index:v1";
21pub const SENSING_INDEX_SCHEMA_V1_HASH: u64 = crate::q_hash(SENSING_INDEX_SCHEMA_V1);
22pub const MAX_SENSING_PROFILE_EXTENSIONS: usize = 64;
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25pub struct SensingIndexProfile {
26 kind: u16,
27}
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub enum RegistrationError {
30 InvalidKind,
31 ConflictingKind,
32 Capacity,
33}
34
35impl SensingIndexProfile {
36 pub const fn native_v1() -> Self {
37 Self {
38 kind: NATIVE_SENSING_INDEX_KIND_V1,
39 }
40 }
41 pub fn new(kind: u16) -> Result<Self, RegistrationError> {
42 validate_kind(kind).map_err(|_| RegistrationError::InvalidKind)?;
43 Ok(Self { kind })
44 }
45 pub const fn kind(self) -> u16 {
46 self.kind
47 }
48
49 pub fn write_supported_kinds(
52 self,
53 other: &[u16],
54 output: &mut [u16],
55 ) -> Result<usize, RegistrationError> {
56 if other.len() >= MAX_SENSING_PROFILE_EXTENSIONS || output.len() < other.len() + 1 {
57 return Err(RegistrationError::Capacity);
58 }
59 validate_kind(self.kind).map_err(|_| RegistrationError::InvalidKind)?;
60 for (i, kind) in other.iter().enumerate() {
61 validate_kind(*kind).map_err(|_| RegistrationError::InvalidKind)?;
62 if *kind == self.kind || other[..i].contains(kind) {
63 return Err(RegistrationError::ConflictingKind);
64 }
65 }
66 let count = other.len() + 1;
67 output[0] = self.kind;
68 output[1..count].copy_from_slice(other);
69 output[..count].sort_unstable();
70 Ok(count)
71 }
72
73 pub fn pack(
74 self,
75 container_id: [u8; 16],
76 index_field_id: u64,
77 raw_field_prefix: &[u8],
78 records: &[MeasurementRecord],
79 output: &mut [u8],
80 ) -> Result<PackedSensingIndex, SensingContainerError> {
81 pack_sensing_index_field(
82 container_id,
83 index_field_id,
84 self.kind,
85 raw_field_prefix,
86 records,
87 output,
88 )
89 }
90 pub fn open<'r, 'data, 'fields>(
91 self,
92 resolver: &'r TypedPayloadResolver<'data, 'fields>,
93 index_field_id: u64,
94 container_id: [u8; 16],
95 ) -> Result<SensingIndexView<'r, 'data, 'fields>, SensingContainerError> {
96 SensingIndexView::open(resolver, index_field_id, self.kind, container_id)
97 }
98
99 pub fn resolve<'data, 'fields>(
101 self,
102 data: &'data [u8],
103 other_extensions: &[u16],
104 field_scratch: &mut [TypedFieldRecord],
105 fields: &'fields mut [TypedFieldRecord],
106 ) -> Result<TypedPayloadResolver<'data, 'fields>, ProfileResolverError> {
107 let mut extensions = [0u16; MAX_SENSING_PROFILE_EXTENSIONS];
108 let count = self
109 .write_supported_kinds(other_extensions, &mut extensions)
110 .map_err(ProfileResolverError::Registration)?;
111 TypedPayloadResolver::new(data, &extensions[..count], field_scratch, fields)
112 .map_err(ProfileResolverError::Payload)
113 }
114}
115
116#[derive(Debug, Clone, PartialEq, Eq)]
117pub enum ProfileResolverError {
118 Registration(RegistrationError),
119 Payload(TypedPayloadError),
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125 #[test]
126 fn explicit_registration_is_canonical_and_transactional() {
127 let profile = SensingIndexProfile::native_v1();
128 let mut out = [0xffff; 4];
129 assert_eq!(
130 profile.write_supported_kinds(&[0x8101, 0x0500], &mut out),
131 Ok(3)
132 );
133 assert_eq!(out, [NATIVE_SENSING_INDEX_KIND_V1, 0x0500, 0x8101, 0xffff]);
134 let before = out;
135 assert_eq!(
136 profile.write_supported_kinds(&[NATIVE_SENSING_INDEX_KIND_V1], &mut out),
137 Err(RegistrationError::ConflictingKind)
138 );
139 assert_eq!(
140 profile.write_supported_kinds(&[0x8101, 0x8101], &mut out),
141 Err(RegistrationError::ConflictingKind)
142 );
143 assert_eq!(
144 profile.write_supported_kinds(&[1], &mut out),
145 Err(RegistrationError::InvalidKind)
146 );
147 assert_eq!(out, before);
148 }
149 #[test]
150 fn native_kind_is_opt_in_and_remappable() {
151 assert!(
152 !crate::container_10d::manifold_identity_v2::BUILTIN_FIELD_KINDS
153 .contains(&NATIVE_SENSING_INDEX_KIND_V1)
154 );
155 assert!(SensingIndexProfile::new(0).is_err());
156 assert!(SensingIndexProfile::new(0x0100).is_err());
157 assert_eq!(SensingIndexProfile::new(0x8101).unwrap().kind(), 0x8101);
158 }
159}