openzeppelin_monitor/models/config/mod.rs
1//! Configuration loading and validation.
2//!
3//! This module provides traits and implementations for loading and validating
4//! configuration files for networks, monitors, and triggers.
5
6#![allow(clippy::result_large_err)]
7
8use async_trait::async_trait;
9use std::path::Path;
10
11mod error;
12mod monitor_config;
13mod network_config;
14mod trigger_config;
15
16pub use error::ConfigError;
17
18/// Common interface for loading configuration files
19#[async_trait]
20pub trait ConfigLoader: Sized {
21 /// Load all configuration files from a directory
22 ///
23 /// If no path is provided, uses the default config directory.
24 async fn load_all<T>(path: Option<&Path>) -> Result<T, error::ConfigError>
25 where
26 T: FromIterator<(String, Self)>;
27
28 /// Load configuration from a specific file path
29 async fn load_from_path(path: &Path) -> Result<Self, error::ConfigError>;
30
31 /// Validate the configuration
32 ///
33 /// Returns Ok(()) if valid, or an error message if invalid.
34 fn validate(&self) -> Result<(), error::ConfigError>;
35
36 /// Validate safety of the protocol
37 ///
38 /// Returns if safe, or logs a warning message if unsafe.
39 fn validate_protocol(&self);
40
41 /// Check if a file is a JSON file based on extension
42 fn is_json_file(path: &Path) -> bool {
43 path.extension()
44 .map(|ext| ext.to_string_lossy().to_lowercase() == "json")
45 .unwrap_or(false)
46 }
47
48 /// Resolve all secrets in the configuration
49 async fn resolve_secrets(&self) -> Result<Self, ConfigError>;
50
51 /// Validate uniqueness of the configuration
52 /// # Arguments
53 /// * `instances` - The instances to validate uniqueness against
54 /// * `current_instance` - The current instance to validate uniqueness for
55 /// * `file_path` - The path to the file containing the current instance (for logging purposes)
56 ///
57 /// Returns Ok(()) if valid, or an error message if found duplicate names.
58 fn validate_uniqueness(
59 instances: &[&Self],
60 current_instance: &Self,
61 file_path: &str,
62 ) -> Result<(), ConfigError>;
63}