[][src]Struct bsn1::DerBuilder

pub struct DerBuilder { /* fields omitted */ }

DerBuilder is a struct to build Der effectively.

Examples

Empty contents.

use bsn1::{Der, DerBuilder, IdRef, Length};

let id = IdRef::octet_string();

let expected = Der::new(IdRef::octet_string(), &[]);

// Because the contents is empty, do not need to call method 'extend_contents()'.
let mut builder = DerBuilder::new(id, Length::Definite(0));
let der = builder.finish();

assert_eq!(expected, der);

Not empty contents

use bsn1::{Der, DerBuilder, IdRef, Length};

let id = IdRef::octet_string();

let contents = &[0, 1, 2, 3, 4];
let expected = Der::new(IdRef::octet_string(), contents);

// Append 'contents' at once.
{
    let length = Length::Definite(contents.len());
    let mut builder = DerBuilder::new(id, length);
    builder.extend_contents(contents);
    let der = builder.finish();

    assert_eq!(expected, der);
}

// Split contents into 2 pieces and append them one by one.
{
    let length = Length::Definite(contents.len());
    let mut builder = DerBuilder::new(id, length);
    builder.extend_contents(&contents[..2]);
    builder.extend_contents(&contents[2..]);
    let der = builder.finish();

    assert_eq!(expected, der);
}

Implementations

impl DerBuilder[src]

pub fn new(id: &IdRef, contents_len: Length) -> Self[src]

Creates a new instance to build Der with id and contents whose length equals to contents_len .

contents_len must be Length::Definite because DER does not allow indefinite length.

Warnings

ASN.1 does not allow some universal identifier for DER, however, this function accepts such an identifier. For example, 'Octet String' must be primitive in DER, but this function will construct a new instance even if id represenets constructed 'Octet String.'

Panics

Panics if contents_len equals Length::Indefinite .

Examples

See examples for the struct .

pub fn extend_contents<B>(&mut self, bytes: B) where
    B: AsRef<[u8]>, 
[src]

Appends bytes to the end of the DER contents to be build.

Panics

Panics if the accumerated length of the 'contents' exceeds contents_len passed to the constructor function new as the argument.

Examples

See examples for the struct .

pub fn finish(self) -> Der[src]

Consumes self , building a new Der .

Panics

Panics if the accumerated length of the 'contents' differs from contents_len passed to the constructor function new as the argument.

Examples

See examples for the struct .

Auto Trait Implementations

impl RefUnwindSafe for DerBuilder

impl !Send for DerBuilder

impl !Sync for DerBuilder

impl Unpin for DerBuilder

impl UnwindSafe for DerBuilder

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.