From 93d940f5c7475e5872ef4d8ee93b14c5ed2e106f Mon Sep 17 00:00:00 2001 From: Alec Thilenius Date: Mon, 29 May 2023 22:02:12 -0700 Subject: [PATCH] WIP - Decouple Prost --- .vscode/settings.json | 2 ++ axum-connect/Cargo.toml | 2 ++ axum-connect/src/error.rs | 30 +++++++++++++++++++++--------- axum-connect/src/lib.rs | 2 ++ axum-connect/src/message.rs | 18 ++++++++++++++++++ buf.yaml | 8 -------- 6 files changed, 45 insertions(+), 17 deletions(-) create mode 100644 axum-connect/src/message.rs delete mode 100644 buf.yaml diff --git a/.vscode/settings.json b/.vscode/settings.json index 6e8b06a..1108e21 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,7 +3,9 @@ "Buf's", "bufbuild", "codegen", + "Compressable", "concat", + "crosstest", "DTLS", "impls", "pbjson", diff --git a/axum-connect/Cargo.toml b/axum-connect/Cargo.toml index 1447ecd..c577fac 100644 --- a/axum-connect/Cargo.toml +++ b/axum-connect/Cargo.toml @@ -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" diff --git a/axum-connect/src/error.rs b/axum-connect/src/error.rs index 9b134ac..d3d1013 100644 --- a/axum-connect/src/error.rs +++ b/axum-connect/src/error.rs @@ -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, } +// impl From 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() -// } -// } diff --git a/axum-connect/src/lib.rs b/axum-connect/src/lib.rs index 4a0d0a6..a766f3e 100644 --- a/axum-connect/src/lib.rs +++ b/axum-connect/src/lib.rs @@ -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; diff --git a/axum-connect/src/message.rs b/axum-connect/src/message.rs new file mode 100644 index 0000000..ed0316a --- /dev/null +++ b/axum-connect/src/message.rs @@ -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(&self, buf: &mut B) + where + B: BufMut, + Self: Sized; + + fn decode(buf: B) -> Result + where + B: Buf, + Self: Default; +} diff --git a/buf.yaml b/buf.yaml deleted file mode 100644 index b4e2cb1..0000000 --- a/buf.yaml +++ /dev/null @@ -1,8 +0,0 @@ -version: v1 -name: buf.build/woodriver-energy/blink -breaking: - use: - - FILE -lint: - use: - - DEFAULT