Skip to content

Commit

Permalink
refactor: split assertions
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <david.dalbusco@outlook.com>
  • Loading branch information
peterpeterparker committed Aug 20, 2024
1 parent 459e05c commit a57122e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 26 deletions.
25 changes: 25 additions & 0 deletions src/orbiter/src/assert/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::memory::STATE;
use crate::msg::ERROR_FEATURE_NOT_ENABLED;
use crate::types::state::SatelliteConfig;
use junobuild_shared::types::state::SatelliteId;

pub fn assert_enabled(satellite_id: &SatelliteId) -> Result<(), String> {
let config: Option<SatelliteConfig> = STATE.with(|state| {
let binding = state.borrow();
let config = binding.heap.config.get(satellite_id);

config.cloned()
});

// Enabling the analytics for a satellite is an opt-in feature
match config {
None => {}
Some(config) => {
if config.enabled {
return Ok(());
}
}
}

Err(ERROR_FEATURE_NOT_ENABLED.to_string())
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,13 @@ use crate::constants::{
KEY_MAX_LENGTH, LONG_STRING_MAX_LENGTH, METADATA_MAX_ELEMENTS, SHORT_STRING_MAX_LENGTH,
STRING_MAX_LENGTH,
};
use crate::memory::STATE;
use crate::msg::{ERROR_BOT_CALL, ERROR_FEATURE_NOT_ENABLED};
use crate::msg::ERROR_BOT_CALL;
use crate::types::interface::{SetPageView, SetTrackEvent};
use crate::types::state::{AnalyticKey, SatelliteConfig};
use crate::types::state::AnalyticKey;
use isbot::Bots;
use junobuild_shared::types::state::SatelliteId;
use junobuild_shared::utils::principal_not_equal;

pub fn assert_enabled(satellite_id: &SatelliteId) -> Result<(), String> {
let config: Option<SatelliteConfig> = STATE.with(|state| {
let binding = state.borrow();
let config = binding.heap.config.get(satellite_id);

config.cloned()
});

// Enabling the analytics for a satellite is an opt-in feature
match config {
None => {}
Some(config) => {
if config.enabled {
return Ok(());
}
}
}

Err(ERROR_FEATURE_NOT_ENABLED.to_string())
}

pub fn assert_analytic_key_length(key: &AnalyticKey) -> Result<(), String> {
if key.key.len() > KEY_MAX_LENGTH {
return Err(format!(
Expand Down
2 changes: 2 additions & 0 deletions src/orbiter/src/assert/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod config;
pub mod constraints;
2 changes: 1 addition & 1 deletion src/orbiter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::analytics::{
analytics_page_views_clients, analytics_page_views_metrics, analytics_page_views_top_10,
analytics_track_events,
};
use crate::assert::assert_enabled;
use crate::config::store::{
del_satellite_config as del_satellite_config_store, get_satellite_configs,
set_satellite_config as set_satellite_config_store,
Expand All @@ -37,6 +36,7 @@ use crate::types::interface::{
SetTrackEvent,
};
use crate::types::state::{AnalyticKey, HeapState, PageView, SatelliteConfigs, State, TrackEvent};
use assert::config::assert_enabled;
use ciborium::{from_reader, into_writer};
use ic_cdk::api::call::{arg_data, ArgDecoderConfig};
use ic_cdk::trap;
Expand Down
2 changes: 1 addition & 1 deletion src/orbiter/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::assert::{
use crate::assert::constraints::{
assert_analytic_key_length, assert_bot, assert_page_view_length, assert_satellite_id,
assert_session_id, assert_track_event_length,
};
Expand Down

0 comments on commit a57122e

Please sign in to comment.