Skip to main content

Crate enc

Crate enc 

Source
Expand description

§enc

Crates.io Docs.rs License: MIT

This library aids in processing encoded data. No dependencies.

enc = "0.20.0"

§Features

All features are included by default.

§Data Encoding

  • data — Provides traits & utilities for encoding data.
  • base-64 — Provides support for base-64 encoded data.
  • hex — Provides support for hexadecimal encoded data.
  • percent — Provides support for URL percent encoded data.

§Value Encoding

  • value — Provides traits & utilities for encoding values.
  • var-int — Provides support for variable-length encoded integers.

§Bundles

  • full-data — Includes all the data encoding features.
  • full-value — Includes all the value encoding features.
  • full — Includes all the data & value encoding features.

§Dev

  • dev — Provides utilities for testing encoding implementations.

§Data & Value Encoding

There are separate traits for handling encoded data and encoded values.

The src/data folder holds the traits: Encoder, StringEncoder, Decoder and Validator. These traits provide an interface for encoders that operate on byte slices, such as base-64 & hex.

The src/value folder holds the traits: EncodedLen, EncodeToSlice, EncodeToWrite etc. These traits provide an interface for values that know how to encode and decode themselves such as var-int.

§Examples

§Base-64

use enc::base_64::Base64Encoder;
use enc::StringEncoder;

let encoder: Base64Encoder = Base64Encoder::url_safe_encoder();
let encoded: String = encoder.encode_as_string(b"Hello, World!")?;
assert_eq!(encoded, "SGVsbG8sIFdvcmxkIQ");

§Hex

use enc::hex::HexEncoder;
use enc::StringEncoder;

let encoded: String = HexEncoder::LOWER.encode_as_string(b"Hello, World!")?;
assert_eq!(encoded, "48656c6c6f2c20576f726c6421");

§Percent Encoding

use enc::percent::PercentEncoder;
use enc::StringEncoder;

let encoder: PercentEncoder = "+-.".into();
let encoded: String = encoder.encode_as_string(b"Hello, World!")?;
assert_eq!(encoded, "Hello%2C%20World%21");

§Variable-Length Integers

use enc::var_int::VarInt32;
use enc::{EncodeToSlice, DecodeFromReadPrefix};

let value: VarInt32 = VarInt32::from(123_456u32);
let bytes: Vec<u8> = value.encode_as_vec()?;

let decoded: VarInt32 = VarInt32::decode_from_read_prefix(&mut bytes.as_slice())?;
assert_eq!(decoded.value(), 123_456u32);

§Issues & Contributing

See ISSUES.md for known issues & future work and CONTRIBUTING.md for guidelines.

Modules§

base_64
Base-64 encoded data.
hex
Hexadecimal encoded data.
percent
URL percent encoded data.
test
Utilities for testing encoding implementations.
var_int
Variable-length encoded integers.

Macros§

impl_decode_from_read_by_prefix
Implements the DecodeFromRead trait for the target_type by delegating it to the DecodeFromReadPrefix::decode_from_read_prefix function on the same target_type.
impl_decode_from_read_prefix_length_prefixed
Implements of the DecodeFromReadPrefix trait for the target_type by using the DecodeFromRead::decode_from_read_length_prefixed_with_first_byte function on the same target_type.
impl_encode_to_write_stack_buf
Implements the EncodeToWrite trait for the target_type.

Enums§

Error
An error processing encoded data.

Traits§

DecodeFromRead
A value that can decode itself from a Read.
DecodeFromReadPrefix
A value that can decode itself from a Read prefix.
Decoder
Responsible for decoding binary data.
EncodeToSlice
A value that can encode itself to a slice.
EncodeToWrite
A value that can encode itself to a Write.
EncodedLen
A value with an encoded length.
Encoder
Responsible for encoding binary data.
StringEncoder
Responsible for encoding binary data as UTF-8 encoded text.
Validator
Responsible for validating encoded data.

Functions§

read_optional_byte
Reads an optional byte from the Read.
read_single_byte
Reads a single byte from the Read.