about summary refs log tree commit diff
path: root/tvix/build/src/proto/grpc_buildservice_wrapper.rs
blob: 4c176e3cf4fbb510580f42e43738083178eab1de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
use crate::buildservice::BuildService;
use std::ops::Deref;
use tonic::async_trait;

use super::{Build, BuildRequest};

/// Implements the gRPC server trait ([self::build_service_server::BuildService]
/// for anything implementing [BuildService].
pub struct GRPCBuildServiceWrapper<BUILD> {
    inner: BUILD,
}

impl<BUILD> GRPCBuildServiceWrapper<BUILD> {
    pub fn new(build_service: BUILD) -> Self {
        Self {
            inner: build_service,
        }
    }
}

#[async_trait]
impl<BUILD> crate::proto::build_service_server::BuildService for GRPCBuildServiceWrapper<BUILD>
where
    BUILD: Deref<Target = dyn BuildService> + Send + Sync + 'static,
{
    async fn do_build(
        &self,
        request: tonic::Request<BuildRequest>,
    ) -> Result<tonic::Response<Build>, tonic::Status> {
        match self.inner.do_build(request.into_inner()).await {
            Ok(resp) => Ok(tonic::Response::new(resp)),
            Err(e) => Err(tonic::Status::internal(e.to_string())),
        }
    }
}