Destructuring reqwest response data

How do I destructure DictionaryApiResponse from
type std::option::Option<Vec<DictionaryApiResponse>>

The full code

...
async fn search(Request(request): Request<ApiRequest>) -> impl IntoResponse {
    //destructure the request
    let ApiRequest { keyword, language } = &request;
    let body = reqwest::get(format!("{}/{}/{}", &DICTIONARY_API, language, keyword))
        .await
        .unwrap();

    //error handling
    let data = match body.json::<Vec<DictionaryApiResponse>>().await {
        Ok(val) => Some(val),
        _ => None,
    };
    //try to destructure the response
    let data: std::option::Option<Vec<DictionaryApiResponse>> = data;
}

You can probably rewrite that function to return something like Result<impl IntoResponse, impl IntoResponse> and then just use the ? operator.

fn search() -> Result<impl IntoResponse, impl IntoResponse> {
    let data = reqwest::get(url)
        .await?
        .json::<Vec<Response>>()
        .await?;
    for res in data { ... }
    ...
}

Check the documentation of wherever IntoResponse comes from.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.