From 3220086a4f50e3b2682c72d21819e9376b7430ea Mon Sep 17 00:00:00 2001 From: findolor Date: Mon, 16 Dec 2024 15:31:23 +0300 Subject: [PATCH 1/2] implement sentry logic and tests --- crates/settings/src/lib.rs | 1 + crates/settings/src/sentry.rs | 24 ++++++++++++++++++++++++ crates/settings/src/yaml/mod.rs | 6 ++++++ crates/settings/src/yaml/orderbook.rs | 11 +++++++++-- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 crates/settings/src/sentry.rs diff --git a/crates/settings/src/lib.rs b/crates/settings/src/lib.rs index 61d78260a..cd0dc6f36 100644 --- a/crates/settings/src/lib.rs +++ b/crates/settings/src/lib.rs @@ -13,6 +13,7 @@ pub mod orderbook; pub mod plot_source; pub mod remote; pub mod scenario; +pub mod sentry; pub mod subgraph; pub mod token; pub mod unit_test; diff --git a/crates/settings/src/sentry.rs b/crates/settings/src/sentry.rs new file mode 100644 index 000000000..0556918db --- /dev/null +++ b/crates/settings/src/sentry.rs @@ -0,0 +1,24 @@ +use crate::yaml::{optional_string, YamlError, YamlParsableString}; +use std::sync::{Arc, RwLock}; +use strict_yaml_rust::StrictYaml; + +#[derive(Clone, Debug)] +pub struct YamlSentry; + +impl YamlParsableString for YamlSentry { + fn parse_from_yaml(_: Arc>) -> Result { + Err(YamlError::InvalidTraitFunction) + } + + fn parse_from_yaml_optional( + document: Arc>, + ) -> Result, YamlError> { + let document_read = document.read().map_err(|_| YamlError::ReadLockError)?; + + if let Some(value) = optional_string(&document_read, "sentry") { + Ok(Some(value)) + } else { + Ok(None) + } + } +} diff --git a/crates/settings/src/yaml/mod.rs b/crates/settings/src/yaml/mod.rs index e2cf8c247..0fb9d9f22 100644 --- a/crates/settings/src/yaml/mod.rs +++ b/crates/settings/src/yaml/mod.rs @@ -33,6 +33,10 @@ pub trait YamlParsableVector: Sized { pub trait YamlParsableString { fn parse_from_yaml(document: Arc>) -> Result; + + fn parse_from_yaml_optional( + document: Arc>, + ) -> Result, YamlError>; } #[derive(Debug, Error)] @@ -61,6 +65,8 @@ pub enum YamlError { ReadLockError, #[error("Document write lock error")] WriteLockError, + #[error("Invalid trait function")] + InvalidTraitFunction, #[error(transparent)] ParseNetworkConfigSourceError(#[from] ParseNetworkConfigSourceError), #[error(transparent)] diff --git a/crates/settings/src/yaml/orderbook.rs b/crates/settings/src/yaml/orderbook.rs index 1f1010a1f..add7b6f53 100644 --- a/crates/settings/src/yaml/orderbook.rs +++ b/crates/settings/src/yaml/orderbook.rs @@ -1,7 +1,7 @@ use super::*; use crate::{ - metaboard::YamlMetaboard, subgraph::YamlSubgraph, Deployer, Metaboard, Network, Orderbook, - Subgraph, Token, + metaboard::YamlMetaboard, sentry::YamlSentry, subgraph::YamlSubgraph, Deployer, Metaboard, + Network, Orderbook, Subgraph, Token, }; use std::sync::{Arc, RwLock}; use strict_yaml_rust::StrictYamlEmitter; @@ -83,6 +83,11 @@ impl OrderbookYaml { pub fn get_deployer(&self, key: &str) -> Result { Deployer::parse_from_yaml(self.document.clone(), key) } + + pub fn get_sentry(&self) -> Result { + let value = YamlSentry::parse_from_yaml_optional(self.document.clone())?; + Ok(value.map_or(false, |v| v == "true")) + } } #[cfg(test)] @@ -213,6 +218,8 @@ mod tests { ); assert_eq!(deployer.network, network.into()); assert_eq!(deployer.label, Some("Main Deployer".to_string())); + + assert!(ob_yaml.get_sentry().unwrap()); } #[test] From e768197da5df7b1c50d282053b5beca5eed8e0f9 Mon Sep 17 00:00:00 2001 From: findolor Date: Tue, 17 Dec 2024 14:00:24 +0300 Subject: [PATCH 2/2] update sentry struct name --- crates/settings/src/sentry.rs | 4 ++-- crates/settings/src/yaml/orderbook.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/settings/src/sentry.rs b/crates/settings/src/sentry.rs index 0556918db..bf068957c 100644 --- a/crates/settings/src/sentry.rs +++ b/crates/settings/src/sentry.rs @@ -3,9 +3,9 @@ use std::sync::{Arc, RwLock}; use strict_yaml_rust::StrictYaml; #[derive(Clone, Debug)] -pub struct YamlSentry; +pub struct Sentry; -impl YamlParsableString for YamlSentry { +impl YamlParsableString for Sentry { fn parse_from_yaml(_: Arc>) -> Result { Err(YamlError::InvalidTraitFunction) } diff --git a/crates/settings/src/yaml/orderbook.rs b/crates/settings/src/yaml/orderbook.rs index add7b6f53..a5539e1ad 100644 --- a/crates/settings/src/yaml/orderbook.rs +++ b/crates/settings/src/yaml/orderbook.rs @@ -1,7 +1,7 @@ use super::*; use crate::{ - metaboard::YamlMetaboard, sentry::YamlSentry, subgraph::YamlSubgraph, Deployer, Metaboard, - Network, Orderbook, Subgraph, Token, + metaboard::YamlMetaboard, sentry::Sentry, subgraph::YamlSubgraph, Deployer, Metaboard, Network, + Orderbook, Subgraph, Token, }; use std::sync::{Arc, RwLock}; use strict_yaml_rust::StrictYamlEmitter; @@ -85,7 +85,7 @@ impl OrderbookYaml { } pub fn get_sentry(&self) -> Result { - let value = YamlSentry::parse_from_yaml_optional(self.document.clone())?; + let value = Sentry::parse_from_yaml_optional(self.document.clone())?; Ok(value.map_or(false, |v| v == "true")) } }