gotham-rs / gotham

A flexible web framework that promotes stability, safety, security and speed.
https://gotham.rs
Other
2.24k stars 125 forks source link

Query string example (introduction) is incorrect #561

Closed jly36963 closed 3 years ago

jly36963 commented 3 years ago

Issue

I believe the query string example works for

gotham = { path = "../../../gotham" }

but doesn't work for

gotham = "0.6.0"

Environment

rustup 1.24.3 (ce5817a94 2021-05-31) rustc 1.57.0-nightly (308dffd25 2021-09-22) cargo 1.57.0-nightly (9a28ac83c 2021-09-18)

Code

I started learning rust a few weeks ago, and I'm trying to learn Gotham. I have example code here

I commented out the route handler and struct that use the query string because the linter/compiler had a lot of issues with those code blocks.

When I run the code without those blocks commented out, the linter & compiler complain about my usage of StateData, StaticResponseExtender, take::from, and with_query_string_extractor.

(Side note: I had to import StaticResponseExtender with use gotham::router::response::extender::StaticResponseExtender; which differs from the example)

error: cannot find derive macro `StateData` in this scope
   --> src/main.rs:136:23
    |
136 | #[derive(Deserialize, StateData, StaticResponseExtender)]
    |                       ^^^^^^^^^
    |
note: `StateData` is imported here, but it is only a trait, without a derive macro
   --> src/main.rs:8:39
    |
8   | use gotham::state::{FromState, State, StateData};
    |                                       ^^^^^^^^^

error: cannot find derive macro `StaticResponseExtender` in this scope
   --> src/main.rs:136:34
    |
136 | #[derive(Deserialize, StateData, StaticResponseExtender)]
    |                                  ^^^^^^^^^^^^^^^^^^^^^^
    |
note: `StaticResponseExtender` is imported here, but it is only a trait, without a derive macro
   --> src/main.rs:6:5
    |
6   | use gotham::router::response::extender::StaticResponseExtender;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0277]: the trait bound `StoreSearchQuery: StaticResponseExtender` is not satisfied
  --> src/main.rs:35:18
   |
35 |                 .with_query_string_extractor::<StoreSearchQuery>()
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StaticResponseExtender` is not implemented for `StoreSearchQuery`
   |
   = note: required because of the requirements on the impl of `QueryStringExtractor<Body>` for `StoreSearchQuery`
   = note: required because of the requirements on the impl of `ReplaceQueryStringExtractor<StoreSearchQuery>` for `SingleRouteBuilder<'_, MethodOnlyRouteMatcher, (), (), NoopPathExtractor, NoopQueryStringExtractor>`

error[E0277]: the trait bound `StoreSearchQuery: StateData` is not satisfied
  --> src/main.rs:35:18
   |
35 |                 .with_query_string_extractor::<StoreSearchQuery>()
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StateData` is not implemented for `StoreSearchQuery`
   |
   = note: required because of the requirements on the impl of `QueryStringExtractor<Body>` for `StoreSearchQuery`
   = note: required because of the requirements on the impl of `ReplaceQueryStringExtractor<StoreSearchQuery>` for `SingleRouteBuilder<'_, MethodOnlyRouteMatcher, (), (), NoopPathExtractor, NoopQueryStringExtractor>`

error[E0599]: the method `to` exists for struct `SingleRouteBuilder<'_, MethodOnlyRouteMatcher, (), (), NoopPathExtractor, StoreSearchQuery>`, but its trait bounds were not satisfied
   --> src/main.rs:36:18
    |
36  |                   .to(get_api_store_search);
    |                    ^^ method cannot be called on `SingleRouteBuilder<'_, MethodOnlyRouteMatcher, (), (), NoopPathExtractor, StoreSearchQuery>` due to unsatisfied trait bounds
...
137 |   struct StoreSearchQuery {
    |   ----------------------- doesn't satisfy `StoreSearchQuery: QueryStringExtractor<Body>`
    |
   ::: /Users/jly/.cargo/registry/src/github.com-1ecc6299db9ec823/gotham-0.6.0/src/router/builder/mod.rs:290:1
    |
290 | / pub struct SingleRouteBuilder<'a, M, C, P, PE, QSE>
291 | | where
292 | |     M: RouteMatcher + Send + Sync + 'static,
293 | |     C: PipelineHandleChain<P> + Send + Sync + 'static,
...   |
302 | |     phantom: PhantomData<(PE, QSE)>,
303 | | }
    | |_- doesn't satisfy `_: gotham::router::builder::DefineSingleRoute`
    |
    = note: the following trait bounds were not satisfied:
            `StoreSearchQuery: QueryStringExtractor<Body>`
            which is required by `SingleRouteBuilder<'_, MethodOnlyRouteMatcher, (), (), NoopPathExtractor, StoreSearchQuery>: gotham::router::builder::DefineSingleRoute`

error[E0599]: the function or associated item `take_from` exists for struct `StoreSearchQuery`, but its trait bounds were not satisfied
   --> src/main.rs:96:35
    |
96  |     let query = StoreSearchQuery::take_from(&mut state);
    |                                   ^^^^^^^^^ function or associated item cannot be called on `StoreSearchQuery` due to unsatisfied trait bounds
...
137 | struct StoreSearchQuery {
    | -----------------------
    | |
    | function or associated item `take_from` not found for this
    | doesn't satisfy `StoreSearchQuery: FromState`
    | doesn't satisfy `StoreSearchQuery: StateData`
    |
    = note: the following trait bounds were not satisfied:
            `StoreSearchQuery: StateData`
            which is required by `StoreSearchQuery: FromState`
            `&StoreSearchQuery: StateData`
            which is required by `&StoreSearchQuery: FromState`
            `&mut StoreSearchQuery: StateData`
            which is required by `&mut StoreSearchQuery: FromState`

Am I messing something up?

msrd0 commented 3 years ago

Hi @jly36963. I'm sorry to hear that this is confusing you but actually this is intentional. Example code on the master branch uses the master branch version of gotham. The master branch might contain breaking changes that will appear in the next release (keeping semantic versioning in mind of course). If you want example code for the latest release, use the example code at the time of release:

https://github.com/gotham-rs/gotham/tree/gotham-0.6.0/examples/query_string/introduction

This could probably do with better documentation, which is tracked in #400 already.

jly36963 commented 3 years ago

Perfect, thank you