You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm learning Rust and was doing a project with Poem while implementing the Hexagonal Architecture Pattern.
From what I understand in Hexagonal Architecture, I should try to create a "service" that encapsulates a group functions which are called to wrappers around 3rd party libraries (adapters) through a standardized way via traits (ports).
That requires me to include an AppState that contains the "service" with a trait like so:
pubstructAppState<M:Meta>{pubmeta:Arc<M>,}
The trait is as follows:
pubtraitMeta:Clone + Send + Sync + 'static{/// Get application metadata, such as version info, uptime, etc.asyncfnget_app_data(&self) -> Config;// Where Config is a struct with key value string pairs}
I have a function called start_service and I'm able to pass the an AppState as follows:
asyncfnstart_server(&self,config:&Webserver,meta:&implMeta,) -> Result<(), std::io::Error>{
...
let app_state = AppState{meta:Arc::new(meta.clone()),};// Create Swagger UIlet ui = main_paths.swagger_ui();// Create routeslet routes = Route::new().nest("/", main_paths).nest(format!("{}/docs",&base_path), ui).data(app_state.clone());// Passing the app_state here compiles without issues
...}
However, on the OpenAPI handler end I'm unable to retrieve that app_state:
pubstructMetaHandler;/// API responses for Meta endpoints#[derive(ApiResponse)]enumMetaResponses{/// Returns information on application configuration in JSON.#[oai(status = 200)]Ok(Json<Value>),}#[OpenApi(prefix_path = "/meta", tag = "OperationalTags::Meta")]implMetaHandler{#[oai(path = "/info", method = "get", hidden = false)]asyncfninfo<M>(&self,Data(app_state):Data<&AppState<M>>) -> MetaResponseswhereM:Meta,{// Return the configuration with app_state as JSONMetaResponses::Ok(Json(serde_json::json!(// Access the inner Config reference
app_state.meta.get_app_data())))}}
I get error[E0412]: cannot find type 'M' in this scope
async fn info<M>(&self, Data(app_state): Data<&AppState<M>>) -> MetaResponses
^ not found in this scope
If I take my info function and move it outside of #[OpenApi] and remove the &self. it seems that the function syntax for referencing the Meta trait is correct.
For example, this will compile without errors:
asyncfninfo<M>(Data(_app_state):Data<&AppState<M>>) -> MetaResponseswhereM:Meta,{// Return the configuration with app_state as JSONMetaResponses::Ok(Json(serde_json::json!(// Access the inner Config reference
app_state.meta.get_app_data())))}
Is this due to some limitation with using Data in Poem? If so, are there any workarounds?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm learning Rust and was doing a project with Poem while implementing the Hexagonal Architecture Pattern.
From what I understand in Hexagonal Architecture, I should try to create a "service" that encapsulates a group functions which are called to wrappers around 3rd party libraries (adapters) through a standardized way via traits (ports).
That requires me to include an AppState that contains the "service" with a trait like so:
The trait is as follows:
I have a function called
start_serviceand I'm able to pass the an AppState as follows:However, on the OpenAPI handler end I'm unable to retrieve that
app_state:I get
error[E0412]: cannot find type 'M' in this scopeIf I take my info function and move it outside of
#[OpenApi]and remove the&self. it seems that the function syntax for referencing theMetatrait is correct.For example, this will compile without errors:
Is this due to some limitation with using
Datain Poem? If so, are there any workarounds?Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions