rusticata / der-parser

BER/DER parser written in pure Rust. Fast, zero-copy, safe.
Apache License 2.0
85 stars 28 forks source link

oid! macro assumes der_parser is in the current namespace #46

Closed lilyball closed 3 years ago

lilyball commented 3 years ago

The oid! macro evaluates to a reference to der_parser::…. This doesn't work if I don't have the crate in my namespace, e.g. if I'm using the macro via use x509_parser::der_parser::oid;. If this was a macro_rules! macro I'd say it needs to be using $crate, I hope proc macros have an equivalent (heck, can they just use $crate directly too?).

chifflier commented 3 years ago

As far as I understand, this is not possible now (see discussion and links in https://github.com/rust-lang/rust/issues/54363) There seems to be a workaround in https://github.com/rust-lang/rust/issues/54647 but I haven't tested it.

lilyball commented 3 years ago

Hack idea: make oid!() a macro-by-example in der_parser that just invoked something like _oid!($crate, $($args)*) and have the proc macro be _oid!(). Hopefully this will pass the crate path in correctly so the proc macro can then use it in the generated code.

yuguorui commented 3 years ago

Maybe this hack could be the solution...

        format!(
             "{{ 
                  use der_parser::oid;
                  use der_parser::oid::Oid;
                  // this will be the ret value
                  der_parser::oid::Oid::new(alloc::borrow::Cow::Borrowed(&{}))}}",
             s
         )
chifflier commented 3 years ago

I'm working on this, and will try to find a solution for 6.0.

Workaround: import the re-exported der-parser:

use x509_parser::der_parser;
let oid = der_parser::oid!{ 1.2.3.4 };