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
4
5#[cfg(feature = "pdk")]
6pub(crate) mod anypoint;
7
8#[cfg(feature = "pdk")]
9pub(crate) mod ldap;
10
11#[cfg(feature = "pdk")]
12pub(crate) mod os;
13pub mod trace;
14
15#[cfg(feature = "pdk")]
16mod utils;
17
18use crate::{UnitGrpcRequest, UnitGrpcResponse, UnitHttpRequest, UnitHttpResponse};
19use std::rc::Rc;
20
21/// The interface for a backend that can be used to mock the response of an http service.
22pub trait Backend {
23    fn call(&self, req: UnitHttpRequest) -> UnitHttpResponse;
24}
25
26impl<F> Backend for F
27where
28    F: Fn(UnitHttpRequest) -> UnitHttpResponse,
29{
30    fn call(&self, req: UnitHttpRequest) -> UnitHttpResponse {
31        self(req)
32    }
33}
34
35impl<B: Backend> Backend for Rc<B> {
36    fn call(&self, req: UnitHttpRequest) -> UnitHttpResponse {
37        self.as_ref().call(req)
38    }
39}
40
41impl Backend for UnitHttpResponse {
42    fn call(&self, _: UnitHttpRequest) -> UnitHttpResponse {
43        self.clone()
44    }
45}
46
47/// The interface for a backend that can be used to mock the response of a grpc service.
48/// For automatic implementation see [protobuf_grpc_backend](crate::protobuf_grpc_backend)
49pub trait GrpcBackend {
50    fn call(&self, req: UnitGrpcRequest) -> UnitGrpcResponse;
51}
52
53impl<F> GrpcBackend for F
54where
55    F: Fn(UnitGrpcRequest) -> UnitGrpcResponse,
56{
57    fn call(&self, req: UnitGrpcRequest) -> UnitGrpcResponse {
58        self(req)
59    }
60}
61
62impl<B: GrpcBackend> GrpcBackend for Rc<B> {
63    fn call(&self, req: UnitGrpcRequest) -> UnitGrpcResponse {
64        self.as_ref().call(req)
65    }
66}
67
68impl GrpcBackend for UnitGrpcResponse {
69    fn call(&self, _: UnitGrpcRequest) -> UnitGrpcResponse {
70        self.clone()
71    }
72}