From aa57ffebc75c8ef5caa0534d7976f43c01f86051 Mon Sep 17 00:00:00 2001 From: Jesper Svennevid <152262941+ttkjesper@users.noreply.github.com> Date: Mon, 15 Jul 2024 14:50:09 +0200 Subject: [PATCH] tonic-reflection: Restructure crate to prep reintroducing v1alpha (#1802) To simplify API verification, this is a prepatory change to shift around the v1 support to allow for a cleaner v1alpha reintroduction. * Created a multi-file version of the server.rs module * Added mod.rs containing the base error with associated imports * Added v1.rs containing the v1 version of the reflection server --- tonic-reflection/src/server/mod.rs | 36 +++++++++++++++++++ .../src/{server.rs => server/v1.rs} | 33 ++--------------- 2 files changed, 39 insertions(+), 30 deletions(-) create mode 100644 tonic-reflection/src/server/mod.rs rename tonic-reflection/src/{server.rs => server/v1.rs} (92%) diff --git a/tonic-reflection/src/server/mod.rs b/tonic-reflection/src/server/mod.rs new file mode 100644 index 000000000..d4889e79d --- /dev/null +++ b/tonic-reflection/src/server/mod.rs @@ -0,0 +1,36 @@ +pub use crate::pb::v1::server_reflection_server::{ServerReflection, ServerReflectionServer}; + +use prost::DecodeError; +use std::fmt::{Display, Formatter}; + +mod v1; + +pub use v1::Builder; + +/// Represents an error in the construction of a gRPC Reflection Service. +#[derive(Debug)] +pub enum Error { + /// An error was encountered decoding a `prost_types::FileDescriptorSet` from a buffer. + DecodeError(prost::DecodeError), + /// An invalid `prost_types::FileDescriptorProto` was encountered. + InvalidFileDescriptorSet(String), +} + +impl From for Error { + fn from(e: DecodeError) -> Self { + Error::DecodeError(e) + } +} + +impl std::error::Error for Error {} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + Error::DecodeError(_) => f.write_str("error decoding FileDescriptorSet from buffer"), + Error::InvalidFileDescriptorSet(s) => { + write!(f, "invalid FileDescriptorSet - {}", s) + } + } + } +} diff --git a/tonic-reflection/src/server.rs b/tonic-reflection/src/server/v1.rs similarity index 92% rename from tonic-reflection/src/server.rs rename to tonic-reflection/src/server/v1.rs index b84184b27..da6f9992b 100644 --- a/tonic-reflection/src/server.rs +++ b/tonic-reflection/src/server/v1.rs @@ -1,4 +1,4 @@ -pub use crate::pb::v1::server_reflection_server::{ServerReflection, ServerReflectionServer}; +use crate::pb::v1::server_reflection_server::{ServerReflection, ServerReflectionServer}; use crate::pb::v1::server_reflection_request::MessageRequest; use crate::pb::v1::server_reflection_response::MessageResponse; @@ -6,45 +6,18 @@ use crate::pb::v1::{ ExtensionNumberResponse, FileDescriptorResponse, ListServiceResponse, ServerReflectionRequest, ServerReflectionResponse, ServiceResponse, }; -use prost::{DecodeError, Message}; +use prost::Message; use prost_types::{ DescriptorProto, EnumDescriptorProto, FieldDescriptorProto, FileDescriptorProto, FileDescriptorSet, }; use std::collections::HashMap; -use std::fmt::{Display, Formatter}; use std::sync::Arc; use tokio::sync::mpsc; use tokio_stream::{wrappers::ReceiverStream, StreamExt}; use tonic::{Request, Response, Status, Streaming}; -/// Represents an error in the construction of a gRPC Reflection Service. -#[derive(Debug)] -pub enum Error { - /// An error was encountered decoding a `prost_types::FileDescriptorSet` from a buffer. - DecodeError(prost::DecodeError), - /// An invalid `prost_types::FileDescriptorProto` was encountered. - InvalidFileDescriptorSet(String), -} - -impl From for Error { - fn from(e: DecodeError) -> Self { - Error::DecodeError(e) - } -} - -impl std::error::Error for Error {} - -impl Display for Error { - fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - match self { - Error::DecodeError(_) => f.write_str("error decoding FileDescriptorSet from buffer"), - Error::InvalidFileDescriptorSet(s) => { - write!(f, "invalid FileDescriptorSet - {}", s) - } - } - } -} +use crate::server::Error; /// A builder used to construct a gRPC Reflection Service. #[derive(Debug)]