use crate::{
db::data::{
DecodedDataStoreKey, PersistedRow, RawRow, StructuralRowContract, StructuralSlotReader,
},
error::InternalError,
traits::EntityValue,
};
pub(in crate::db) fn decode_raw_row_for_entity_key_with_contract<E>(
data_key: &DecodedDataStoreKey,
raw_row: &RawRow,
contract: StructuralRowContract,
) -> Result<(E::Key, E), InternalError>
where
E: PersistedRow + EntityValue,
{
let expected_key = data_key.try_key::<E>()?;
let mut slots = StructuralSlotReader::from_raw_row_with_validated_contract(raw_row, contract)
.map_err(|_| InternalError::persisted_row_decode_corruption())?;
let entity = E::materialize_from_slots(&mut slots)
.map_err(|_| InternalError::persisted_row_decode_corruption())?;
let actual_key = entity.id().key();
if expected_key != actual_key {
return Err(InternalError::persisted_row_key_mismatch());
}
Ok((expected_key, entity))
}