// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

#[cfg(feature = "pdk")]
pub(crate) mod anypoint;

#[cfg(feature = "pdk")]
pub(crate) mod ldap;

#[cfg(feature = "pdk")]
pub(crate) mod os;
pub mod trace;

#[cfg(feature = "pdk")]
mod utils;

use crate::{UnitGrpcRequest, UnitGrpcResponse, UnitHttpRequest, UnitHttpResponse};
use std::rc::Rc;

/// The interface for a backend that can be used to mock the response of an http service.
pub trait Backend {
    fn call(&self, req: UnitHttpRequest) -> UnitHttpResponse;
}

impl<F> Backend for F
where
    F: Fn(UnitHttpRequest) -> UnitHttpResponse,
{
    fn call(&self, req: UnitHttpRequest) -> UnitHttpResponse {
        self(req)
    }
}

impl<B: Backend> Backend for Rc<B> {
    fn call(&self, req: UnitHttpRequest) -> UnitHttpResponse {
        self.as_ref().call(req)
    }
}

impl Backend for UnitHttpResponse {
    fn call(&self, _: UnitHttpRequest) -> UnitHttpResponse {
        self.clone()
    }
}

/// The interface for a backend that can be used to mock the response of a grpc service.
/// For automatic implementation see [protobuf_grpc_backend](crate::protobuf_grpc_backend)
pub trait GrpcBackend {
    fn call(&self, req: UnitGrpcRequest) -> UnitGrpcResponse;
}

impl<F> GrpcBackend for F
where
    F: Fn(UnitGrpcRequest) -> UnitGrpcResponse,
{
    fn call(&self, req: UnitGrpcRequest) -> UnitGrpcResponse {
        self(req)
    }
}

impl<B: GrpcBackend> GrpcBackend for Rc<B> {
    fn call(&self, req: UnitGrpcRequest) -> UnitGrpcResponse {
        self.as_ref().call(req)
    }
}

impl GrpcBackend for UnitGrpcResponse {
    fn call(&self, _: UnitGrpcRequest) -> UnitGrpcResponse {
        self.clone()
    }
}
