Skip to content

Commit

Permalink
Factor out ServerState into a new file.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirTalwar committed Aug 22, 2024
1 parent 31cc94f commit aab7f39
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
32 changes: 1 addition & 31 deletions crates/sdk/src/default_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::connector::{Connector, ConnectorSetup, ErrorResponse, Result};
use crate::fetch_metrics::fetch_metrics;
use crate::json_rejection::JsonRejection;
use crate::json_response::JsonResponse;
use crate::state::ServerState;
use crate::tracing::{init_tracing, make_span, on_response};

#[derive(Parser)]
Expand Down Expand Up @@ -138,37 +139,6 @@ struct CheckHealthCommand {

type Port = u16;

#[derive(Debug)]
pub struct ServerState<C: Connector> {
configuration: C::Configuration,
state: C::State,
metrics: Registry,
}

impl<C: Connector> Clone for ServerState<C>
where
C::Configuration: Clone,
C::State: Clone,
{
fn clone(&self) -> Self {
Self {
configuration: self.configuration.clone(),
state: self.state.clone(),
metrics: self.metrics.clone(),
}
}
}

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
1 change: 1 addition & 0 deletions crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod default_main;
pub mod fetch_metrics;
pub mod json_rejection;
pub mod json_response;
pub mod state;
pub mod tracing;

pub use ndc_models as models;
36 changes: 36 additions & 0 deletions crates/sdk/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use crate::connector::Connector;

#[derive(Debug)]
pub struct ServerState<C: Connector> {
pub configuration: C::Configuration,
pub state: C::State,
pub metrics: prometheus::Registry,
}

impl<C: Connector> Clone for ServerState<C>
where
C::Configuration: Clone,
C::State: Clone,
{
fn clone(&self) -> Self {
Self {
configuration: self.configuration.clone(),
state: self.state.clone(),
metrics: self.metrics.clone(),
}
}
}

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

0 comments on commit aab7f39

Please sign in to comment.