WIP - Decouple Prost

This commit is contained in:
Alec Thilenius 2023-05-29 22:02:12 -07:00
parent ccbebb8dce
commit 93d940f5c7
6 changed files with 45 additions and 17 deletions

View file

@ -3,7 +3,9 @@
"Buf's",
"bufbuild",
"codegen",
"Compressable",
"concat",
"crosstest",
"DTLS",
"impls",
"pbjson",

View file

@ -18,6 +18,8 @@ repository = "https://github.com/AThilenius/axum-connect"
async-stream = "0.3.5"
async-trait = "0.1.64"
axum = "0.6.9"
base-62 = "0.1.1"
bytes = "1.4.0"
futures = "0.3.26"
pbjson = "0.5.1"
pbjson-types = "0.5.1"

View file

@ -1,4 +1,5 @@
use axum::http::StatusCode;
use base_62::base62;
use prost::Message;
use serde::Serialize;
@ -51,8 +52,28 @@ pub struct RpcErrorDetail {
pub proto_type: String,
#[serde(rename = "value")]
pub proto_b62_value: String,
#[serde(rename = "debug")]
pub debug_json: Box<serde_json::value::RawValue>,
}
// impl<M> From<M> for RpcErrorDetail
// where
// M: Message + Serialize,
// {
// fn from(val: M) -> Self {
// let binary = M::encode_to_vec(&val.1);
// // Encode as base62
// let b62 = base62::encode(&binary);
// let json = serde_json::to_string(&val.1).unwrap();
// Self {
// M::
// proto_b62_value: b62,
// debug_json: serde_json::value::RawValue::from_string(json).unwrap(),
// }
// }
// }
#[derive(Clone, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RpcErrorCode {
@ -115,12 +136,3 @@ where
Err(self)
}
}
// TODO: This needs to be done in the handler to support streaming errors.
// impl IntoResponse for RpcError {
// fn into_response(self) -> Response {
// let status_code = StatusCode::from(self.code.clone());
// let json = serde_json::to_string(&self).expect("serialize error type");
// (status_code, json).into_response()
// }
// }

View file

@ -1,5 +1,6 @@
pub mod error;
pub mod handler;
mod message;
pub mod parts;
pub mod response;
pub mod router;
@ -13,6 +14,7 @@ pub use serde;
pub mod prelude {
pub use crate::error::*;
pub use crate::message::*;
pub use crate::parts::*;
pub use crate::response::*;
pub use crate::router::RpcRouterExt;

View file

@ -0,0 +1,18 @@
use bytes::{Buf, BufMut};
use crate::prelude::RpcError;
/// Wrap Prost traits in our own, so I can start decoupling Prost because it's driving me insane.
pub trait Message: prost::Message {
const TYPE_URL: &'static str;
fn encode<B>(&self, buf: &mut B)
where
B: BufMut,
Self: Sized;
fn decode<B>(buf: B) -> Result<Self, RpcError>
where
B: Buf,
Self: Default;
}

View file

@ -1,8 +0,0 @@
version: v1
name: buf.build/woodriver-energy/blink
breaking:
use:
- FILE
lint:
use:
- DEFAULT