feat(avn): implement ASN.1 Value Notation codec#544
feat(avn): implement ASN.1 Value Notation codec#544jp-marcotte wants to merge 3 commits intolibrasn:mainfrom
Conversation
Add a new `Codec::Avn` variant implementing the ASN.1 Value Notation
text format as defined in ITU-T X.680. AVN is the native textual
representation used in ASN.1 modules for DEFAULT values and value
assignments, distinct from JER (JSON) and XER (XML).
Architecture mirrors the JER codec:
- `src/avn/value.rs`: `AvnValue` IR enum with `Display` impl for
pretty-printed output (indented SEQUENCEs, `'HEX'H`/`'BIN'B`
bit strings, `{ arc... }` OIDs, `id : val` CHOICEs)
- `src/avn/enc.rs`: `Encoder` struct preserving SEQUENCE field
declaration order via `Vec<(String, AvnValue)>` (vs JER's BTreeMap)
- `src/avn/de.rs`: two-phase decode — recursive-descent lexer/parser
producing `AvnValue` tree, then type-directed dispatch; handles
space-separated OID arc notation and underscore identifiers
- Error types `AvnDecodeErrorKind` / `AvnEncodeErrorKind` integrated
into the existing snafu error hierarchy
- `Codec::Avn` dispatch wired into all five `Codec` methods
Key format rules:
- BOOLEAN: `TRUE` / `FALSE`
- OCTET STRING: `'01FF'H`
- BIT STRING: `'A0'H` (byte-aligned) or `'10'B` (non-byte-aligned)
- OID: `{ 2 5 4 3 }` (space-separated, no commas)
- SEQUENCE: `{\n field value,\n ...\n}`
- CHOICE: `identifier : value`
- Absent OPTIONAL fields omitted entirely
Round-trip tests cover: bool, integer, null, octet string, bit string,
OID, enumerated, choice, sequence-of, nested sequences with absent
optionals, and hyphenated/underscored field identifiers.
| Null, | ||
| } | ||
|
|
||
| fn lex(input: &str) -> Result<alloc::vec::Vec<Token>, DecodeError> { |
There was a problem hiding this comment.
Is there a reason not to use nom for parsing? We already depend on it for binary parsing, so it wouldn't add any cost to use it here.
There was a problem hiding this comment.
no reasons, beside lack of caffeine I guess. Let me change that.
There was a problem hiding this comment.
I mean it will be easier now that you've written it, so it will be easy to rewrite it with the tests ensuring it doesn't regress 🙂
|
Thank you for your PR! Exciting o see more formats added. What are you using it for if you're comfortable sharing? |
AVN is used with Trusted Connectivity Alliance specs, is the industry standard format to describe the content of an eSIM. |
…coder stack Sequence now stores Vec<(String, Option<AvnValue>)> where None represents an absent optional field. The decoder stack changes from Vec<AvnValue> to Vec<Option<AvnValue>> for the same reason. This removes the Absent variant from the public AvnValue enum entirely. Thanks to @XAMPPRocky for the review.
|
Interesting! Would you also be interested in contributing some those specs to be added in the |
Can you elaborate on this? Do you mean the rasn/standards ? |
Add a new
Codec::Avnvariant implementing the ASN.1 Value Notation text format as defined in ITU-T X.680. AVN is the native textual representation used in ASN.1 modules for DEFAULT values and value assignments, distinct from JER (JSON) and XER (XML).Architecture mirrors the JER codec:
src/avn/value.rs:AvnValueIR enum withDisplayimpl for pretty-printed output (indented SEQUENCEs,'HEX'H/'BIN'Bbit strings,{ arc... }OIDs,id : valCHOICEs)src/avn/enc.rs:Encoderstruct preserving SEQUENCE field declaration order viaVec<(String, AvnValue)>(vs JER's BTreeMap)src/avn/de.rs: two-phase decode — recursive-descent lexer/parser producingAvnValuetree, then type-directed dispatch; handles space-separated OID arc notation and underscore identifiersAvnDecodeErrorKind/AvnEncodeErrorKindintegrated into the existing snafu error hierarchyCodec::Avndispatch wired into all fiveCodecmethodsKey format rules:
TRUE/FALSE'01FF'H'A0'H(byte-aligned) or'10'B(non-byte-aligned){ 2 5 4 3 }(space-separated, no commas){\n field value,\n ...\n}identifier : valueRound-trip tests cover: bool, integer, null, octet string, bit string, OID, enumerated, choice, sequence-of, nested sequences with absent optionals, and hyphenated/underscored field identifiers.