pub struct Primitive<'a, S: 'a> { /* private fields */ }
Expand description
The content octets of a primitive value.
You will receive a reference to a value of this type through a closure,
possibly wrapped in a Content
value. Your task will be to read out all
the octets of the value before returning from the closure or produce an
error if the value isn’t correctly encoded. If you read less octets than
are available, whoever called the closure will produce an error after
you returned. Thus, you can read as many octets as you expect and not
bother to check whether that was all available octets.
The most basic way to do this is through the primitive’s implementation
of the Source
trait. Thus, you can gain access to some or all of the
octets and mark them read by advancing over them. You can safely attempt
to read more octets than available as that will reliably result in a
malformed error.
A number of methods are available to deal with the encodings defined for
various types. These are prefixed by to_
to indicate that they are
intended to convert the content to a certain type. They all read exactly
one encoded value.
The value provides access to the decoding mode via the mode
method.
All methodes that decode data will honour the decoding mode and enforce
that data is encoded according to the mode.
Implementations§
source§impl<'a, S: Source + 'a> Primitive<'a, S>
impl<'a, S: Source + 'a> Primitive<'a, S>
sourcepub fn content_err(&self, err: impl Into<ContentError>) -> DecodeError<S::Error>
pub fn content_err(&self, err: impl Into<ContentError>) -> DecodeError<S::Error>
Produces a content error at the current source position.
source§impl<'a, S: Source + 'a> Primitive<'a, S>
impl<'a, S: Source + 'a> Primitive<'a, S>
sourcepub fn to_bool(&mut self) -> Result<bool, DecodeError<S::Error>>
pub fn to_bool(&mut self) -> Result<bool, DecodeError<S::Error>>
Parses the primitive value as a BOOLEAN value.
sourcepub fn to_i8(&mut self) -> Result<i8, DecodeError<S::Error>>
pub fn to_i8(&mut self) -> Result<i8, DecodeError<S::Error>>
Parses the primitive value as an INTEGER limited to a i8
.
sourcepub fn to_i16(&mut self) -> Result<i16, DecodeError<S::Error>>
pub fn to_i16(&mut self) -> Result<i16, DecodeError<S::Error>>
Parses the primitive value as an INTEGER limited to a i8
.
sourcepub fn to_i32(&mut self) -> Result<i32, DecodeError<S::Error>>
pub fn to_i32(&mut self) -> Result<i32, DecodeError<S::Error>>
Parses the primitive value as an INTEGER limited to a i8
.
sourcepub fn to_i64(&mut self) -> Result<i64, DecodeError<S::Error>>
pub fn to_i64(&mut self) -> Result<i64, DecodeError<S::Error>>
Parses the primitive value as an INTEGER limited to a i8
.
sourcepub fn to_i128(&mut self) -> Result<i128, DecodeError<S::Error>>
pub fn to_i128(&mut self) -> Result<i128, DecodeError<S::Error>>
Parses the primitive value as an INTEGER limited to a i8
.
sourcepub fn to_u8(&mut self) -> Result<u8, DecodeError<S::Error>>
pub fn to_u8(&mut self) -> Result<u8, DecodeError<S::Error>>
Parses the primitive value as an INTEGER limited to a u8
.
sourcepub fn to_u16(&mut self) -> Result<u16, DecodeError<S::Error>>
pub fn to_u16(&mut self) -> Result<u16, DecodeError<S::Error>>
Parses the primitive value as an INTEGER limited to a u16
.
sourcepub fn to_u32(&mut self) -> Result<u32, DecodeError<S::Error>>
pub fn to_u32(&mut self) -> Result<u32, DecodeError<S::Error>>
Parses the primitive value as an INTEGER limited to a u32
.
sourcepub fn to_u64(&mut self) -> Result<u64, DecodeError<S::Error>>
pub fn to_u64(&mut self) -> Result<u64, DecodeError<S::Error>>
Parses the primitive value as a INTEGER value limited to a u64
.
source§impl<'a, S: Source + 'a> Primitive<'a, S>
impl<'a, S: Source + 'a> Primitive<'a, S>
Low-level Access
For basic low-level access, Primitive
implements the Source
trait.
Because the length of the content is guaranteed to be known, it can
provide a few additional methods. Note that these may still fail because
the underlying source doesn’t guarantee that as many octets are actually
available.
sourcepub fn remaining(&self) -> usize
pub fn remaining(&self) -> usize
Returns the number of remaining octets.
The returned value reflects what is left of the expected length of content and therefore decreases when the primitive is advanced.
sourcepub fn skip_all(&mut self) -> Result<(), DecodeError<S::Error>>
pub fn skip_all(&mut self) -> Result<(), DecodeError<S::Error>>
Skips the rest of the content.
Returns a malformed error if the source ends before the expected length of content.
sourcepub fn take_all(&mut self) -> Result<Bytes, DecodeError<S::Error>>
pub fn take_all(&mut self) -> Result<Bytes, DecodeError<S::Error>>
Returns the remainder of the content as a Bytes
value.
sourcepub fn slice_all(&mut self) -> Result<&[u8], DecodeError<S::Error>>
pub fn slice_all(&mut self) -> Result<&[u8], DecodeError<S::Error>>
Returns a bytes slice of the remainder of the content.
sourcepub fn with_slice_all<F, T, E>(
&mut self,
op: F
) -> Result<T, DecodeError<S::Error>>
pub fn with_slice_all<F, T, E>( &mut self, op: F ) -> Result<T, DecodeError<S::Error>>
Process a slice of the remainder of the content via a closure.
source§impl Primitive<'static, ()>
impl Primitive<'static, ()>
sourcepub fn decode_slice<F, T>(
data: &[u8],
mode: Mode,
op: F
) -> Result<T, DecodeError<Infallible>>
pub fn decode_slice<F, T>( data: &[u8], mode: Mode, op: F ) -> Result<T, DecodeError<Infallible>>
Decode a bytes slice via a closure.
This method can be used in testing code for decoding primitive
values by providing a bytes slice with the content. For instance,
decoding the to_bool
method could be tested like this:
use bcder::Mode;
use bcder::decode::Primitive;
assert_eq!(
Primitive::decode_slice(
b"\x00".as_ref(), Mode::Der,
|prim| prim.to_bool()
).unwrap(),
false
)
Trait Implementations§
source§impl<'a, S: Source + 'a> Source for Primitive<'a, S>
impl<'a, S: Source + 'a> Source for Primitive<'a, S>
source§fn request(&mut self, len: usize) -> Result<usize, Self::Error>
fn request(&mut self, len: usize) -> Result<usize, Self::Error>
len
bytes to be available. Read moresource§fn bytes(&self, start: usize, end: usize) -> Bytes
fn bytes(&self, start: usize, end: usize) -> Bytes
Bytes
value from part of the data. Read moresource§fn skip(&mut self, len: usize) -> Result<usize, Self::Error>
fn skip(&mut self, len: usize) -> Result<usize, Self::Error>
len
bytes. Read more