Skip to main content

Crate oxiproto_reflect

Crate oxiproto_reflect 

Source
Expand description

Runtime protobuf reflection via prost-reflect.

This crate provides a thin facade over prost_reflect for dynamic protobuf operations: building a DescriptorPool from a prost_types::FileDescriptorSet and constructing DynamicMessage instances at runtime without generated Rust types.

§Quick start

use oxiproto_reflect::{pool_from_fds_bytes, dynamic_message};

// `fds_bytes` is the raw bytes of a `FileDescriptorSet` proto.
let pool = pool_from_fds_bytes(fds_bytes)?;
let msg  = dynamic_message(&pool, "my.package.MyMessage")?;
println!("fields: {:?}", msg.descriptor().fields().collect::<Vec<_>>());

§Debug and Display for DynamicMessage

DynamicMessage implements both std::fmt::Debug and std::fmt::Display (protobuf text format). The following example verifies both traits work correctly through this crate’s re-exports.

use oxiproto_reflect::{pool_from_fds, DynamicMessage};
use prost_types::{
    FileDescriptorSet, FileDescriptorProto, DescriptorProto, FieldDescriptorProto,
};
use prost_types::field_descriptor_proto::{Label, Type};

let fds = FileDescriptorSet {
    file: vec![FileDescriptorProto {
        name: Some("test.proto".to_string()),
        syntax: Some("proto3".to_string()),
        message_type: vec![DescriptorProto {
            name: Some("Ping".to_string()),
            field: vec![FieldDescriptorProto {
                name: Some("value".to_string()),
                number: Some(1),
                label: Some(Label::Optional as i32),
                r#type: Some(Type::Int32 as i32),
                json_name: Some("value".to_string()),
                ..Default::default()
            }],
            ..Default::default()
        }],
        ..Default::default()
    }],
};

let pool = pool_from_fds(fds).unwrap();
let msg_desc = pool.get_message_by_name("Ping").unwrap();
let msg = DynamicMessage::new(msg_desc);

// Debug format is always available.
let debug_str = format!("{msg:?}");
assert!(!debug_str.is_empty());

// Display uses the protobuf text format; an empty message formats to "".
let display_str = format!("{msg}");
assert_eq!(display_str, "");

Re-exports§

pub use dynamic::clear_field;
pub use dynamic::get_field_by_name;
pub use dynamic::has_field;
pub use dynamic::set_field_by_name;
pub use dynamic::unknown_fields;
pub use native::Cardinality as NativeCardinality;
pub use native::DescriptorPool as NativeDescriptorPool;
pub use native::DynamicMessage as NativeDynamicMessage;
pub use native::EnumDescriptor as NativeEnumDescriptor;
pub use native::EnumValueDescriptor as NativeEnumValueDescriptor;
pub use native::FieldDescriptor as NativeFieldDescriptor;
pub use native::FileDescriptor as NativeFileDescriptor;
pub use native::Kind as NativeKind;
pub use native::MapKey as NativeMapKey;
pub use native::MessageDescriptor as NativeMessageDescriptor;
pub use native::MethodDescriptor as NativeMethodDescriptor;
pub use native::NativeJsonError;
pub use native::NativeTextError;
pub use native::OneofDescriptor as NativeOneofDescriptor;
pub use native::ServiceDescriptor as NativeServiceDescriptor;
pub use native::Value as NativeValue;

Modules§

dynamic
Free-function convenience helpers for DynamicMessage field access.
native
Native, pure-Rust protobuf reflection.

Structs§

DescriptorPool
A DescriptorPool is a collection of related descriptors. Typically it will be created from a FileDescriptorSet output by the protobuf compiler (see DescriptorPool::from_file_descriptor_set) but it may also be built up by adding files individually.
DynamicMessage
DynamicMessage provides encoding, decoding and reflection of a protobuf message.
EnumDescriptor
A protobuf enum type.
FieldDescriptor
A protobuf message definition.
FileDescriptor
A single source file containing protobuf messages and services.
MessageDescriptor
A protobuf message definition.
MethodDescriptor
A method definition for a ServiceDescriptor.
ServiceDescriptor
A protobuf service definition.
UnknownField
An unknown field found when deserializing a protobuf message.

Enums§

ReflectError
Errors produced by reflection operations.
ReflectValue
Re-export of prost_reflect::Value under a distinct alias to avoid name conflicts with prost_types::Value. A dynamically-typed protobuf value.

Traits§

ReflectMessage
Re-export of the prost_reflect::ReflectMessage trait so callers can use msg.descriptor() without a separate prost_reflect dependency. Trait for message types that support reflection.

Functions§

all_messages
Iterate over all message descriptors registered in the pool.
all_services
Iterate over all service descriptors registered in the pool.
dynamic_message
Construct an empty DynamicMessage for the named message in pool.
get_enum_by_name
Look up an enum descriptor by its fully-qualified name.
get_service_by_name
Look up a service descriptor by its fully-qualified name.
pool_from_fds
Build a DescriptorPool directly from a FileDescriptorSet.
pool_from_fds_bytes
Build a DescriptorPool from the raw bytes of a serialized prost_types::FileDescriptorSet.

Derive Macros§

ReflectMessage
Re-export of the prost_reflect::ReflectMessage trait so callers can use msg.descriptor() without a separate prost_reflect dependency. A derive macro for the ReflectMessage trait.