The trait `Reply` is not implemented for `()`

use mysql::;
use mysql::prelude::
;
use calamine::{Reader, Xlsx, open_workbook};
// use warp::{Filter, hyper::StatusCode};
use warp::Filter;

#[derive(Debug, PartialEq, Eq)]
struct Payment {
label: Option,
value: Option,
}

fn hello() -> std::result::Result<(), Box> {

println!("hiiiiiiiii");
//connect to database
let url = "mysql://root:password@localhost:3306/testDB";
let pool: Pool = Pool::new(url)?;
let mut conn: PooledConn = pool.get_conn()?;

//excel
let mut excel: Xlsx<_> = open_workbook("temperature.xlsx").unwrap();

// Let's create a table for payments.
conn.query_drop(
    r"CREATE TABLE payment (
        label text,
        value int not null
    )")?;

if let Some(Ok(r)) = excel.worksheet_range("Sheet1")
{
    for row in r.rows(){
        let k:String= (&row[0]).to_string();
        let m:String= (&row[1]).to_string();
        // println!("{}",k);
        // println!("{}",m);


        let payments = vec![
            Payment { label: Some(k), value: Some(m) },
        ];

             // Now let's insert payments to the database
         conn.exec_batch(
          r"INSERT INTO payment (label, value)
            VALUES (:label, :value)",
           payments.iter().map(|p| params! {
            "label" => &p.label,
            "value" => &p.value,
            })
        )?;
    }
}

println!("Yay!");

Ok(())

}

#[tokio::main]
async fn main() {

// let hello1 = warp::path!("hello")
//     .map(|| StatusCode::OK);

// let hello = hello1
//     .with(warp::cors().allow_any_origin());

// warp::serve(hello).run(([127, 0, 0, 1], 8000)).await;

let hello = warp::get();

warp::serve(hello).run(([127, 0, 0, 1], 3030)).await;

}

Is there a question?

error[E0277]: the trait bound (): Reply is not satisfied
--> src/main.rs:76:17
|
76 | warp::serve(hello).run(([127, 0, 0, 1], 3030)).await;
| ----------- ^^^^^ the trait Reply is not implemented for ()
| |
| required by a bound introduced by this call
|
= help: the trait Reply is implemented for (T,)
note: required by a bound in serve
--> /home/rohit/.cargo/registry/src/github.com-1ecc6299db9ec823/warp-0.3.2/src/server.rs:26:17
|
26 | F::Extract: Reply,
| ^^^^^ required by this bound in serve

error[E0277]: the trait bound (): Reply is not satisfied
--> src/main.rs:76:5
|
76 | warp::serve(hello).run(([127, 0, 0, 1], 3030)).await;
| ^^^^^^^^^^^^^^^^^^ --- required by a bound introduced by this call
| |
| the trait Reply is not implemented for ()
|
= help: the trait Reply is implemented for (T,)
note: required by a bound in warp::Server::<F>::run
--> /home/rohit/.cargo/registry/src/github.com-1ecc6299db9ec823/warp-0.3.2/src/server.rs:127:35
|
127 | <F::Future as TryFuture>::ok: Reply,
| ^^^^^ required by this bound in warp::Server::<F>::run

warning: unused import: warp::Filter
--> src/main.rs:5:5
|
5 | use warp::Filter;
| ^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

For more information about this error, try rustc --explain E0277.
warning: warp_tokio_rust_api (bin "warp_tokio_rust_api") generated 1 warning
error: could not compile warp_tokio_rust_api due to 2 previous errors; 1 warning emitted

these error I am getting

please help

Please wrap your code and compiler output in triple backticks so it is readable.

For example,

```rust
use mysql::*;

#[tokio::main]
async fn main() {
  ...
}
```

It might also help if you explain what you think the compiler's error message is telling you and what you've done to try and fix it.

1 Like

use mysql::;
use mysql::prelude::
;
use calamine::{Reader, Xlsx, open_workbook};
// use warp::{Filter, hyper::StatusCode};
use warp::Filter;

#[derive(Debug, PartialEq, Eq)]
struct Payment {
label: Option,
value: Option,
}

async fn hello() -> std::result::Result<(), Box> {

println!("hiiiiiiiii");
//connect to database
let url = "mysql://root:password@localhost:3306/testDB";
let pool: Pool = Pool::new(url)?;
let mut conn: PooledConn = pool.get_conn()?;

//excel
let mut excel: Xlsx<_> = open_workbook("temperature.xlsx").unwrap();

// Let's create a table for payments.
conn.query_drop(
    r"CREATE TABLE payment (
        label text,
        value int not null
    )")?;

if let Some(Ok(r)) = excel.worksheet_range("Sheet1")
{
    for row in r.rows(){
        let k:String= (&row[0]).to_string();
        let m:String= (&row[1]).to_string();
        // println!("{}",k);
        // println!("{}",m);


        let payments = vec![
            Payment { label: Some(k), value: Some(m) },
        ];

             // Now let's insert payments to the database
         conn.exec_batch(
          r"INSERT INTO payment (label, value)
            VALUES (:label, :value)",
           payments.iter().map(|p| params! {
            "label" => &p.label,
            "value" => &p.value,
            })
        )?;
    }
}

println!("Yay!");

Ok(())

}

#[tokio::main]
async fn main() {

let hello = warp::get()
.and(warp::path("info"));

warp::serve(hello).run(([127, 0, 0, 1], 3030)).await;

}



I am getting the errors mentioned above.

warp::get returns a Filter implementer with Extract = (), whereas warp::serve needs a Filter implementer with Extract: Reply. That's the shallow explanation of your error. You need something besides just warp::get(), like warp::any().map(|| ...) or something.

I'm not a warp expert and it's not clear to me what you're actually trying to do; e.g. what your hello function (which is shadowed by your local hello variable) is for. So it's hard to give any concrete advice.

Please also take some time to read the pinned post @Michael-F-Bryan linked so you understand how to format your comments.

3 Likes

can you please help out

Not yet. In order to help, we need to understand both:

  1. What you want to do, and
  2. How you have already tried to do it so far.

The source code you’ve provided gives us lots of information about (2), but we still don’t know anything about (1). For that, you’ll need to describe, in words, what you want your source code to do.

I am trying to upload excel sheet data to mysql database using get API using rust.

hope you got it, what I am trying to say.

Your currently don't have any endpoints added to warp. You gave it the HTTP method and path, but haven't given it a function to handle the HTTP Request

If we look at this example from the warp docs, we can see what you're missing

let hi = warp::path("hello")
    .and(warp::path::param())
    .and(warp::header("user-agent"))
    .map(|param: String, agent: String| {
        format!("Hello {}, whose agent is {}", param, agent)
    });

You don't need the header filter right now, but you do need to use map (or one of the other options) to actually handle the HTTP Request.

You should be able to add .map(hello) I think but it's been awhile since I used warp.

I have hello function which I need to call after I hit get API

I've simplified your code to hopefully make it clearer what you need to change

use std::time::Duration;
use warp::Filter;

// Simplified hello function
fn hello() -> std::result::Result<(), Box<dyn std::error::Error>> {
    println!("Hello World!");

    Ok(())
}

#[tokio::main]
async fn main() {
    let endpoints = warp::get().and(warp::path("hello")).map(|| {
        // Call the hello function, and then convert its result into something that implements warp::reply::Reply
        match hello() {
            Ok(()) => warp::http::StatusCode::OK,
            Err(err) => {
                eprintln!("Got error: {}", err);
                warp::http::StatusCode::INTERNAL_SERVER_ERROR
            }
        }
    });

    warp::serve(endpoints).run(([127, 0, 0, 1], 3030)).await;
}

This builds and runs correctly for me

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.