use async_trait::async_trait;
use camel_api::CamelError;
use crate::component_context::ComponentContext;
use crate::endpoint::Endpoint;
#[async_trait]
pub trait Component: Send + Sync {
fn scheme(&self) -> &str;
fn create_endpoint(
&self,
uri: &str,
ctx: &dyn ComponentContext,
) -> Result<Box<dyn Endpoint>, CamelError>;
async fn start(&self) -> Result<(), CamelError> {
Ok(())
}
async fn stop(&self) -> Result<(), CamelError> {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
struct DummyComponent;
#[async_trait]
impl Component for DummyComponent {
fn scheme(&self) -> &str {
"dummy"
}
fn create_endpoint(
&self,
_uri: &str,
_ctx: &dyn ComponentContext,
) -> Result<Box<dyn Endpoint>, CamelError> {
Err(CamelError::EndpointCreationFailed("not implemented".into()))
}
}
#[tokio::test]
async fn test_component_default_lifecycle() {
let c = DummyComponent;
assert!(c.start().await.is_ok());
assert!(c.stop().await.is_ok());
}
}