pub struct PercentCodec;Expand description
Encodes and decodes percent-encoded UTF-8 text.
Its low-level Codec<Value = u8, Unit = u8> implementation converts one
byte to either one unreserved ASCII unit or a %XX escape. UTF-8 validation
remains part of the owned decode helper.
Streaming skip/replace hooks may consume the malformed span visible in the
current input chunk, so recovery output for malformed input can depend on
chunk boundaries. This does not affect valid input or strict decoding.
§Examples
use qubit_codec_misc::PercentCodec;
let codec = PercentCodec::new();
let encoded = codec.encode("hello world");
assert_eq!(encoded, "hello%20world");
assert_eq!(codec.decode(&encoded).expect("valid encoded text"), "hello world");Implementations§
Source§impl PercentCodec
impl PercentCodec
Sourcepub fn decode(&self, text: &str) -> MiscCodecResult<String>
pub fn decode(&self, text: &str) -> MiscCodecResult<String>
Decodes percent-encoded UTF-8 text.
§Parameters
text: Percent-encoded text.
§Returns
Decoded UTF-8 text.
§Errors
Returns MiscCodecError when a percent escape is malformed or decoded
bytes are not valid UTF-8.
Trait Implementations§
Source§impl Clone for PercentCodec
impl Clone for PercentCodec
Source§impl Codec for PercentCodec
impl Codec for PercentCodec
Source§type DecodeError = MiscCodecError
type DecodeError = MiscCodecError
Error reported when encoded bytes cannot be decoded as one value.
Source§type EncodeError = MiscCodecError
type EncodeError = MiscCodecError
Error type for encoding; this codec can encode every byte.
Source§fn encode_len(&self, value: &u8) -> usize
fn encode_len(&self, value: &u8) -> usize
Source§unsafe fn decode(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>>
unsafe fn decode( &mut self, input: &[u8], input_index: usize, ) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>>
Decodes one raw byte or %XX escape.
§Parameters
input: Encoded byte units.input_index: Index of the byte or escape to decode.
§Returns
The decoded byte and non-zero number of consumed units.
§Errors
Returns a decode failure when the visible escape is malformed or incomplete.
§Safety
The caller must provide an input index with at least one readable unit and a slice satisfying the codec trait’s decode preconditions.
Source§unsafe fn encode(
&mut self,
value: &u8,
output: &mut [u8],
output_index: usize,
) -> Result<usize, Self::EncodeError>
unsafe fn encode( &mut self, value: &u8, output: &mut [u8], output_index: usize, ) -> Result<usize, Self::EncodeError>
Encodes one byte using percent encoding.
§Parameters
value: Byte to encode.output: Destination unit buffer.output_index: Start index inoutput.
§Returns
Number of units written.
§Errors
This implementation accepts every byte and therefore always succeeds.
§Safety
The caller must provide enough writable output units for
Self::encode_len.
Source§unsafe fn decode_eof(
&mut self,
input: &[u8],
input_index: usize,
) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>>
unsafe fn decode_eof( &mut self, input: &[u8], input_index: usize, ) -> Result<(u8, NonZeroUsize), DecodeFailure<Self::DecodeError>>
Decodes one value after end of input has been confirmed.
§Parameters
input: Complete encoded byte units.input_index: Index of the byte or escape to decode.
§Returns
The decoded byte and non-zero number of consumed units.
§Errors
Returns a decode failure when the escape is malformed or incomplete.
§Safety
The caller must provide an input index with at least one readable unit and a slice satisfying the codec trait’s EOF decode preconditions.
Source§const MIN_UNITS_PER_VALUE: usize = 1
const MIN_UNITS_PER_VALUE: usize = 1
Source§const MAX_ENCODE_UNITS_PER_VALUE: usize = 3
const MAX_ENCODE_UNITS_PER_VALUE: usize = 3
Source§const MAX_DECODE_UNITS_PER_VALUE: usize = 3
const MAX_DECODE_UNITS_PER_VALUE: usize = 3
Source§const MAX_ENCODE_RESET_UNITS: usize = 0
const MAX_ENCODE_RESET_UNITS: usize = 0
Source§const MAX_ENCODE_FINISH_UNITS: usize = 0
const MAX_ENCODE_FINISH_UNITS: usize = 0
Source§const MAX_DECODE_RESET_VALUES: usize = 0
const MAX_DECODE_RESET_VALUES: usize = 0
Source§const MAX_DECODE_FINISH_VALUES: usize = 0
const MAX_DECODE_FINISH_VALUES: usize = 0
Source§fn can_encode_value(&self, _value: &Self::Value) -> bool
fn can_encode_value(&self, _value: &Self::Value) -> bool
value is in this codec’s encodable value domain. Read moreSource§unsafe fn encode_reset(
&mut self,
_output: &mut [Self::Unit],
_output_index: usize,
) -> Result<usize, Self::EncodeError>
unsafe fn encode_reset( &mut self, _output: &mut [Self::Unit], _output_index: usize, ) -> Result<usize, Self::EncodeError>
Source§unsafe fn encode_finish(
&mut self,
_output: &mut [Self::Unit],
_output_index: usize,
) -> Result<usize, Self::EncodeError>
unsafe fn encode_finish( &mut self, _output: &mut [Self::Unit], _output_index: usize, ) -> Result<usize, Self::EncodeError>
Source§unsafe fn decode_reset(
&mut self,
_output: &mut [Self::Value],
_output_index: usize,
) -> Result<usize, Self::DecodeError>
unsafe fn decode_reset( &mut self, _output: &mut [Self::Value], _output_index: usize, ) -> Result<usize, Self::DecodeError>
Source§unsafe fn decode_finish(
&mut self,
_output: &mut [Self::Value],
_output_index: usize,
) -> Result<usize, Self::DecodeError>
unsafe fn decode_finish( &mut self, _output: &mut [Self::Value], _output_index: usize, ) -> Result<usize, Self::DecodeError>
output. Read moreimpl Copy for PercentCodec
Source§impl Debug for PercentCodec
impl Debug for PercentCodec
Source§impl Default for PercentCodec
impl Default for PercentCodec
impl Eq for PercentCodec
Source§impl PartialEq for PercentCodec
impl PartialEq for PercentCodec
impl StructuralPartialEq for PercentCodec
Source§impl ValueDecoder<str> for PercentCodec
impl ValueDecoder<str> for PercentCodec
Source§type Error = MiscCodecError
type Error = MiscCodecError
Error type used when decoding percent-encoded text.
Errors report malformed escapes or decoded bytes that are not valid UTF-8.
Source§impl ValueEncoder<str> for PercentCodec
impl ValueEncoder<str> for PercentCodec
Source§type Error = MiscCodecError
type Error = MiscCodecError
Error type used when encoding a text value.
Percent encoding accepts every UTF-8 input, so this implementation does not produce an encoding error.