| 1 | // Copyright (c) 2019 Parity Technologies (UK) Ltd. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 |
| 4 | // <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT |
| 5 | // license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 6 | // option. All files in the project carrying such notice may not be copied, |
| 7 | // modified, or distributed except according to those terms. |
| 8 | |
| 9 | //! Types describing various forms of payload data. |
| 10 | |
| 11 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/; |
| 12 | |
| 13 | use craterust-analyzer cargo soketto 0.8.1 crate/::connectionrust-analyzer cargo soketto 0.8.1 connection/::CloseReasonrust-analyzer cargo soketto 0.8.1 connection/CloseReason#; |
| 14 | |
| 15 | /// Data received from the remote end. |
| 16 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 17 | pub enum Incomingrust-analyzer cargo soketto 0.8.1 data/Incoming#<'a> { |
| 18 | /// Text or binary data. |
| 19 | Datarust-analyzer cargo soketto 0.8.1 data/Incoming#Data#(Datarust-analyzer cargo soketto 0.8.1 data/Data#), |
| 20 | /// Data sent with a PONG control frame. |
| 21 | Pongrust-analyzer cargo soketto 0.8.1 data/Incoming#Pong#(&'a [u8]), |
| 22 | /// The other end closed the connection. |
| 23 | Closedrust-analyzer cargo soketto 0.8.1 data/Incoming#Closed#(CloseReasonrust-analyzer cargo soketto 0.8.1 connection/CloseReason#), |
| 24 | } |
| 25 | |
| 26 | impl Incomingrust-analyzer cargo soketto 0.8.1 data/Incoming#<'_> { |
| 27 | /// Is this text or binary data? |
| 28 | pub fn is_datarust-analyzer cargo soketto 0.8.1 data/impl#[`Incoming<'_>`]is_data().(&self) -> bool { |
| 29 | if let Incomingrust-analyzer cargo soketto 0.8.1 data/Incoming#::Datarust-analyzer cargo soketto 0.8.1 data/Incoming#Data#(_) = self { |
| 30 | true |
| 31 | } else { |
| 32 | false |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /// Is this a PONG? |
| 37 | pub fn is_pongrust-analyzer cargo soketto 0.8.1 data/impl#[`Incoming<'_>`]is_pong().(&self) -> bool { |
| 38 | if let Incomingrust-analyzer cargo soketto 0.8.1 data/Incoming#::Pongrust-analyzer cargo soketto 0.8.1 data/Incoming#Pong#(_) = self { |
| 39 | true |
| 40 | } else { |
| 41 | false |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Is this text data? |
| 46 | pub fn is_textrust-analyzer cargo soketto 0.8.1 data/impl#[`Incoming<'_>`]is_text().(&self) -> bool { |
| 47 | if let Incomingrust-analyzer cargo soketto 0.8.1 data/Incoming#::Datarust-analyzer cargo soketto 0.8.1 data/Incoming#Data#(d) = self { |
| 48 | d.is_textrust-analyzer cargo soketto 0.8.1 data/impl#[Data]is_text().() |
| 49 | } else { |
| 50 | false |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Is this binary data? |
| 55 | pub fn is_binaryrust-analyzer cargo soketto 0.8.1 data/impl#[`Incoming<'_>`]is_binary().(&self) -> bool { |
| 56 | if let Incomingrust-analyzer cargo soketto 0.8.1 data/Incoming#::Datarust-analyzer cargo soketto 0.8.1 data/Incoming#Data#(d) = self { |
| 57 | d.is_binaryrust-analyzer cargo soketto 0.8.1 data/impl#[Data]is_binary().() |
| 58 | } else { |
| 59 | false |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)] |
| 65 | pub enum Datarust-analyzer cargo soketto 0.8.1 data/Data# { |
| 66 | /// Textual data (number of bytes). |
| 67 | Textrust-analyzer cargo soketto 0.8.1 data/Data#Text#(usize), |
| 68 | /// Binary data (number of bytes). |
| 69 | Binaryrust-analyzer cargo soketto 0.8.1 data/Data#Binary#(usize), |
| 70 | } |
| 71 | |
| 72 | impl Datarust-analyzer cargo soketto 0.8.1 data/Data# { |
| 73 | /// Is this text data? |
| 74 | pub fn is_textrust-analyzer cargo soketto 0.8.1 data/impl#[Data]is_text().(&self) -> bool { |
| 75 | if let Datarust-analyzer cargo soketto 0.8.1 data/Data#::Textrust-analyzer cargo soketto 0.8.1 data/Data#Text#(_) = self { |
| 76 | true |
| 77 | } else { |
| 78 | false |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /// Is this binary data? |
| 83 | pub fn is_binaryrust-analyzer cargo soketto 0.8.1 data/impl#[Data]is_binary().(&self) -> bool { |
| 84 | if let Datarust-analyzer cargo soketto 0.8.1 data/Data#::Binaryrust-analyzer cargo soketto 0.8.1 data/Data#Binary#(_) = self { |
| 85 | true |
| 86 | } else { |
| 87 | false |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// The length of data (number of bytes). |
| 92 | pub fn lenrust-analyzer cargo soketto 0.8.1 data/impl#[Data]len().(&self) -> usize { |
| 93 | match self { |
| 94 | Datarust-analyzer cargo soketto 0.8.1 data/Data#::Textrust-analyzer cargo soketto 0.8.1 data/Data#Text#(n) => *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().n, |
| 95 | Datarust-analyzer cargo soketto 0.8.1 data/Data#::Binaryrust-analyzer cargo soketto 0.8.1 data/Data#Binary#(n) => *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().n, |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /// Wrapper type which restricts the length of its byte slice to 125 bytes. |
| 101 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 102 | pub struct ByteSlice125rust-analyzer cargo soketto 0.8.1 data/ByteSlice125#<'a>(&'a [u8]); |
| 103 | |
| 104 | /// Error, if converting to [`ByteSlice125`] fails. |
| 105 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 106 | pub struct SliceTooLargerust-analyzer cargo soketto 0.8.1 data/SliceTooLarge#(()); |
| 107 | |
| 108 | impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for SliceTooLargerust-analyzer cargo soketto 0.8.1 data/SliceTooLarge# { |
| 109 | fn fmtrust-analyzer cargo soketto 0.8.1 data/impl#[SliceTooLarge][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 110 | f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().("Slice larger than 125 bytes") |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for SliceTooLargerust-analyzer cargo soketto 0.8.1 data/SliceTooLarge# {} |
| 115 | |
| 116 | impl<'a> TryFromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#<&'a [u8]> for ByteSlice125rust-analyzer cargo soketto 0.8.1 data/ByteSlice125#<'a> { |
| 117 | type Errorrust-analyzer cargo soketto 0.8.1 data/impl#[`ByteSlice125<'a>`][`TryFrom<&'a [u8]>`]Error# = SliceTooLargerust-analyzer cargo soketto 0.8.1 data/SliceTooLarge#; |
| 118 | |
| 119 | fn try_fromrust-analyzer cargo soketto 0.8.1 data/impl#[`ByteSlice125<'a>`][`TryFrom<&'a [u8]>`]try_from().(value: &'a [u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo soketto 0.8.1 data/impl#[`ByteSlice125<'a>`][`TryFrom<&'a [u8]>`], Selfrust-analyzer cargo soketto 0.8.1 data/impl#[`ByteSlice125<'a>`][`TryFrom<&'a [u8]>`]::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/TryFrom#Error#> { |
| 120 | if value.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().125 { |
| 121 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SliceTooLargerust-analyzer cargo soketto 0.8.1 data/SliceTooLarge#(())) |
| 122 | } else { |
| 123 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ByteSlice125rust-analyzer cargo soketto 0.8.1 data/ByteSlice125#(value)) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]> for ByteSlice125rust-analyzer cargo soketto 0.8.1 data/ByteSlice125#<'_> { |
| 129 | fn as_refrust-analyzer cargo soketto 0.8.1 data/impl#[`ByteSlice125<'_>`][`AsRef<[u8]>`]as_ref().(&self) -> &[u8] { |
| 130 | self.0rust-analyzer cargo soketto 0.8.1 data/ByteSlice125#0. |
| 131 | } |
| 132 | } |