Skip to content

Commit

Permalink
fix(axum): Expose IntrospectionConfig to facilitate creating Axum s…
Browse files Browse the repository at this point in the history
…ubstates (#523)

# Problem 
Can't inject other dependencies while using `IntrospectionState` as the
only state in Axum. Embedding `IntrospectionState` in an `AppState`
object that includes other dependencies renders `IntrospectedUser`
unusable since it would require an implementation for `FromRef<AppState>
for IntrospectionConfig` which in its turn not possible due to
`IntrospectionConfig` inside `IntrospectionState` not being public/does
not have a public getter.

# Solution 
Since `IntrospectionState` is built using `IntrospectionStateBuilder` it
doesn't make sense to expose the field directly. A getter makes more
sense :)

# Usage Example 
```rust
struct AppState {
    some_other_dep: String,
    introspection_state: IntrospectionState,
}

impl FromRef<AppState> for IntrospectionConfig {
    fn from_ref(input: &AppState) -> Self {
        input.introspection_state.config.clone()
    }
}
```
  • Loading branch information
jamil7 committed Feb 7, 2024
1 parent 96c9333 commit f1133ad
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/axum/introspection/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ pub struct IntrospectionState {
pub(crate) config: IntrospectionConfig,
}

impl IntrospectionState {
pub fn config(&self) -> &IntrospectionConfig {
&self.config
}
}

/// Configuration that must be inject into the axum application state. Used by the
/// [IntrospectionStateBuilder](super::IntrospectionStateBuilder). This struct is also used to create the [IntrospectionState](IntrospectionState)
#[derive(Debug, Clone)]
Expand Down

0 comments on commit f1133ad

Please sign in to comment.