pispas_modules/
base.rs

1use std::any::Any;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use log::{info, error, debug};
5use async_trait::async_trait;
6use crate::service::{Service, WebSocketWrite};
7
8/// The base module version constant.
9pub const BASE_VERSION: &str = "1.0.0";
10
11/// Enum defining actions for the BaseService.
12#[derive(Deserialize, Serialize, Debug, Clone)]
13#[serde(rename_all = "UPPERCASE")]
14pub enum BaseAction {
15    /// Represents a health check action.
16    Check,
17    /// Represents a print action with a given string.
18    Print { action: String },
19    /// Represents a ping action to verify connectivity.
20    Ping,
21    /// Represents an unknown action.
22    Unknown,
23}
24
25impl From<&str> for BaseAction {
26    /// Converts a string into a `BaseAction` enum.
27    fn from(action: &str) -> Self {
28        match action {
29            "CHECK" => BaseAction::Check,
30            "PRINT" => BaseAction::Print { action: "print".to_string() },
31            "PING" => BaseAction::Ping,
32            _ => BaseAction::Unknown,
33        }
34    }
35}
36
37/// The base service implementation.
38#[derive(Clone)]
39pub struct BaseService;
40
41impl BaseService {
42    /// Creates a new instance of `BaseService`.
43    pub fn new() -> Self {
44        info!("BaseService initialized (version: {})", BASE_VERSION);
45        BaseService
46    }
47}
48
49/// Implementation of the `Service` trait for `BaseService`.
50#[async_trait]
51impl Service for BaseService {
52    /// Runs the given action with optional WebSocket support.
53    async fn run(&self, action: Value, _write: WebSocketWrite) -> (i32, String) {
54        let action_str = action.get("ACTION").and_then(|v| v.as_str()).unwrap_or("UNKNOWN");
55        // PING is a keep-alive fired every 30s per client — log at debug so
56        // the production log stays usable. CHECK / unknown stay at info.
57        if action_str == "PING" {
58            debug!("BaseService: Running action: {:?}", action);
59        } else {
60            info!("BaseService: Running action: {:?}", action);
61        }
62        match action_str {
63            "CHECK" => {
64                info!("BaseService: Performing check action");
65                (0, "check ok".to_string())
66            }
67            "PING" => {
68                debug!("BaseService: Performing ping action");
69                (0, "pong".to_string()) // Returns a "pong" response.
70            }
71            _ => {
72                error!("BaseService: Unknown action");
73                (1, "unknown action".to_string())
74            }
75        }
76    }
77
78    /// Allows dynamic casting to `Any` for type-safe downcasting.
79    fn as_any(&self) -> &dyn Any {
80        self
81    }
82
83    /// Stops the service and performs any cleanup if necessary.
84    fn stop_service(&self) {
85        info!("BaseService: Stopping service");
86    }
87
88    /// Returns the version of the BaseService module.
89    fn get_version(&self) -> String {
90        BASE_VERSION.to_string()
91    }
92}
93
94impl Default for BaseAction {
95    /// Returns the default `BaseAction` as `Unknown`.
96    fn default() -> Self {
97        BaseAction::Unknown
98    }
99}