Macro asn1_rs::oid

source ·
macro_rules! oid {
    (raw $items:expr) => { ... };
    (rel $items:expr) => { ... };
    ($items:expr) => { ... };
}
Expand description

Helper macro to declare integers at compile-time

Since the DER encoded oids are not very readable we provide a procedural macro oid!. The macro can be used the following ways:

  • oid!(1.4.42.23): Create a const expression for the corresponding Oid<'static>
  • oid!(rel 42.23): Create a const expression for the corresponding relative Oid<'static>
  • oid!(raw 1.4.42.23)/oid!(raw rel 42.23): Obtain the DER encoded form as a byte array.

§Comparing oids

Comparing a parsed oid to a static oid is probably the most common thing done with oids in your code. The oid! macro can be used in expression positions for this purpose. For example

use asn1_rs::{oid, Oid};

const SOME_STATIC_OID: Oid<'static> = oid!(1.2.456);
assert_eq!(some_oid, SOME_STATIC_OID)

To get a relative Oid use oid!(rel 1.2).

Because of limitations for procedural macros (rust issue) and constants used in patterns (rust issue) the oid macro can not directly be used in patterns, also not through constants. You can do this, though:

const SOME_OID: Oid<'static> = oid!(1.2.456);
if some_oid == SOME_OID || some_oid == oid!(1.2.456) {
    println!("match");
}

// Alternatively, compare the DER encoded form directly:
const SOME_OID_RAW: &[u8] = &oid!(raw 1.2.456);
match some_oid.as_bytes() {
    SOME_OID_RAW => println!("match"),
    _ => panic!("no match"),
}

Attention, be aware that the latter version might not handle the case of a relative oid correctly. An extra check might be necessary.