Skip to content

Commit

Permalink
Doc: reorder sections to match parsing/serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
chifflier committed Jan 13, 2025
1 parent e910536 commit 02c0609
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions doc/DERIVE.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,35 +242,6 @@ let ref_c = result.c.as_ref();
# Ok(()) };
```

# Serialization

## BER/DER Sequence serialization

To serialize a struct to a DER `SEQUENCE`, add the [`ToDerSequence`] derive attribute to an existing struct.
Serialization will be derived automatically for all fields, which must implement the [`ToDer`] trait.

Some parser traits may be required, so also deriving parsers using [`DerSequence`] may be required.

*Note*: serialization to BER is currently not available. In most cases, DER serialization should be enough.


For ex:

```rust
# use asn1_rs::*;
#[derive(Debug, PartialEq, DerSequence, ToDerSequence)]
pub struct S {
a: u32,
b: u16,
c: u16,
}

let s = S { a: 1, b: 2, c: 3 };
let output = s.to_der_vec().expect("serialization failed");
let (_rest, result) = S::from_ber(&output).expect("parsing failed");
assert_eq!(s, result);
```

# Advanced

## Custom errors
Expand Down Expand Up @@ -340,6 +311,35 @@ pub struct T4 {

*Note*: when deriving BER and DER parsers, errors paths are different (`TryFrom` returns the error type, while [`FromDer`] returns a [`ParseResult`]). Some code will be inserted by the `map_err` attribute to handle this transparently and keep the same function signature.

# Serialization

## BER/DER Sequence serialization

To serialize a struct to a DER `SEQUENCE`, add the [`ToDerSequence`] derive attribute to an existing struct.
Serialization will be derived automatically for all fields, which must implement the [`ToDer`] trait.

Some parser traits may be required, so also deriving parsers using [`DerSequence`] may be required.

*Note*: serialization to BER is currently not available. In most cases, DER serialization should be enough.


For ex:

```rust
# use asn1_rs::*;
#[derive(Debug, PartialEq, DerSequence, ToDerSequence)]
pub struct S {
a: u32,
b: u16,
c: u16,
}

let s = S { a: 1, b: 2, c: 3 };
let output = s.to_der_vec().expect("serialization failed");
let (_rest, result) = S::from_ber(&output).expect("parsing failed");
assert_eq!(s, result);
```

[`FromBer`]: crate::FromBer
[`FromDer`]: crate::FromDer
[`ToDer`]: crate::ToDer
Expand Down

0 comments on commit 02c0609

Please sign in to comment.