How to use csv-async crate with Rocket? #2601
-
I'm currently working on a small project where I open a CSV file as part of a GET request with Rocket. Of course I am trying to do this asynchronously so I'm trying to use csv-async and the following is my effort so far: #[macro_use] extern crate rocket;
use rocket_dyn_templates::{Template, context};
#[get("/")]
async fn hello() -> Template {
let mut rdr: csv_async::AsyncReader<Result<async_std::fs::File, std::io::Error>> = csv_async::AsyncReader::from_reader(async_std::fs::File::open("csv/entities.csv").await);
Template::render("index", context! {
name: "Derek",
items: vec!["One", "Two", "Three"],
})
}
#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![hello])
.attach(Template::fairing())
} Now the line
So my question is, am I using csv-async and Rocket incorrectly together or is this a bug where these two crates are incompatible with each other because of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I'm not familiar with The problem you are encountering is that #[get("/")]
async fn hello() -> Template {
// handle for errors first
let Ok(file) = tokio::fs::File::open("csv/entities.csv").await else {
todo!("return 404 template or whatever")
};
// now we can use the file however we like
let mut reader = csv_async::AsyncReader::from_reader(file);
Template::render(
"index",
context! {
name: "Derek",
items: vec!["One", "Two", "Three"],
},
)
} Also, one thing I notice is you are using |
Beta Was this translation helpful? Give feedback.
I'm not familiar with
csv-async
, but from a glance, what is your intent when passingAsyncReader
aResult<T, E>
?The problem you are encountering is that
Result<T, E>
doesn't implAsyncRead
. You cannot impl this either, asResult
is defined in Rust core. Of course, you could use a NewType to get around it, but all of that can be sidestepped by just structuring your code differently.