| 1 | //! Implementation of line-delimiting codec for Tokio. |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 4 | |
| 5 | use bytesrust-analyzer cargo bytes 1.12.1 crate/::BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#; |
| 6 | #[cfg(feature = "encoding")] |
| 7 | use encodingrust-analyzer cargo encoding 0.2.33 crate/::labelrust-analyzer cargo encoding 0.2.33 label/::encoding_from_whatwg_labelrust-analyzer cargo encoding 0.2.33 label/encoding_from_whatwg_label().; |
| 8 | #[cfg(feature = "encoding")] |
| 9 | use encodingrust-analyzer cargo encoding 0.2.33 crate/::{DecoderTraprust-analyzer cargo encoding 0.2.33 types/DecoderTrap#, EncoderTraprust-analyzer cargo encoding 0.2.33 types/EncoderTrap#, EncodingRefrust-analyzer cargo encoding 0.2.33 types/EncodingRef#}; |
| 10 | use tokio_utilrust-analyzer cargo tokio-util 0.7.19 crate/::codecrust-analyzer cargo tokio-util 0.7.19 codec/::{Decoderrust-analyzer cargo tokio-util 0.7.19 codec/decoder/Decoder#, Encoderrust-analyzer cargo tokio-util 0.7.19 codec/encoder/Encoder#}; |
| 11 | |
| 12 | use craterust-analyzer cargo irc-proto 1.1.0 crate/::errorrust-analyzer cargo irc-proto 1.1.0 error/; |
| 13 | |
| 14 | /// A line-based codec parameterized by an encoding. |
| 15 | pub struct LineCodecrust-analyzer cargo irc-proto 1.1.0 line/LineCodec# { |
| 16 | #[cfg(feature = "encoding")] |
| 17 | encodingrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#encoding.: EncodingRefrust-analyzer cargo encoding 0.2.33 types/EncodingRef#, |
| 18 | next_indexrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#next_index.: usize, |
| 19 | } |
| 20 | |
| 21 | impl LineCodecrust-analyzer cargo irc-proto 1.1.0 line/LineCodec# { |
| 22 | /// Creates a new instance of LineCodec from the specified encoding. |
| 23 | pub fn newrust-analyzer cargo irc-proto 1.1.0 line/impl#[LineCodec]new().(label: &str) -> errorrust-analyzer cargo irc-proto 1.1.0 error/::Resultrust-analyzer cargo irc-proto 1.1.0 error/Result#<LineCodecrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#> { |
| 24 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(LineCodecrust-analyzer cargo irc-proto 1.1.0 line/LineCodec# { |
| 25 | #[cfg(feature = "encoding")] |
| 26 | encodingrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#encoding.: match encoding_from_whatwg_labelrust-analyzer cargo encoding 0.2.33 label/encoding_from_whatwg_label().(label) { |
| 27 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(x) => x, |
| 28 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 29 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(errorrust-analyzer cargo irc-proto 1.1.0 error/::ProtocolErrorrust-analyzer cargo irc-proto 1.1.0 error/ProtocolError#::Iorust-analyzer cargo irc-proto 1.1.0 error/ProtocolError#Io#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 30 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#, |
| 31 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Attempted to use unknown codec {}.", label)[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()., |
| 32 | ))); |
| 33 | } |
| 34 | }, |
| 35 | next_indexrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#next_index.: 0, |
| 36 | }) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl Decoderrust-analyzer cargo tokio-util 0.7.19 codec/decoder/Decoder# for LineCodecrust-analyzer cargo irc-proto 1.1.0 line/LineCodec# { |
| 41 | type Itemrust-analyzer cargo irc-proto 1.1.0 line/impl#[LineCodec][Decoder]Item# = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#; |
| 42 | type Errorrust-analyzer cargo irc-proto 1.1.0 line/impl#[LineCodec][Decoder]Error# = errorrust-analyzer cargo irc-proto 1.1.0 error/::ProtocolErrorrust-analyzer cargo irc-proto 1.1.0 error/ProtocolError#; |
| 43 | |
| 44 | fn decoderust-analyzer cargo irc-proto 1.1.0 line/impl#[LineCodec][Decoder]decode().(&mut self, src: &mut BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#) -> errorrust-analyzer cargo irc-proto 1.1.0 error/::Resultrust-analyzer cargo irc-proto 1.1.0 error/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>> { |
| 45 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(offset) = src[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().self.next_indexrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#next_index...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().positionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]position().(|b| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().b rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'\n') { |
| 46 | // Remove the next frame from the buffer. |
| 47 | let line = src.split_torust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]split_to().(self.next_indexrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#next_index. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 48 | |
| 49 | // Set the search start index back to 0 since we found a newline. |
| 50 | self.next_indexrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#next_index. = 0; |
| 51 | |
| 52 | #[cfg(feature = "encoding")] |
| 53 | { |
| 54 | // Decode the line using the codec's encoding. |
| 55 | match self.encodingrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#encoding..decoderust-analyzer cargo encoding 0.2.33 types/Encoding#decode().(line.as_refrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][`AsRef<[u8]>`]as_ref().(), DecoderTraprust-analyzer cargo encoding 0.2.33 types/DecoderTrap#::Replacerust-analyzer cargo encoding 0.2.33 types/DecoderTrap#Replace#) { |
| 56 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(data) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(data)), |
| 57 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(data) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 58 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#, |
| 59 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Failed to decode {} as {}.", data, self.encodingrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#encoding..namerust-analyzer cargo encoding 0.2.33 types/Encoding#name().())[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()., |
| 60 | ) |
| 61 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[cfg(not(feature = "encoding"))] |
| 66 | { |
| 67 | match Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8(linerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/line!.to_vec()) { |
| 68 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(data) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(data)), |
| 69 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(data) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::new( |
| 70 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#, |
| 71 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Failed to decode {} as UTF-8.", data)[..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#], |
| 72 | ) |
| 73 | .into()), |
| 74 | } |
| 75 | } |
| 76 | } else { |
| 77 | // Set the search start index to the current length since we know that none of the |
| 78 | // characters we've already looked at are newlines. |
| 79 | self.next_indexrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#next_index. = src.lenrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut]len().(); |
| 80 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl Encoderrust-analyzer cargo tokio-util 0.7.19 codec/encoder/Encoder#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> for LineCodecrust-analyzer cargo irc-proto 1.1.0 line/LineCodec# { |
| 86 | type Errorrust-analyzer cargo irc-proto 1.1.0 line/impl#[LineCodec][`Encoder<String>`]Error# = errorrust-analyzer cargo irc-proto 1.1.0 error/::ProtocolErrorrust-analyzer cargo irc-proto 1.1.0 error/ProtocolError#; |
| 87 | |
| 88 | fn encoderust-analyzer cargo irc-proto 1.1.0 line/impl#[LineCodec][`Encoder<String>`]encode().(&mut self, msg: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, dst: &mut BytesMutrust-analyzer cargo bytes 1.12.1 bytes_mut/BytesMut#) -> errorrust-analyzer cargo irc-proto 1.1.0 error/::Resultrust-analyzer cargo irc-proto 1.1.0 error/Result#<()> { |
| 89 | #[cfg(feature = "encoding")] |
| 90 | { |
| 91 | // Encode the message using the codec's encoding. |
| 92 | let data: errorrust-analyzer cargo irc-proto 1.1.0 error/::Resultrust-analyzer cargo irc-proto 1.1.0 error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> = self |
| 93 | .encodingrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#encoding. |
| 94 | .encoderust-analyzer cargo encoding 0.2.33 types/Encoding#encode().(&msg, EncoderTraprust-analyzer cargo encoding 0.2.33 types/EncoderTrap#::Replacerust-analyzer cargo encoding 0.2.33 types/EncoderTrap#Replace#) |
| 95 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|data| { |
| 96 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 97 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidInputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidInput#, |
| 98 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Failed to encode {} as {}.", data, self.encodingrust-analyzer cargo irc-proto 1.1.0 line/LineCodec#encoding..namerust-analyzer cargo encoding 0.2.33 types/Encoding#name().())[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Index<I>`]index()., |
| 99 | ) |
| 100 | .intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().() |
| 101 | }); |
| 102 | // Write the encoded message to the output buffer. |
| 103 | dst.extendrust-analyzer cargo bytes 1.12.1 bytes_mut/impl#[BytesMut][`Extend<&'a u8>`]extend().(&data?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 104 | } |
| 105 | |
| 106 | #[cfg(not(feature = "encoding"))] |
| 107 | { |
| 108 | dst.extend(msg.into_bytes()); |
| 109 | } |
| 110 | |
| 111 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 112 | } |
| 113 | } |