ratchet_core/
ext.rs

1// Copyright 2015-2021 Swim 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.
14
15use crate::Error;
16use bytes::BytesMut;
17use http::{HeaderMap, HeaderValue};
18use ratchet_ext::{
19    Extension, ExtensionDecoder, ExtensionEncoder, ExtensionProvider, FrameHeader,
20    ReunitableExtension, RsvBits, SplittableExtension,
21};
22use std::convert::Infallible;
23
24/// An extension stub that does nothing.
25#[derive(Debug, Default, Copy, Clone)]
26pub struct NoExt;
27
28impl ExtensionEncoder for NoExt {
29    type Error = Infallible;
30
31    fn encode(
32        &mut self,
33        _payload: &mut BytesMut,
34        _header: &mut FrameHeader,
35    ) -> Result<(), Self::Error> {
36        Ok(())
37    }
38}
39
40impl ExtensionDecoder for NoExt {
41    type Error = Infallible;
42
43    fn decode(
44        &mut self,
45        _payload: &mut BytesMut,
46        _header: &mut FrameHeader,
47    ) -> Result<(), Self::Error> {
48        Ok(())
49    }
50}
51
52/// An extension provider stub that will always succeed with `NoExt`.
53#[derive(Copy, Clone, Debug)]
54pub struct NoExtProvider;
55impl ExtensionProvider for NoExtProvider {
56    type Extension = NoExt;
57    type Error = Infallible;
58
59    fn apply_headers(&self, _headers: &mut HeaderMap) {}
60
61    fn negotiate_client(
62        &self,
63        _headers: &HeaderMap,
64    ) -> Result<Option<Self::Extension>, Self::Error> {
65        Ok(None)
66    }
67
68    fn negotiate_server(
69        &self,
70        _headers: &HeaderMap,
71    ) -> Result<Option<(Self::Extension, HeaderValue)>, Self::Error> {
72        Ok(None)
73    }
74}
75
76impl From<Infallible> for Error {
77    fn from(e: Infallible) -> Self {
78        match e {}
79    }
80}
81
82impl Extension for NoExt {
83    fn bits(&self) -> RsvBits {
84        RsvBits {
85            rsv1: false,
86            rsv2: false,
87            rsv3: false,
88        }
89    }
90}
91
92impl SplittableExtension for NoExt {
93    type SplitEncoder = NoExtEncoder;
94    type SplitDecoder = NoExtDecoder;
95
96    fn split(self) -> (Self::SplitEncoder, Self::SplitDecoder) {
97        (NoExtEncoder, NoExtDecoder)
98    }
99}
100
101impl ReunitableExtension for NoExt {
102    fn reunite(_encoder: Self::SplitEncoder, _decoder: Self::SplitDecoder) -> Self {
103        NoExt
104    }
105}
106
107#[derive(Copy, Clone, Debug)]
108#[allow(missing_docs)]
109pub struct NoExtEncoder;
110impl ExtensionEncoder for NoExtEncoder {
111    type Error = Infallible;
112
113    fn encode(
114        &mut self,
115        _payload: &mut BytesMut,
116        _header: &mut FrameHeader,
117    ) -> Result<(), Self::Error> {
118        Ok(())
119    }
120}
121
122#[derive(Copy, Clone, Debug)]
123#[allow(missing_docs)]
124pub struct NoExtDecoder;
125impl ExtensionDecoder for NoExtDecoder {
126    type Error = Infallible;
127
128    fn decode(
129        &mut self,
130        _payload: &mut BytesMut,
131        _header: &mut FrameHeader,
132    ) -> Result<(), Self::Error> {
133        Ok(())
134    }
135}