use std::sync::Arc;
pub trait ServiceConsumer: ServiceNamed + Send + Sync + 'static {
fn consume_proxy() -> Arc<Self>;
}
#[async_trait::async_trait]
pub trait HttpCallable: Send + Sync {
fn service_name(&self) -> &'static str;
async fn http_invoke(
&self,
method: &str,
params: serde_json::Value,
) -> Result<serde_json::Value, String>;
}
#[async_trait::async_trait]
pub trait ServiceLifecycle: Send + Sync + 'static {
async fn init(&self) -> bool;
}
pub trait ServiceNamed: Send + Sync + 'static {
const SERVICE_NAME: &'static str;
fn service_name(&self) -> &'static str {
Self::SERVICE_NAME
}
}
#[async_trait::async_trait]
pub trait ServiceInit: Send + Sync + 'static {
async fn on_init(&self) -> bool {
true
}
fn dependencies(&self) -> Vec<&'static str> {
vec![]
}
}
#[cfg(test)]
mod tests {
use super::*;
struct MyService;
impl ServiceNamed for MyService {
const SERVICE_NAME: &'static str = "MyService";
}
struct ServiceWithDeps;
impl ServiceNamed for ServiceWithDeps {
const SERVICE_NAME: &'static str = "ServiceWithDeps";
}
#[async_trait::async_trait]
impl ServiceInit for ServiceWithDeps {
fn dependencies(&self) -> Vec<&'static str> {
vec!["DatabaseService", "ConfigService"]
}
}
struct FailingInitService;
impl ServiceNamed for FailingInitService {
const SERVICE_NAME: &'static str = "FailingInitService";
}
#[async_trait::async_trait]
impl ServiceLifecycle for FailingInitService {
async fn init(&self) -> bool {
false
}
}
struct SuccessInitService;
impl ServiceNamed for SuccessInitService {
const SERVICE_NAME: &'static str = "SuccessInitService";
}
#[async_trait::async_trait]
impl ServiceLifecycle for SuccessInitService {
async fn init(&self) -> bool {
true
}
}
#[test]
fn service_named_const_is_accessible() {
assert_eq!(MyService::SERVICE_NAME, "MyService");
}
#[test]
fn service_named_default_method_delegates_to_const() {
let svc = MyService;
assert_eq!(svc.service_name(), "MyService");
}
#[test]
fn service_named_different_services_have_different_names() {
assert_ne!(MyService::SERVICE_NAME, ServiceWithDeps::SERVICE_NAME);
}
#[test]
fn service_init_default_on_init_returns_true() {
struct DefaultInit;
impl ServiceNamed for DefaultInit {
const SERVICE_NAME: &'static str = "DefaultInit";
}
#[async_trait::async_trait]
impl ServiceInit for DefaultInit {}
let svc = DefaultInit;
assert!(pollster::block_on(svc.on_init()));
}
#[test]
fn service_init_default_dependencies_is_empty() {
struct DefaultInit;
impl ServiceNamed for DefaultInit {
const SERVICE_NAME: &'static str = "DefaultInit";
}
#[async_trait::async_trait]
impl ServiceInit for DefaultInit {}
let svc = DefaultInit;
assert!(svc.dependencies().is_empty());
}
#[test]
fn service_init_custom_dependencies() {
let svc = ServiceWithDeps;
let deps = svc.dependencies();
assert_eq!(deps.len(), 2);
assert!(deps.contains(&"DatabaseService"));
assert!(deps.contains(&"ConfigService"));
}
#[test]
fn lifecycle_init_success() {
let svc = SuccessInitService;
assert!(pollster::block_on(svc.init()));
}
#[test]
fn lifecycle_init_failure() {
let svc = FailingInitService;
assert!(!pollster::block_on(svc.init()));
}
}