diff --git a/rust-connector-sdk/src/connector.rs b/rust-connector-sdk/src/connector.rs index 3e41dfcd..4925c89f 100644 --- a/rust-connector-sdk/src/connector.rs +++ b/rust-connector-sdk/src/connector.rs @@ -1,4 +1,4 @@ -use std::{error::Error, path::PathBuf}; +use std::{error::Error, path::Path}; use async_trait::async_trait; use ndc_client::models; @@ -192,14 +192,14 @@ pub enum MutationError { #[async_trait] pub trait Connector { /// The type of validated configuration - type Configuration; + type Configuration: Sync + Send; /// The type of unserializable state - type State; + type State: Sync + Send; /// Validate the raw configuration provided by the user, /// returning a configuration error or a validated [`Connector::Configuration`]. async fn validate_raw_configuration( - configuration_dir: PathBuf, + configuration_dir: impl AsRef + Send, ) -> Result; /// Initialize the connector's in-memory state. diff --git a/rust-connector-sdk/src/connector/example.rs b/rust-connector-sdk/src/connector/example.rs index 8b0f2ce9..0dc81c92 100644 --- a/rust-connector-sdk/src/connector/example.rs +++ b/rust-connector-sdk/src/connector/example.rs @@ -1,5 +1,4 @@ use std::collections::BTreeMap; -use std::path::PathBuf; use async_trait::async_trait; use tracing::info_span; @@ -16,7 +15,7 @@ impl Connector for Example { type State = (); async fn validate_raw_configuration( - _configuration_dir: PathBuf, + _configuration_dir: impl AsRef + Send, ) -> Result { Ok(()) } diff --git a/rust-connector-sdk/src/default_main.rs b/rust-connector-sdk/src/default_main.rs index 22789c90..7734c0c5 100644 --- a/rust-connector-sdk/src/default_main.rs +++ b/rust-connector-sdk/src/default_main.rs @@ -8,14 +8,14 @@ use crate::{ routes, tracing::{init_tracing, make_span, on_response}, }; +use async_trait::async_trait; use axum_extra::extract::WithRejection; -use std::error::Error; +use std::{error::Error, path::Path}; use std::net; use std::path::PathBuf; use std::process::exit; -use async_trait::async_trait; use axum::{ body::Body, extract::State, @@ -31,7 +31,7 @@ use ndc_client::models::{ }; use ndc_test::report; use prometheus::Registry; -use serde::{de::DeserializeOwned, Serialize}; +use serde::Serialize; use tower_http::{trace::TraceLayer, validate_request::ValidateRequestHeaderLayer}; #[derive(Parser)] @@ -121,13 +121,27 @@ struct CheckHealthCommand { type Port = u16; -#[derive(Debug, Clone)] +#[derive(Debug)] pub struct ServerState { configuration: C::Configuration, state: C::State, metrics: Registry, } +impl Clone for ServerState +where + C::Configuration: Clone, + C::State: Clone, +{ + fn clone(&self) -> Self { + Self { + configuration: self.configuration.clone(), + state: self.state.clone(), + metrics: self.metrics.clone(), + } + } +} + /// A default main function for a connector. /// /// The intent is that this function can replace your `main` function @@ -151,11 +165,10 @@ pub struct ServerState { /// - It reads configuration as JSON from a file specified on the command line, /// - It reports traces to an OTLP collector specified on the command line, /// - Logs are written to stdout -pub async fn default_main( -) -> Result<(), Box> +pub async fn default_main() -> Result<(), Box> where - C::Configuration: Serialize + DeserializeOwned + Sync + Send + Clone, - C::State: Sync + Send + Clone, + C::Configuration: Clone + Serialize, + C::State: Clone, { let CliArgs { command } = CliArgs::parse(); @@ -167,11 +180,11 @@ where } } -async fn serve( +async fn serve( serve_command: ServeCommand, ) -> Result<(), Box> where - C::Configuration: Serialize + DeserializeOwned + Sync + Send + Clone, + C::Configuration: Serialize + Clone, C::State: Sync + Send + Clone, { init_tracing(&serve_command.service_name, &serve_command.otlp_endpoint) @@ -232,12 +245,9 @@ where } /// Initialize the server state from the configuration file. -pub async fn init_server_state( - config_directory: PathBuf, +pub async fn init_server_state( + config_directory: impl AsRef + Send, ) -> ServerState -where - C::Configuration: Serialize + DeserializeOwned + Sync + Send + Clone, - C::State: Sync + Send + Clone, { let configuration = C::validate_raw_configuration(config_directory) .await @@ -255,13 +265,13 @@ where } } -pub fn create_router( +pub fn create_router( state: ServerState, service_token_secret: Option, ) -> Router where - C::Configuration: Serialize + Clone + Sync + Send, - C::State: Sync + Send + Clone, + C::Configuration: Clone, + C::State: Clone, { let router = Router::new() .route("/capabilities", get(get_capabilities::)) @@ -335,13 +345,13 @@ where )) } -pub fn create_v2_router( +pub fn create_v2_router( state: ServerState, service_token_secret: Option, ) -> Router where - C::Configuration: Serialize + Clone + Sync + Send, - C::State: Sync + Send + Clone, + C::Configuration: Clone + Serialize, + C::State: Clone, { Router::new() .route("/schema", post(v2_compat::post_schema::)) @@ -466,11 +476,7 @@ struct ConnectorAdapter { } #[async_trait] -impl ndc_test::Connector for ConnectorAdapter -where - C::Configuration: Send + Sync + 'static, - C::State: Send + Sync + 'static, -{ +impl ndc_test::Connector for ConnectorAdapter { async fn get_capabilities( &self, ) -> Result { @@ -519,9 +525,6 @@ where async fn test( command: TestCommand, ) -> Result<(), Box> -where - C::Configuration: Sync + Send + 'static, - C::State: Send + Sync + 'static, { let test_configuration = ndc_test::TestConfiguration { seed: command.seed, @@ -544,9 +547,6 @@ where async fn replay( command: ReplayCommand, ) -> Result<(), Box> -where - C::Configuration: Sync + Send + 'static, - C::State: Send + Sync + 'static, { let connector = make_connector_adapter::(command.configuration).await; let results = ndc_test::test_snapshots_in_directory(&connector, command.snapshots_dir).await;