Skip to content

Commit

Permalink
Add a public constructor for ServerState.
Browse files Browse the repository at this point in the history
I would like this for testing.

I also removed some `.unwrap()` calls for fun.
  • Loading branch information
SamirTalwar committed Feb 26, 2024
1 parent b81fc0b commit 0608dcf
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions rust-connector-sdk/src/default_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ where
}
}

impl<C: Connector> ServerState<C> {
pub fn new(configuration: C::Configuration, state: C::State, metrics: Registry) -> Self {
Self {
configuration,
state,
metrics,
}
}
}

/// A default main function for a connector.
///
/// The intent is that this function can replace your `main` function
Expand Down Expand Up @@ -173,7 +183,7 @@ where
init_tracing(&serve_command.service_name, &serve_command.otlp_endpoint)
.expect("Unable to initialize tracing");

let server_state = init_server_state::<C>(serve_command.configuration).await;
let server_state = init_server_state::<C>(serve_command.configuration).await?;

let router = create_router::<C>(
server_state.clone(),
Expand Down Expand Up @@ -230,19 +240,11 @@ where
/// Initialize the server state from the configuration file.
pub async fn init_server_state<C: Connector>(
config_directory: impl AsRef<Path> + Send,
) -> ServerState<C> {
let configuration = C::parse_configuration(config_directory).await.unwrap();

) -> Result<ServerState<C>, Box<dyn Error + Send + Sync>> {
let mut metrics = Registry::new();
let state = C::try_init_state(&configuration, &mut metrics)
.await
.unwrap();

ServerState::<C> {
configuration,
state,
metrics,
}
let configuration = C::parse_configuration(config_directory).await?;
let state = C::try_init_state(&configuration, &mut metrics).await?;
Ok(ServerState::new(configuration, state, metrics))
}

pub fn create_router<C: Connector + 'static>(
Expand Down Expand Up @@ -516,7 +518,7 @@ async fn test<C: Connector>(command: TestCommand) -> Result<(), Box<dyn Error +
snapshots_dir: command.snapshots_dir,
};

let connector = make_connector_adapter::<C>(command.configuration).await;
let connector = make_connector_adapter::<C>(command.configuration).await?;
let results = ndc_test::test_connector(&test_configuration, &connector).await;

if !results.failures.is_empty() {
Expand All @@ -530,7 +532,7 @@ async fn test<C: Connector>(command: TestCommand) -> Result<(), Box<dyn Error +
}

async fn replay<C: Connector>(command: ReplayCommand) -> Result<(), Box<dyn Error + Send + Sync>> {
let connector = make_connector_adapter::<C>(command.configuration).await;
let connector = make_connector_adapter::<C>(command.configuration).await?;
let results = ndc_test::test_snapshots_in_directory(&connector, command.snapshots_dir).await;

if !results.failures.is_empty() {
Expand All @@ -543,18 +545,16 @@ async fn replay<C: Connector>(command: ReplayCommand) -> Result<(), Box<dyn Erro
Ok(())
}

async fn make_connector_adapter<C: Connector>(configuration_path: PathBuf) -> ConnectorAdapter<C> {
let configuration = C::parse_configuration(configuration_path).await.unwrap();

async fn make_connector_adapter<C: Connector>(
configuration_path: PathBuf,
) -> Result<ConnectorAdapter<C>, Box<dyn Error + Send + Sync>> {
let mut metrics = Registry::new();
let state = C::try_init_state(&configuration, &mut metrics)
.await
.unwrap();

ConnectorAdapter::<C> {
let configuration = C::parse_configuration(configuration_path).await?;
let state = C::try_init_state(&configuration, &mut metrics).await?;
Ok(ConnectorAdapter {
configuration,
state,
}
})
}

async fn check_health(
Expand Down

0 comments on commit 0608dcf

Please sign in to comment.