Skip to content

Commit

Permalink
Merge pull request #35 from Jurshsmith/validate-config
Browse files Browse the repository at this point in the history
Add simple validations for config
  • Loading branch information
Jurshsmith authored Oct 20, 2023
2 parents 725b6d8 + 555c062 commit 128a33b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
34 changes: 34 additions & 0 deletions chaindexing/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,37 @@ impl Config {
self
}
}

pub enum ConfigError {
NoContract,
NoChain,
}

impl std::fmt::Debug for ConfigError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConfigError::NoContract => {
write!(f, "At least one contract is required")
}
ConfigError::NoChain => {
write!(f, "At least one chain is required")
}
}
}
}

pub trait ValidatableConfig {
fn validate(&self) -> Result<(), ConfigError>;
}

impl ValidatableConfig for Config {
fn validate(&self) -> Result<(), ConfigError> {
if self.contracts.is_empty() {
Err(ConfigError::NoContract)
} else if self.chains.is_empty() {
Err(ConfigError::NoChain)
} else {
Ok(())
}
}
}
26 changes: 24 additions & 2 deletions chaindexing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod reset_counts;
pub use chain_reorg::{MinConfirmationCount, ReorgedBlock, ReorgedBlocks, UnsavedReorgedBlock};
pub use chains::Chains;
pub use config::Config;
use config::{ConfigError, ValidatableConfig};
pub use contract_states::{ContractState, ContractStateMigrations, ContractStates};
pub use contracts::{Contract, ContractAddress, ContractEvent, Contracts};
pub use diesel;
Expand Down Expand Up @@ -48,17 +49,38 @@ pub type ChaindexingRepoRawQueryTxnClient<'a> = PostgresRepoRawQueryTxnClient<'a
#[cfg(feature = "postgres")]
pub use repos::PostgresRepoAsyncConnection as ChaindexingRepoAsyncConnection;

pub enum ChaindexingError {
Config(ConfigError),
}

impl From<ConfigError> for ChaindexingError {
fn from(value: ConfigError) -> Self {
ChaindexingError::Config(value)
}
}

impl std::fmt::Debug for ChaindexingError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ChaindexingError::Config(config_error) => {
write!(f, "Config Error: {:?}", config_error)
}
}
}
}

pub struct Chaindexing;

impl Chaindexing {
pub async fn index_states(config: &Config) -> Result<(), ()> {
pub async fn index_states(config: &Config) -> Result<(), ChaindexingError> {
config.validate()?;
Self::setup(config).await?;
EventsIngester::start(config);
EventHandlers::start(config);
Ok(())
}

pub async fn setup(config: &Config) -> Result<(), ()> {
pub async fn setup(config: &Config) -> Result<(), ChaindexingError> {
let Config {
repo,
contracts,
Expand Down

0 comments on commit 128a33b

Please sign in to comment.