Skip to main content

conjure_runtime/service/node/selector/
empty.rs

1// Copyright 2020 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14use crate::service::{Layer, Service};
15use conjure_error::Error;
16use http::Request;
17use std::marker::PhantomData;
18use std::sync::Arc;
19
20/// A node selector that always returns an error.
21pub struct EmptyNodeSelectorLayer {
22    service: Arc<str>,
23}
24
25impl EmptyNodeSelectorLayer {
26    pub fn new(service: &str) -> EmptyNodeSelectorLayer {
27        EmptyNodeSelectorLayer {
28            service: service.into(),
29        }
30    }
31}
32
33impl<S> Layer<S> for EmptyNodeSelectorLayer {
34    type Service = EmptyNodeSelectorService<S>;
35
36    fn layer(self, _: S) -> Self::Service {
37        EmptyNodeSelectorService {
38            service: self.service,
39            _p: PhantomData,
40        }
41    }
42}
43
44pub struct EmptyNodeSelectorService<S> {
45    service: Arc<str>,
46    _p: PhantomData<S>,
47}
48
49impl<S, B> Service<Request<B>> for EmptyNodeSelectorService<S>
50where
51    S: Service<Request<B>, Error = Error>,
52{
53    type Response = S::Response;
54    type Error = Error;
55
56    async fn call(&self, _: Request<B>) -> Result<Self::Response, Self::Error> {
57        Err(Error::internal_safe("service configured with no URIs")
58            .with_safe_param("service", &*self.service))
59    }
60}