I am really confused with a feature I'm trying to implement, let's say I have a Vec<Object> which I am trying to serialize, then trying to send a POST request with the serialized body.
The code you posted wouldn't compile, but for different reasons. The first map maps each Object into a Result<String, serde_error::Error> as you say. The second map then maps each Result<String, serde_error::Error>, which you then try to give to client.post().body(str_object), but you can't pass any Result to .body(), no matter what the Error type. You also can't .await inside a non-async closure.
There are ways to handle errors and async when using iterator combinators, but in this case the idiomatic way is to keep it simple with a for loop:
for object in vec {
let str_object = serde_json::to_string(&object)?;
client.post().body(str_object).send().await?;
}
You can do that, and that's typically the approach taken by library code. See thiserror - Rust to make it more convenient.
You can also use a general error type that allow you to convert most other errors into them. Box<dyn Error>, anyhow::Error or eyre::Error are some such types. For example
async fn send_objects() -> Result<(), anyhow::Error> {
let client = get_client_from_somewhere();
let objects = get_vec_of_objects_from_somewhere();
for object in objects {
let str_object = serde_json::to_string(&object)?;
client.post().body(str_object).send().await?;
}
Ok(())
}