Skip to content

move DNS constants to macro implementation#682

Open
withjannisNLnetLabs wants to merge 7 commits into
mainfrom
improve-new-compatibility
Open

move DNS constants to macro implementation#682
withjannisNLnetLabs wants to merge 7 commits into
mainfrom
improve-new-compatibility

Conversation

@withjannisNLnetLabs

@withjannisNLnetLabs withjannisNLnetLabs commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

This PR simplifies the construction of constants i.e. assignments done by IANA.

It introduces a macro known_values_define which defines the constants and stores all values and names defined in two lists. These lists are used to implement from_mnemonic and to_mnemonic, but could be used for other functions as well.

Additionally the known_values_from_and_to_primitive macro which simplifies the conversion between the wrapper type and the wrapped primitive type.

The following types have been touched:

  • RType
  • RClass
  • QType
  • QClass
  • Nsec3HashAlgorithm
  • SecAlg
  • ZoneMDScheme
  • ZoneMDHashAlg

They are implemented using the above mentioned macros including sensible tests.
All these types now have a uniform Display and Debug implementation including doctests.

Interface Changes

  • I decided to change the formatting of the Debug output slightly. This means specifically that the output of RType and others now also the number of the Type includes.
    Example: RType::A(1), Before: RType::A

Might not be complete.

Known Limitation/Imperfection

  • The Debug output for types that don't have the same constant name as the mnemonic are not copy-pastable into code.
    For example SecAlg::DSA (mnemonic) != SecAlg::DSA_SHA1 (rust constant).

@withjannisNLnetLabs
withjannisNLnetLabs force-pushed the improve-new-compatibility branch 5 times, most recently from 659bbbc to 4ded3c2 Compare June 25, 2026 14:12
@withjannisNLnetLabs withjannisNLnetLabs self-assigned this Jun 25, 2026

@bal-e bal-e left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the overall idea of using macros for deduplicating some work. I'm curious if they can be applied to more enum types in new-base so the PR removes more lines than it adds.

Comment thread src/new/base/name/absolute.rs Outdated
Comment thread src/new/base/name/mod.rs Outdated
Comment thread src/new/base/name/mod.rs Outdated
Comment thread src/new/base/record.rs Outdated
Comment thread src/new/base/question.rs Outdated
Comment thread src/new/base/record.rs Outdated
Comment thread src/new/base/record.rs Outdated
Comment thread src/new/base/record.rs Outdated
Comment thread src/new/base/record.rs Outdated
Comment thread src/new/base/record.rs Outdated
@withjannisNLnetLabs
withjannisNLnetLabs force-pushed the improve-new-compatibility branch 9 times, most recently from e21cb60 to 0fdc786 Compare July 9, 2026 14:34
@withjannisNLnetLabs
withjannisNLnetLabs force-pushed the improve-new-compatibility branch 4 times, most recently from a6128fc to 435b4ba Compare July 14, 2026 13:38
@withjannisNLnetLabs withjannisNLnetLabs changed the title Various improvements in domain::new move DNS constants to macro implementation Jul 14, 2026
@withjannisNLnetLabs
withjannisNLnetLabs force-pushed the improve-new-compatibility branch 2 times, most recently from c1de247 to ac9de62 Compare July 14, 2026 15:56
Different DNS constants have a similar structure and need common
implementations. The added macro offers a readable interface to
construct constants for an already defined type.

The `known_values_define!` macro takes a list of constants which get
defined. Additionally the functions `get_mnemonic` and `from_mnemonic`
offer a simplified interface to access the constants.
The `known_values_from_and_to_primitive` makes it easy to define the
`From` implementations from and to the primitive value underlying the
type.
`RType`, `RClass`, `QType` and `QClass` are defined through the
`known_values_define` macro implementation. They all have a `Debug`,
`Display` and `From` implementations.
The (doc)tests verify the functionality and keep the Question and Record
types in sync.
@withjannisNLnetLabs
withjannisNLnetLabs force-pushed the improve-new-compatibility branch 2 times, most recently from f6655ac to 3fd762d Compare July 21, 2026 17:06
@withjannisNLnetLabs
withjannisNLnetLabs marked this pull request as ready for review July 21, 2026 17:08
`SecAlg` is implemented through the `known_values_define` macro. It has
a `Debug`, `Display` and `From` implementations.
`Nsec3HashAlgorithm` is implemented through the `known_values_define`
macro. It has a `Debug`, `Display` and `From` implementations.
`ZoneMDScheme` is implemented through the `known_values_define` macro.
It has a `Debug`, `Display` and `From` implementations.
`ZoneMDHashAlg` is implemented through the `known_values_define` macro.
It has a `Debug`, `Display` and `From` implementations.
@withjannisNLnetLabs
withjannisNLnetLabs force-pushed the improve-new-compatibility branch from 3fd762d to b8b8c2c Compare July 21, 2026 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants