Skip to main content

TypeDescriptor

Struct TypeDescriptor 

Source
#[non_exhaustive]
pub struct TypeDescriptor { pub id: TypeId, pub name: String, pub version: String, pub canonical_collection: Option<String>, pub validate_json: JsonValidatorFn, pub proto3_zero_fn: Proto3ZeroFn, pub fields: Vec<FieldDescriptor>, }
Expand description

Descriptor for one known type. Registries hand back references to these so consumers can use them as a stable handle through their own data flow.

#[non_exhaustive]: additional fields (renderer hints, deprecation markers, etc.) can be added in follow-on commits without breaking external consumers. Construct externally via TypeDescriptor::new plus direct field assignment for the optional fields.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§id: TypeId

Canonical, stable identifier.

§name: String

Human-readable display name (without the proto path).

§version: String

Version label (e.g. "v1"). Convention; not parsed.

§canonical_collection: Option<String>

Conventional collection where documents of this type live. None for types that aren’t associated with a single collection by convention. Consumers MAY override this at the registry-build layer for deployment-specific collection layouts.

§validate_json: JsonValidatorFn

Validator: JSON value → typed message → field-level validate.

§proto3_zero_fn: Proto3ZeroFn

Proto3 wire-zero JSON supplier (peat#953). Returns the canonical proto3 zero JSON for this type — every field populated with its proto3 default (string → “”, numeric → 0/0.0, bool → false, enum → first variant, repeated → [], optional message → null).

Driven by the prost-generated Default impl on the underlying message struct, so the value cannot drift from the proto3 field list — consumers always get the current canonical zero shape as the schema evolves.

Defaulted to default_proto3_zero (returns {}) by Self::new so external callers that haven’t wired in a proto3-aware zero supplier fall back to a safe empty-object shape; descriptors registered by BuiltinRegistry::with_peat_schema_types are built by the crate’s private descriptors module helpers, which set this field explicitly via the prost-generated Default of each type. (Plain prose rather than an intra-doc link on the helpers — the descriptors module is private, so a [\mod@descriptors`]link would failrustdoc::private-intra-doc-linksundercargo doc – -D warnings`.)

§fields: Vec<FieldDescriptor>

Fields in canonical display order, with rendering hints. Renderer-side downstream consumers iterate this list to produce typed output. Empty for types that don’t yet have field metadata authored.

Implementations§

Source§

impl TypeDescriptor

Source

pub fn new( id: TypeId, name: impl Into<String>, version: impl Into<String>, validate_json: JsonValidatorFn, ) -> Self

Construct a TypeDescriptor with the required fields. Optional fields (canonical_collection, proto3_zero_fn, fields) default to empty / no-op; callers set them via direct field assignment after construction.

Use this from external crates that register their own types — direct struct-literal construction is reserved for peat-schema itself so future field additions stay non-breaking.

Source

pub fn proto3_zero(&self) -> Value

Return the canonical proto3 wire-zero JSON for this type (peat#953).

Every field of the proto3 message is populated with its zero value, exactly as the prost-generated Default::default() for the underlying struct would produce after serde_json::to_value. Consumers that need to fill in a partial user payload before strict prost-derived Deserialize (e.g. peat-cli’s apply_proto3_defaults for partial --set payloads, or any SDK that needs to construct a baseline document programmatically) merge user-supplied fields on top of this value.

Round-trips through this type’s Deserialize impl by construction: the value is serde_json::to_value(<T>::default()) where <T> already implements both Default (prost) and Serialize (peat-schema build script), so the deserialise of the wire-form of a valid in-memory instance is always a valid in-memory instance.

§Example
use peat_schema::type_registry::{BuiltinRegistry, TypeRegistry};

let registry = BuiltinRegistry::with_peat_schema_types();
let desc = registry.for_collection("capabilities").unwrap();
let zero = desc.proto3_zero();
// Every field of `Capability` is present at its proto3 zero.
assert!(zero.is_object());

Trait Implementations§

Source§

impl Clone for TypeDescriptor

Source§

fn clone(&self) -> TypeDescriptor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TypeDescriptor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.