asn1rs - ASN.1 Compiler for Rust
This crate allows one to generate Rust, Protobuf and SQL code from ASN.1 definitions, providing also support for basic serde integration.
The crate can be used as standalone binary using its command line interface or included invoked through its API
(for example inside the build.rs
script).
The generated Rust code has serializers and deserializers for ASN.1 UPER, protobuf and PostgreSQL and can therefore communicate with other applications supporting these formats (like Java-classes generated by the Google protobuf compiler).
Currently only UPER is supported for ASN.1.
CLI usage
It is always helpful to check asn1rs --help
in advance.
The basic usage can be seen blow:
asn1rs -t rust directory/for/rust/files some.asn1 messages.asn1
asn1rs -t proto directory/for/protobuf/files some.asn1 messages.asn1
asn1rs -t sql directory/for/sql/schema/files some.asn1 messages.asn1
API usage
The following example generates Rust, Protobuf and SQL files for all .asn1
-files in the asn/
directory of the project.
While the generated Rust code is written to the src/
directory, the Protobuf files are written to proto/
and the SQL files are written to sql/
.
Additionally, in this example each generated Rust-Type also receives Serialize
and Deserialize
derive directives (#[derive(Serialize, Deserialize)]
) for automatic serde integration.
File build.rs
:
extern crate asn1rs;
use fs;
use convert_to_proto;
use convert_to_rust;
use convert_to_sql;
use RustCodeGenerator;
Example Input / Output
Input input.asn1
MyMessages DEFINITIONS AUTOMATIC TAGS ::=
BEGIN
Header ::= SEQUENCE {
timestamp INTEGER (0..1209600000)
}
END
Output my_messages.rs
:
// use ...
// Serialize and deserialize functions for ASN.1 UPER
// Serialize and deserialize functions for protobuf
// Insert and query functions for PostgreSQL
Output my_messages.proto
:
syntax = 'proto3';
package my.messages;
message Header {
uint32 timestamp = 1;
}
Output my_messages.sql
:
EXISTS Header CASCADE;
(
id SERIAL PRIMARY KEY,
timestamp INTEGER NOT NULL
);
IF
Good to know
The module asn1rs::io
exposes (de-)serializers and helpers for direct usage without ASN.1 definitons:
use *;
use BitBuffer;
let mut buffer = default;
buffer.write_bit.unwrap;
buffer.write_utf8_string.unwrap;
send_to_another_host:
use *;
let mut buffer = Vec default;
buffer.write_varint.unwrap;
buffer.wrote_string.unwrap;
send_to_another_host:
What works
- Generating Rust Code with serializtion support for
- UPER
- Protobuf
- PostgreSQL
- Generating Protobuf Definitions
- Generating PostgreSQL Schema files
- Support for the following ASN.1 datatypes:
SEQUENCE
,SEQUENCE OF
,CHOICE
andENUMERATED
- inline
SEQUENCE OF
andCHOICE
OPTIONAL
INTEGER
with range value only (numbers orMIN
/MAX
)UTF8String
OCTET STRING
BOOLEAN
- using previously declared message types
IMPORTS .. FROM ..;
What doesn't work
- most of the (not mentioned) remaining ASN.1 data-types
- probably most non-trivial ASN.1 declarations
- comments
- let me know
TODO
Things to do at some point in time
- support
#![no_std]
- refactor / clean-up (rust) code-generators
- async postgres
LICENSE
MIT/Apache-2.0
About
This crate was initially developed during a research project at IT-Designers GmbH (http://www.it-designers.de).