eagre_asn1/
lib.rs

1//! eagre-asn1
2//! ==========
3//!
4//! eagre-asn1 is an asn1 library for [Rust](https://www.rust-lang.org/).
5//!
6//! It makes heavy use of macros to make the interface easy to use.
7//!
8//! Currently only DER and a very small bit of XER is supported.
9//!
10//! ## Example ##
11//! Say you have the following asn1 structure:
12//!
13//! ```text
14//! User ::= SEQUENCE {
15//!     username         UTF8String,
16//!     passwordHash     [CONTEXT 12] IMPLICIT OctetString,
17//!     age              [APPLICATION 1] EXPLICIT Integer,
18//!     admin            Boolean
19//! }
20//! ```
21//!
22//! In Rust it would look like this:
23//!
24//! ```ignore
25//! struct User {
26//!     pub username: String,
27//!     pub password_hash: Vec<u8>,
28//!     pub age: i32,
29//!     pub admin: bool,
30//! }
31//!
32//! der_sequence!{
33//!     User:
34//!         username:      NOTAG                       TYPE String,
35//!         password_hash: IMPLICIT TAG CONTEXT 12;    TYPE Vec<u8>,
36//!         age:           EXPLICIT TAG APPLICATION 1; TYPE i32,
37//!         admin:         NOTAG                       TYPE bool,
38//! }
39//! ```
40//!
41//! And serializing is as easy as:
42//!
43//! ```ignore
44//! use eagre_asn1::der::DER;
45//!
46//! let some_user = User { ... };
47//! let encoded = some_user.der_bytes().unwrap();
48//! Send to far away planet
49//! let decoded = User::der_from_bytes(encoded).unwrap();
50//! assert_eq!(some_user, decoded);
51//! ```
52//!
53
54#![deny(missing_docs,
55        missing_debug_implementations, missing_copy_implementations,
56        )]
57
58#[doc(hidden)]
59extern crate byteorder;
60
61/// **UNFINISHED** XER Implementation
62#[macro_use]
63pub mod xer;
64
65/// DER Implementation
66#[macro_use]
67pub mod der;
68
69/// Asn1 Types
70pub mod types;
71
72#[doc(hidden)]
73#[macro_export]
74macro_rules! debug_xer {
75    ($struct_name:ident) => {
76        impl ::std::fmt::Debug for $struct_name {
77            fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
78                use $crate::xer::XEREncodeable;
79                let mut stream = Vec::<u8>::new();
80                self.xer_encode(&mut stream).unwrap();
81                write!(f, "{}", ::std::string::String::from_utf8(stream).unwrap())
82            }
83        }
84    }
85}