use std::sync::Arc;
use tonic::codegen::async_trait;
use tonic::{Request as GrpcRequest, Response as GrpcResponse, Status};
use crate::api::RuntimeMetadata;
use crate::error::Result as ProviderResult;
use crate::generated::v1::{self as pb};
#[async_trait]
pub trait PluginRuntimeProvider:
pb::plugin_runtime_provider_server::PluginRuntimeProvider + Send + Sync + 'static
{
async fn configure(
&self,
_name: &str,
_config: serde_json::Map<String, serde_json::Value>,
) -> ProviderResult<()> {
Ok(())
}
fn metadata(&self) -> Option<RuntimeMetadata> {
None
}
fn warnings(&self) -> Vec<String> {
Vec::new()
}
async fn health_check(&self) -> ProviderResult<()> {
Ok(())
}
async fn start(&self) -> ProviderResult<()> {
Ok(())
}
async fn close(&self) -> ProviderResult<()> {
Ok(())
}
}
#[derive(Clone)]
pub(crate) struct PluginRuntimeServer<P> {
provider: Arc<P>,
}
impl<P> PluginRuntimeServer<P> {
pub(crate) fn new(provider: Arc<P>) -> Self {
Self { provider }
}
}
#[async_trait]
impl<P> pb::plugin_runtime_provider_server::PluginRuntimeProvider for PluginRuntimeServer<P>
where
P: PluginRuntimeProvider,
{
async fn get_support(
&self,
request: GrpcRequest<()>,
) -> std::result::Result<GrpcResponse<pb::PluginRuntimeSupport>, Status> {
self.provider.get_support(request).await
}
async fn start_session(
&self,
request: GrpcRequest<pb::StartPluginRuntimeSessionRequest>,
) -> std::result::Result<GrpcResponse<pb::PluginRuntimeSession>, Status> {
self.provider.start_session(request).await
}
async fn get_session(
&self,
request: GrpcRequest<pb::GetPluginRuntimeSessionRequest>,
) -> std::result::Result<GrpcResponse<pb::PluginRuntimeSession>, Status> {
self.provider.get_session(request).await
}
async fn list_sessions(
&self,
request: GrpcRequest<pb::ListPluginRuntimeSessionsRequest>,
) -> std::result::Result<GrpcResponse<pb::ListPluginRuntimeSessionsResponse>, Status> {
self.provider.list_sessions(request).await
}
async fn stop_session(
&self,
request: GrpcRequest<pb::StopPluginRuntimeSessionRequest>,
) -> std::result::Result<GrpcResponse<()>, Status> {
self.provider.stop_session(request).await
}
async fn start_plugin(
&self,
request: GrpcRequest<pb::StartHostedPluginRequest>,
) -> std::result::Result<GrpcResponse<pb::HostedPlugin>, Status> {
self.provider.start_plugin(request).await
}
}