pispas_modules/
base.rs

1use std::any::Any;
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use log::{info, error};
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        info!("BaseService: Running action: {:?}", action);
55        let action_str = action.get("ACTION").and_then(|v| v.as_str()).unwrap_or("UNKNOWN");
56        match action_str {
57            "CHECK" => {
58                info!("BaseService: Performing check action");
59                (0, "check ok".to_string())
60            }
61            "PING" => {
62                info!("BaseService: Performing ping action");
63                (0, "pong".to_string()) // Returns a "pong" response.
64            }
65            _ => {
66                info!("BaseService: Unknown action");
67                error!("BaseService: Unknown action");
68                (1, "unknown action".to_string())
69            }
70        }
71    }
72
73    /// Allows dynamic casting to `Any` for type-safe downcasting.
74    fn as_any(&self) -> &dyn Any {
75        self
76    }
77
78    /// Stops the service and performs any cleanup if necessary.
79    fn stop_service(&self) {
80        info!("BaseService: Stopping service");
81    }
82
83    /// Returns the version of the BaseService module.
84    fn get_version(&self) -> String {
85        BASE_VERSION.to_string()
86    }
87}
88
89impl Default for BaseAction {
90    /// Returns the default `BaseAction` as `Unknown`.
91    fn default() -> Self {
92        BaseAction::Unknown
93    }
94}