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

Simplify API and use "state" to improve type-safety #27

Merged
merged 6 commits into from
Nov 2, 2023
Merged

Conversation

connec
Copy link
Member

@connec connec commented Nov 2, 2023

The upshot of this PR is that:

  • Tx is the only type that needs to be handled directly (i.e. used or otherwise named).
  • Tx's FromRequestParts implementation requires axum_sqlx_tx::State, which goes someway to making it harder to forget to supply the extractor's dependencies (it doesn't prevent compilation when Layer is forgotten, but the setup methods return (State, Layer) making it harder to miss).

  • 0927aab chore: doc typo

  • c9eccaf feat!: introduce State to make Tx more type-safe

    Tx now implements FromRequestParts<State>, meaning the Router must
    be provided an instance of State in order for it to compile.

    State can be constructed with Layer::new, which now returns a tuple
    of Layer and State. The idea behind constructing both in the same
    function is to make it harder to forget to add the Layer, however
    Layer::new is perhaps not the best place for it long-term.

    Ultimately this has the desired effect of making Tx more type-safe -
    applications that attempt to use Tx without providing State won't
    compile, and the API for obtaining State makes it harder to forget to
    add the Layer.

    BREAKING CHANGE: Layer::new now returns a (Layer, State) tuple. This
    can be consumed easily by destructuring assignment. Tx now requires
    State to be provided on Routers in order for the Router to be
    usable.

  • 32a0a0f feat: introduce Tx::setup, Tx::config, and Config APIs

    This will centralise the configuration API and reduce the number of
    types that need to interacted with directly (i.e. used) down to just
    Tx.

  • e8f694e refactor!: remove Layer::new and Layer::new_with_error

    This leaves Tx::{setup,config} as the only entrypoints to the API.

    Error handling documentation was also rewritten.

    BREAKING CHANGE: Layer::{new,new_with_error} have been removed. Use
    Tx::{setup,config} instead.

  • e2701a7 refactor!: rationalise generic error type bounds

    Required bounds are now present on more impl blocks to provide better
    diagnostics if error types are missing required traits.

    The Layer error type is now only required to implement
    From<sqlx::Error> (technically sqlx::Error is required to implement
    Into<LayerError> since this is more flexible).

    BREAKING CHANGE: The tighter bounds on impl blocks shouldn't break
    already working code, however the change to the Layer error type
    bounds will break error overrides that implement
    From<axum_sqlx_tx::Error> - this must be changed to
    From<sqlx::Error>.

  • e896b5a chore: doc fix

`Tx` now implements `FromRequestParts<State>`, meaning the `Router` must
be provided an instance of `State` in order for it to compile.

`State` can be constructed with `Layer::new`, which now returns a tuple
of `Layer` and `State`. The idea behind constructing both in the same
function is to make it harder to forget to add the `Layer`, however
`Layer::new` is perhaps not the best place for it long-term.

Ultimately this has the desired effect of making `Tx` more type-safe -
applications that attempt to use `Tx` without providing `State` won't
compile, and the API for obtaining `State` makes it harder to forget to
add the `Layer`.

BREAKING CHANGE: `Layer::new` now returns a `(Layer, State)` tuple. This
can be consumed easily by destructuring assignment. `Tx` now requires
`State` to be provided on `Router`s in order for the `Router` to be
usable.
This will centralise the configuration API and reduce the number of
types that need to interacted with directly (i.e. `use`d) down to just
`Tx`.
This leaves `Tx::{setup,config}` as the only entrypoints to the API.

Error handling documentation was also rewritten.

BREAKING CHANGE: `Layer::{new,new_with_error}` have been removed. Use
`Tx::{setup,config}` instead.
Required bounds are now present on more `impl` blocks to provide better
diagnostics if error types are missing required traits.

The `Layer` error type is now only required to implement
`From<sqlx::Error>` (technically `sqlx::Error` is required to implement
`Into<LayerError>` since this is more flexible).

BREAKING CHANGE: The tighter bounds on `impl` blocks shouldn't break
already working code, however the change to the `Layer` error type
bounds will break error overrides that implement
`From<axum_sqlx_tx::Error>` - this must be changed to
`From<sqlx::Error>`.
@connec connec marked this pull request as ready for review November 2, 2023 17:32
@connec connec merged commit 1940658 into master Nov 2, 2023
19 checks passed
@connec connec deleted the state branch November 2, 2023 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant