[][src]Struct bcder::decode::Primitive

pub struct Primitive<'a, S: 'a> { /* fields omitted */ }

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

impl<'a, S: 'a> Primitive<'a, S>[src]

pub fn mode(&self) -> Mode[src]

Returns the current decoding mode.

The higher-level to_ methods will use this mode to enforce that data is encoded correctly.

pub fn set_mode(&mut self, mode: Mode)[src]

Sets the current decoding mode.

impl<'a, S: Source + 'a> Primitive<'a, S>[src]

pub fn to_bool(&mut self) -> Result<bool, S::Err>[src]

Parses the primitive value as a BOOLEAN value.

pub fn to_i8(&mut self) -> Result<i8, S::Err>[src]

Parses the primitive value as an INTEGER limited to a i8.

pub fn to_i16(&mut self) -> Result<i16, S::Err>[src]

Parses the primitive value as an INTEGER limited to a i8.

pub fn to_i32(&mut self) -> Result<i32, S::Err>[src]

Parses the primitive value as an INTEGER limited to a i8.

pub fn to_i64(&mut self) -> Result<i64, S::Err>[src]

Parses the primitive value as an INTEGER limited to a i8.

pub fn to_i128(&mut self) -> Result<i128, S::Err>[src]

Parses the primitive value as an INTEGER limited to a i8.

pub fn to_u8(&mut self) -> Result<u8, S::Err>[src]

Parses the primitive value as an INTEGER limited to a u8.

pub fn to_u16(&mut self) -> Result<u16, S::Err>[src]

Parses the primitive value as an INTEGER limited to a u16.

pub fn to_u32(&mut self) -> Result<u32, S::Err>[src]

Parses the primitive value as an INTEGER limited to a u32.

pub fn to_u64(&mut self) -> Result<u64, S::Err>[src]

Parses the primitive value as a INTEGER value limited to a u64.

pub fn to_u128(&mut self) -> Result<u64, S::Err>[src]

Parses the primitive value as a INTEGER value limited to a u128.

pub fn to_null(&mut self) -> Result<(), S::Err>[src]

Converts the content octets to a NULL value.

Since such a value is empty, this doesn’t really do anything.

impl<'a, S: Source + 'a> Primitive<'a, S>[src]

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.

pub fn remaining(&self) -> usize[src]

Returns the number of remaining octets.

The returned value reflects what is left of the content and therefore decreases when the primitive is advanced.

pub fn skip_all(&mut self) -> Result<(), S::Err>[src]

Skips the rest of the content.

pub fn take_all(&mut self) -> Result<Bytes, S::Err>[src]

Returns the remainder of the content as a Bytes value.

pub fn slice_all(&mut self) -> Result<&[u8], S::Err>[src]

Returns a bytes slice of the remainder of the content.

impl<'a> Primitive<'a, &'a [u8]>[src]

pub fn decode_slice<F, T>(
    source: &'a [u8],
    mode: Mode,
    op: F
) -> Result<T, Error> where
    F: FnOnce(&mut Primitive<'_, &[u8]>) -> Result<T, Error>, 
[src]

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

impl<'a, S: Source + 'a> Source for Primitive<'a, S>[src]

type Err = S::Err

The error produced by the source. Read more

Auto Trait Implementations

impl<'a, S> RefUnwindSafe for Primitive<'a, S> where
    S: RefUnwindSafe
[src]

impl<'a, S> Send for Primitive<'a, S> where
    S: Send
[src]

impl<'a, S> Sync for Primitive<'a, S> where
    S: Sync
[src]

impl<'a, S> Unpin for Primitive<'a, S>[src]

impl<'a, S> !UnwindSafe for Primitive<'a, S>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.