Skip to main content

pdk_unit/backends/
mod.rs

1// Copyright (c) 2026, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4pub(crate) mod anypoint;
5pub(crate) mod ldap;
6pub(crate) mod os;
7pub mod trace;
8mod utils;
9
10use crate::{UnitGrpcRequest, UnitGrpcResponse, UnitHttpRequest, UnitHttpResponse};
11use std::rc::Rc;
12
13/// The interface for a backend that can be used to mock the response of an http service.
14pub trait Backend {
15    fn call(&self, req: UnitHttpRequest) -> UnitHttpResponse;
16}
17
18impl<F> Backend for F
19where
20    F: Fn(UnitHttpRequest) -> UnitHttpResponse,
21{
22    fn call(&self, req: UnitHttpRequest) -> UnitHttpResponse {
23        self(req)
24    }
25}
26
27impl<B: Backend> Backend for Rc<B> {
28    fn call(&self, req: UnitHttpRequest) -> UnitHttpResponse {
29        self.as_ref().call(req)
30    }
31}
32
33impl Backend for UnitHttpResponse {
34    fn call(&self, _: UnitHttpRequest) -> UnitHttpResponse {
35        self.clone()
36    }
37}
38
39/// The interface for a backend that can be used to mock the response of a grpc service.
40/// For automatic implementation see [protobuf_grpc_backend](crate::protobuf_grpc_backend)
41pub trait GrpcBackend {
42    fn call(&self, req: UnitGrpcRequest) -> UnitGrpcResponse;
43}
44
45impl<F> GrpcBackend for F
46where
47    F: Fn(UnitGrpcRequest) -> UnitGrpcResponse,
48{
49    fn call(&self, req: UnitGrpcRequest) -> UnitGrpcResponse {
50        self(req)
51    }
52}
53
54impl<B: GrpcBackend> GrpcBackend for Rc<B> {
55    fn call(&self, req: UnitGrpcRequest) -> UnitGrpcResponse {
56        self.as_ref().call(req)
57    }
58}
59
60impl GrpcBackend for UnitGrpcResponse {
61    fn call(&self, _: UnitGrpcRequest) -> UnitGrpcResponse {
62        self.clone()
63    }
64}