Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macros to help using warp #395

Open
kdy1 opened this issue Jan 19, 2020 · 13 comments
Open

macros to help using warp #395

kdy1 opened this issue Jan 19, 2020 · 13 comments
Labels
rfc Extra attention is needed

Comments

@kdy1
Copy link

kdy1 commented Jan 19, 2020

I wrote macros to ease using warp, and I'm considering merging macros back into warp.

See rweb/tests and rweb/examples to see usage examples.

@carllerche
Copy link

This is pretty cool. I didn’t see any examples of using the macros and passing around application state. Do you have one?

@kdy1
Copy link
Author

kdy1 commented Jan 19, 2020

@carllerche I'm sorry, but I'm not sure what did you mean by state. (I'm not English native)
Did you mean something like a db in

warp/examples/todos.rs

Lines 39 to 46 in 2823891

pub fn todos(
db: Db,
) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
todos_list(db.clone())
.or(todos_create(db.clone()))
.or(todos_update(db.clone()))
.or(todos_delete(db))
}

or something like hb in

.map(|| WithTemplate {
name: "template.html",
value: json!({"user" : "Warp"}),
})

If you meant the latter, it's implemented as #[filter = "foo_path"].

Anyway, I'm going to add #[data] which allows passing an argument from caller to actual handler.

@kdy1
Copy link
Author

kdy1 commented Jan 20, 2020

I've implemented #[data], which allows passing datas down. I'll update tests and examples

bors bot pushed a commit to kdy1/rweb that referenced this issue Jan 21, 2020
# Macros

 - [x] `#[router]` improvement
   - [x] use fn instead of unit struct
   - [x] #[data]
 - [x] #[body_size(max = "8192")]
 - [x] #[cors]
 - [x] header guard

```rust
#[get("/")]
#[header("X-Appengine-Cron", "1")]
fn gae_cron() -> String {
    String::new()
}
```

 - <del> query guard </del>:It's easy to do so with `#[filter]`

 - [x] `#[cookie = "foo"]` (on an argument)
 - <del>`#[ws]` (on an argument)</del>: Implemented with `FromRequest`

 - <del>`#[multipart]` (on an argument)</del>: Implemented with `FromRequest`
 

# Enhancement

 - [x] Factory pattern
We can replace `#[json]`, `#[body]` with automatic detection based on a trait, but it requires nightly **or** dynamic dispatch.



```rust
/// One box is created per function which accepts Json<T>
#[post("/typed")]
pub fn typed(body: Json<LoginForm>) {}

/// Everything is statically dispatched
#[post("/annotated")]
pub fn annotated(#[json] body: LoginForm) {}

```


Related: seanmonstar/warp#395
@seanmonstar
Copy link
Owner

This is really cool! It sounds similar to what Carl wanted to do with tower-web. I've always wanted warp to provide its FP-style Filter API, but some people really like the attribute style.

We could add a warp-macros crate that people can add if they prefer that style...

@jxs
Copy link
Collaborator

jxs commented Jan 24, 2020

hi @kdy1 , have you seen tower-web? I think what @carllerche meant was something like tower-web's impl_web! macro where you would define a struct to agregate state across handlers, which would then ored and passed to warp::serve

@davidbarsky
Copy link

I would absolutely love to have the macros in rweb in warp. A warp-macros crate would be excellent.

@seanmonstar seanmonstar added the rfc Extra attention is needed label Jan 24, 2020
@kdy1
Copy link
Author

kdy1 commented Jan 24, 2020

I added automatic openapi spec genertion to rweb, and I'm not sure if it would be useful for warp. As macro removes attributes, there's no way to make openapi related code act as 3rd-party library. I mean, openapi annotation should be handled by macros like #[get("/")].

Anyway, I want to hear more opinion about it.

@kdy1
Copy link
Author

kdy1 commented Jan 24, 2020

hi @kdy1 , have you seen tower-web? I think what @carllerche meant was something like tower-web's impl_web! macro where you would define a struct to agregate state across handlers, which would then ored and passed to warp::serve

I were not aware of it. But rweb supports it via #[data]. You can pass down e.g. db struct to handlers.

@kdy1
Copy link
Author

kdy1 commented Jan 24, 2020

@seanmonstar What's your opinion about openapi spec generator? Do you think it can be feature of warp?

If not, I should redesign rweb-macros to be usable with warp.

@thomaseizinger
Copy link
Contributor

I was looking to write something like this!

Would be very cool if we could make a warp-macros crate happen. The filter-style API provides a lot of flexibility but also comes at the cost of some boilerplate.

The other reason why I am very interested in this is more ergonomic error handling. Concretely, I would like to use anyhow by just using ? in route handlers. Given the controversy of directly adopting anyhow, maybe a macro approach like rweb could provide this support?

@Txuritan
Copy link

I took a shot at making my own version of warp-macros based off rweb's.

I did both for fun and for the fact that I didn't want to drag in more dependencies, so it just depends on proc-macro2, syn and quote.

@szagi3891
Copy link

szagi3891 commented Jan 16, 2021

@seanmonstar - Maybe it is worth resurrecting this topic ? This "rweb" library is fantastic.

I use warp. But for a long time I had to deal with the fact that it does not support openapi. It was only by accident that I found rweb.

Maybe it is worth to give a link in warp documentation to rweb library for a start ?

@Type1J
Copy link

Type1J commented Feb 1, 2021

I find the OpenAPI generation more valuable than the macro boilerplate generation, but I can see its value, too. These should be seperable! The rweb library isn't able to document correctly if path Filters are used. It's really not able to document unless the macros can tell it what it needs to know, but Filters should be what's telling the OpenAPI spec generator everything. If that were the case, then these concerns would be seperable. I don't think that this is a case where the external library (rweb) can just NewType the relevant filters: this should be something from the Warp Filter trait.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rfc Extra attention is needed
Projects
None yet
Development

No branches or pull requests

9 participants