|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use std::{fmt::Display, path::PathBuf, sync::Arc}; |
| 3 | +use thiserror::Error; |
| 4 | + |
| 5 | +use crate::{chain_observer::ChainObserver, CardanoNetwork, StdResult}; |
| 6 | + |
| 7 | +use super::{CardanoCliChainObserver, CardanoCliRunner, FakeObserver, PallasChainObserver}; |
| 8 | + |
| 9 | +/// Type of chain observers available |
| 10 | +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] |
| 11 | +#[serde(rename_all = "lowercase")] |
| 12 | +pub enum ChainObserverType { |
| 13 | + /// Cardano Cli chain observer. |
| 14 | + #[serde(rename = "cardano-cli")] |
| 15 | + CardanoCli, |
| 16 | + /// Pallas chain observer. |
| 17 | + Pallas, |
| 18 | + /// Fake chain observer. |
| 19 | + #[cfg(feature = "test_tools")] |
| 20 | + Fake, |
| 21 | +} |
| 22 | + |
| 23 | +impl Display for ChainObserverType { |
| 24 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 25 | + match self { |
| 26 | + Self::CardanoCli => write!(f, "cardano-cli"), |
| 27 | + Self::Pallas => write!(f, "pallas"), |
| 28 | + Self::Fake => write!(f, "fake"), |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Error type for chain observer builder service. |
| 34 | +#[derive(Error, Debug)] |
| 35 | +pub enum ChainObserverBuilderError { |
| 36 | + /// Missing cardano cli runner error. |
| 37 | + #[error("cardano cli runner is missing")] |
| 38 | + MissingCardanoCliRunner, |
| 39 | +} |
| 40 | + |
| 41 | +/// Chain observer builder |
| 42 | +pub struct ChainObserverBuilder { |
| 43 | + chain_observer_type: ChainObserverType, |
| 44 | + cardano_node_socket_path: PathBuf, |
| 45 | + cardano_network: CardanoNetwork, |
| 46 | + cardano_cli_runner: Option<Box<CardanoCliRunner>>, |
| 47 | +} |
| 48 | + |
| 49 | +impl ChainObserverBuilder { |
| 50 | + /// Chain observer builder factory |
| 51 | + pub fn new( |
| 52 | + chain_observer_type: &ChainObserverType, |
| 53 | + cardano_node_socket_path: &PathBuf, |
| 54 | + cardano_node_network: &CardanoNetwork, |
| 55 | + cardano_cli_runner: Option<&CardanoCliRunner>, |
| 56 | + ) -> Self { |
| 57 | + Self { |
| 58 | + chain_observer_type: chain_observer_type.to_owned(), |
| 59 | + cardano_node_socket_path: cardano_node_socket_path.to_owned(), |
| 60 | + cardano_network: cardano_node_network.to_owned(), |
| 61 | + cardano_cli_runner: cardano_cli_runner.map(|c| c.to_owned().into()), |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// Create chain observer |
| 66 | + pub fn build(&self) -> StdResult<Arc<dyn ChainObserver>> { |
| 67 | + match self.chain_observer_type { |
| 68 | + ChainObserverType::CardanoCli => Ok(Arc::new(CardanoCliChainObserver::new( |
| 69 | + self.cardano_cli_runner |
| 70 | + .as_ref() |
| 71 | + .ok_or(ChainObserverBuilderError::MissingCardanoCliRunner)? |
| 72 | + .to_owned(), |
| 73 | + ))), |
| 74 | + ChainObserverType::Pallas => { |
| 75 | + let fallback = CardanoCliChainObserver::new( |
| 76 | + self.cardano_cli_runner |
| 77 | + .as_ref() |
| 78 | + .ok_or(ChainObserverBuilderError::MissingCardanoCliRunner)? |
| 79 | + .to_owned(), |
| 80 | + ); |
| 81 | + let observer = PallasChainObserver::new( |
| 82 | + &self.cardano_node_socket_path, |
| 83 | + self.cardano_network, |
| 84 | + fallback, |
| 85 | + ); |
| 86 | + Ok(Arc::new(observer)) |
| 87 | + } |
| 88 | + #[cfg(feature = "test_tools")] |
| 89 | + ChainObserverType::Fake => Ok(Arc::new(FakeObserver::default())), |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments