Skip to main content

rustbridge_core/
lib.rs

1//! rustbridge-core - Core traits, types, and lifecycle management
2//!
3//! This crate provides the foundational types for building rustbridge plugins:
4//! - [`Plugin`] trait for implementing plugin logic
5//! - [`LifecycleState`] for managing plugin lifecycle
6//! - [`PluginError`] for error handling
7//! - [`PluginConfig`] for plugin configuration
8
9mod config;
10mod error;
11mod lifecycle;
12mod plugin;
13mod request;
14
15pub use config::{PluginConfig, PluginMetadata};
16pub use error::{PluginError, PluginResult};
17pub use lifecycle::LifecycleState;
18pub use plugin::{Plugin, PluginContext, PluginFactory};
19pub use request::{RequestContext, ResponseBuilder};
20
21use serde::{Deserialize, Serialize};
22
23/// Log levels for FFI callbacks
24#[repr(u8)]
25#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Default)]
26#[serde(rename_all = "lowercase")]
27pub enum LogLevel {
28    Trace = 0,
29    Debug = 1,
30    #[default]
31    Info = 2,
32    Warn = 3,
33    Error = 4,
34    Off = 5,
35}
36
37impl LogLevel {
38    pub fn from_u8(value: u8) -> Self {
39        match value {
40            0 => LogLevel::Trace,
41            1 => LogLevel::Debug,
42            2 => LogLevel::Info,
43            3 => LogLevel::Warn,
44            4 => LogLevel::Error,
45            _ => LogLevel::Off,
46        }
47    }
48}
49
50impl std::fmt::Display for LogLevel {
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        match self {
53            LogLevel::Trace => write!(f, "TRACE"),
54            LogLevel::Debug => write!(f, "DEBUG"),
55            LogLevel::Info => write!(f, "INFO"),
56            LogLevel::Warn => write!(f, "WARN"),
57            LogLevel::Error => write!(f, "ERROR"),
58            LogLevel::Off => write!(f, "OFF"),
59        }
60    }
61}
62
63/// Prelude module for convenient imports
64pub mod prelude {
65    pub use crate::{
66        LifecycleState, LogLevel, Plugin, PluginConfig, PluginContext, PluginError, PluginFactory,
67        PluginResult, RequestContext, ResponseBuilder,
68    };
69}
70
71#[cfg(test)]
72mod lib_tests;