-
SummaryI want to introspect an #[derive(Serialize, Deserialize)]
struct Payload(Option<serde_json::Value>);
impl Payload {
fn new(value: impl Serialize) -> core::result::Result<Self, InternalError> {
serde_json::to_value(value)
.map(|value| Self(Some(value)))
.map_err(|_| InternalError::new("Error"))
}
}
impl IntoResponse for Payload {
fn into_response(self) -> Response {
(StatusCode::OK, Json(self.0)).into_response()
}
}
#[derive(Serialize, Deserialize)]
struct InternalError(&'static str);
impl InternalError {
pub fn new(info: &'static str) -> Self {
Self(info)
}
}
impl IntoResponse for InternalError {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, Json(self.0)).into_response()
}
}
#[derive(Clone, Default)]
struct MyState(Arc<Mutex<DummyStruct>>);
#[derive(Serialize)]
struct Response1 {
parameter1: f64,
parameter2: bool,
}
#[derive(Deserialize)]
struct Inputs {
parameter1: f64,
#[serde(rename = "my-name")]
parameter2: bool,
}
async fn first(
Extension(state): Extension<MyState>,
Json(input): Json<Inputs>,
) -> Result<Payload, InternalError> {
let a = state.0.lock().await;
Payload::new(Response1 {
parameter1: input.parameter1,
parameter2: input.parameter2,
})
} Is it possible to retrieve in some way axum version0.7.5 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Yes, this is possible (at compile time). Basically, you have to define your own trait GetAsyncFnReturnTypeName<Args> {
const TYPE_NAME: &'static str;
}
impl<F, Fut> GetAsyncFnReturnTypeName<()> for F
where
F: FnOnce() -> Fut,
Fut: Future,
{
const TYPE_NAME: &'static str = type_name::<Fut::Output>();
}
impl<F, A1, Fut> GetAsyncFnReturnTypeName<(A1,)> for F
where
F: FnOnce(A1) -> Fut,
Fut: Future,
{
const TYPE_NAME: &'static str = type_name::<Fut::Output>();
}
impl<F, A1, A2, Fut> GetAsyncFnReturnTypeName<(A1, A2)> for F
where
F: FnOnce(A1, A2) -> Fut,
Fut: Future,
{
const TYPE_NAME: &'static str = type_name::<Fut::Output>();
}
// ... for as many arguments as you need if you have your own |
Beta Was this translation helpful? Give feedback.
I want to share my solution which also answers to this discussion.