Files
mxaccess/rust/crates/mxaccess-galaxy/src/metadata.rs
T
Joseph Doherty d84b066c62 [M3] mxaccess-galaxy: GalaxyTagMetadata + parser + Resolver trait + SQL
Lands M3 stream A — the pure-Rust foundation of the Galaxy resolver:
the data type, the tag-reference parser, the async trait, and the
canonical SQL strings. Unblocks F13 (NmxClient::write_* wrappers depend
on GalaxyTagMetadata) without pulling in tiberius yet.

New
- metadata.rs (~195 LoC, 7 tests) — GalaxyTagMetadata record (port of
  cs:6-73). Includes is_buffer_property + to_reference_handle(galaxy_id)
  bridging into mxaccess-codec::MxReferenceHandle::from_names.
- parser.rs (~330 LoC, 12 tests) — ParsedTagReference parser. Handles
  Object.Attribute (1 candidate), Object.Primitive.Attribute (2
  candidates: primitive-attr first, dotted-attr second per cs:181-185),
  and the case-insensitive .property(buffer) suffix. Pure-Rust, no I/O.
- resolver.rs (~200 LoC, 5 tests including a tokio-driven InMemoryResolver
  proving the trait is implementable without SQL) — async Resolver trait
  + ResolverError. Default browse returns Backend("not implemented") so
  read-only backends don't need to override it.
- sql.rs (~280 LoC, 5 smoke tests) — RESOLVE_SQL + BROWSE_SQL constants
  ported byte-for-byte from cs:208-432. Available publicly so any
  backend (the planned tiberius impl, a wwtools/grdb snapshot replay,
  etc.) can grab the canonical query.

Cargo.toml: added mxaccess-codec (path), async-trait, thiserror;
tokio added as dev-dependency for the resolver-trait async tests.

Deliberately deferred to a later iteration:
- The tiberius-backed Resolver impl behind the galaxy-resolver feature.
- ToValueKind / TryGetValueKind / ProjectWriteValue helpers on
  GalaxyTagMetadata (cs:41-72) — these need a MxDataType -> MxValueKind
  lookup that the codec doesn't currently expose; landing them with
  F13's write-helper iteration keeps the iteration coherent.

Test count delta: 397 -> 427 (+30). All four DoD gates green.
Open followups touched: F13 prerequisite (GalaxyTagMetadata) now in
place; F13 itself stays open until the write helpers wire it up.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 08:17:16 -04:00

196 lines
7.0 KiB
Rust

//! `GalaxyTagMetadata` — the resolved attribute-metadata record.
//!
//! Direct port of the `GalaxyTagMetadata` record at the top of
//! `src/MxNativeClient/GalaxyRepositoryTagResolver.cs:6-73`. Carries the
//! exact set of fields the .NET reference reads from a Galaxy SQL row,
//! plus three small derived helpers:
//!
//! - [`GalaxyTagMetadata::is_buffer_property`] (mirrors the `IsBufferProperty`
//! property at `cs:24`).
//! - [`GalaxyTagMetadata::to_reference_handle`] (mirrors `ToReferenceHandle`
//! at `cs:26-39`) — converts the metadata into a wire-ready
//! [`mxaccess_codec::MxReferenceHandle`].
//!
//! The .NET reference's `ToValueKind`, `TryGetValueKind`, `IsSupportedValueKind`,
//! and `ProjectWriteValue` (`cs:41-72`) are deferred to a future iteration
//! when the high-level `NmxClient::write_*` wrappers (followup F13) actually
//! need them. They reduce to a `MxDataType` → `MxValueKind` lookup table that
//! the codec doesn't currently expose; adding both the lookup and the
//! projection together keeps the iteration that needs them coherent.
use mxaccess_codec::{CodecError, MxReferenceHandle};
/// Resolved Galaxy tag metadata. Field order and types match the .NET
/// `GalaxyTagMetadata` record exactly (`cs:6-19`).
///
/// # Numeric ranges
///
/// `platform_id`, `engine_id`, `object_id` are stored as `u16` because they
/// come from `dbo.instance.mx_*_id` (SQL `smallint` checked-cast to ushort
/// in .NET — `cs:155-157`). `primitive_id`, `attribute_id`, `property_id`,
/// `mx_data_type`, `security_classification` are `i16` for the same reason
/// (signed `smallint`).
///
/// `property_id` is sourced from `SQL int` and checked-cast to `i16`
/// (`cs:160`). The Rust port stores `i16` to match the .NET shape; values
/// outside the i16 range are a SQL-side issue, not a Rust-side issue.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GalaxyTagMetadata {
pub object_tag_name: String,
pub attribute_name: String,
/// `None` for `dynamic` attributes; `Some(name)` for `primitive`.
/// Mirrors `string? PrimitiveName` (`cs:9`).
pub primitive_name: Option<String>,
pub platform_id: u16,
pub engine_id: u16,
pub object_id: u16,
pub primitive_id: i16,
pub attribute_id: i16,
pub property_id: i16,
pub mx_data_type: i16,
pub is_array: bool,
pub security_classification: i16,
/// Provenance tag — either `"dynamic"` or `"primitive"` per the SQL
/// `attribute_source` column (`cs:247,276,375,399`).
pub attribute_source: String,
}
impl GalaxyTagMetadata {
/// Default `property_id` for ordinary value attributes — `cs:21`.
pub const VALUE_PROPERTY_ID: i16 = 10;
/// `property_id` used for `(buffer)` property references — `cs:22`.
pub const BUFFER_PROPERTY_ID: i16 = 50;
/// `true` when [`Self::property_id`] equals [`Self::BUFFER_PROPERTY_ID`].
/// Mirrors `IsBufferProperty` (`cs:24`).
#[must_use]
pub const fn is_buffer_property(&self) -> bool {
self.property_id == Self::BUFFER_PROPERTY_ID
}
/// Build the wire-form [`MxReferenceHandle`] this metadata describes.
/// Mirrors `ToReferenceHandle(byte galaxyId = 1)` (`cs:26-39`).
///
/// `galaxy_id` defaults to 1 in the .NET signature; the Rust port makes
/// it explicit so callers don't accidentally use `0` (which would
/// produce a different wire handle).
///
/// # Errors
///
/// Propagates [`CodecError::InvalidName`] from
/// [`MxReferenceHandle::from_names`] when either name is empty or
/// whitespace-only.
pub fn to_reference_handle(&self, galaxy_id: u8) -> Result<MxReferenceHandle, CodecError> {
MxReferenceHandle::from_names(
galaxy_id,
self.platform_id,
self.engine_id,
self.object_id,
&self.object_tag_name,
self.primitive_id,
self.attribute_id,
self.property_id,
&self.attribute_name,
self.is_array,
)
}
}
#[cfg(test)]
#[allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic
)]
mod tests {
use super::*;
fn sample(
property_id: i16,
primitive_id: i16,
primitive_name: Option<&str>,
) -> GalaxyTagMetadata {
GalaxyTagMetadata {
object_tag_name: "TestObject_001".to_string(),
attribute_name: "TestInt".to_string(),
primitive_name: primitive_name.map(str::to_string),
platform_id: 5,
engine_id: 7,
object_id: 42,
primitive_id,
attribute_id: 99,
property_id,
mx_data_type: 4,
is_array: false,
security_classification: 0,
attribute_source: if primitive_name.is_some() {
"primitive"
} else {
"dynamic"
}
.to_string(),
}
}
#[test]
fn property_id_constants_match_dotnet() {
// .NET `GalaxyTagMetadata.ValuePropertyId` and `BufferPropertyId` at cs:21-22.
assert_eq!(GalaxyTagMetadata::VALUE_PROPERTY_ID, 10);
assert_eq!(GalaxyTagMetadata::BUFFER_PROPERTY_ID, 50);
}
#[test]
fn is_buffer_property_true_only_for_50() {
assert!(!sample(10, 0, None).is_buffer_property());
assert!(sample(50, 0, None).is_buffer_property());
assert!(!sample(0, 0, None).is_buffer_property());
assert!(!sample(11, 0, None).is_buffer_property());
}
#[test]
fn to_reference_handle_builds_wire_handle() {
let meta = sample(10, -1, None); // primitive_id = -1, the .NET "no primitive" sentinel
let handle = meta.to_reference_handle(1).unwrap();
assert_eq!(handle.galaxy_id, 1);
assert_eq!(handle.platform_id, 5);
assert_eq!(handle.engine_id, 7);
assert_eq!(handle.object_id, 42);
assert_eq!(handle.attribute_id, 99);
assert_eq!(handle.property_id, 10);
// is_array = false → attribute_index = 0 per MxReferenceHandle::from_names.
assert_eq!(handle.attribute_index, 0);
}
#[test]
fn to_reference_handle_array_sets_attribute_index_minus_one() {
let mut meta = sample(10, 0, None);
meta.is_array = true;
let handle = meta.to_reference_handle(1).unwrap();
assert_eq!(handle.attribute_index, -1);
}
#[test]
fn to_reference_handle_rejects_empty_name() {
let mut meta = sample(10, 0, None);
meta.object_tag_name = " ".to_string();
let err = meta.to_reference_handle(1).unwrap_err();
assert!(matches!(err, CodecError::InvalidName));
}
#[test]
fn primitive_name_round_trips() {
let meta = sample(10, 0, Some("DelmiaReceiver"));
assert_eq!(meta.primitive_name.as_deref(), Some("DelmiaReceiver"));
assert_eq!(meta.attribute_source, "primitive");
}
#[test]
fn dynamic_metadata_has_no_primitive_name() {
let meta = sample(10, 0, None);
assert_eq!(meta.primitive_name, None);
assert_eq!(meta.attribute_source, "dynamic");
}
}