rustbridge-cli 0.6.2

Build tool and code generator for rustbridge
//! {{project-name}} - A rustbridge plugin
//!
//! This template implements a simple "echo" message type.
//! Modify it to add your own message types and business logic.

use rustbridge::prelude::*;
use rustbridge::{serde_json, tracing};

// ============================================================================
// Message Types
// ============================================================================

/// Echo request message
#[derive(Debug, Clone, Serialize, Deserialize, Message)]
#[message(tag = "echo")]
pub struct EchoRequest {
    pub message: String,
}

/// Echo response message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EchoResponse {
    pub message: String,
    pub length: usize,
}

// ============================================================================
// Plugin Implementation
// ============================================================================

/// Plugin implementation
#[derive(Default)]
pub struct {{class-name}};

#[async_trait]
impl Plugin for {{class-name}} {
    async fn on_start(&self, _ctx: &PluginContext) -> PluginResult<()> {
        tracing::info!("{{project-name}} started");
        Ok(())
    }

    async fn handle_request(
        &self,
        _ctx: &PluginContext,
        type_tag: &str,
        payload: &[u8],
    ) -> PluginResult<Vec<u8>> {
        match type_tag {
            "echo" => {
                let req: EchoRequest = serde_json::from_slice(payload)?;
                let response = EchoResponse {
                    length: req.message.len(),
                    message: req.message,
                };
                Ok(serde_json::to_vec(&response)?)
            }
            _ => Err(PluginError::UnknownMessageType(type_tag.to_string())),
        }
    }

    async fn on_stop(&self, _ctx: &PluginContext) -> PluginResult<()> {
        tracing::info!("{{project-name}} stopped");
        Ok(())
    }

    fn supported_types(&self) -> Vec<&'static str> {
        vec!["echo"]
    }
}

// ============================================================================
// FFI Entry Point
// ============================================================================

// Generate FFI entry point
rustbridge_entry!({{class-name}}::default);

// Re-export FFI functions for the shared library
pub use rustbridge::ffi_exports::*;

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_echo() {
        let plugin = {{class-name}};
        let ctx = PluginContext::new(PluginConfig::default());

        let request = serde_json::to_vec(&EchoRequest {
            message: "Hello, World!".to_string(),
        })
        .unwrap();

        let response = plugin.handle_request(&ctx, "echo", &request).await.unwrap();
        let echo_response: EchoResponse = serde_json::from_slice(&response).unwrap();

        assert_eq!(echo_response.message, "Hello, World!");
        assert_eq!(echo_response.length, 13);
    }
}