NLnetLabs / domain

A DNS library for Rust.

Home Page:https://nlnetlabs.nl/projects/domain/about/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Difficult to work with `Dname` and `ParsedDname` due to `Octets`

jeromegn opened this issue · comments

I might be missing something, but we're having to write a lot more code than I think necessary in our tests to fashion an impl ToDname.

In our app, we have a function that takes a ParsedDname<&Bytes> because that's what we get from a Question::qname() call from a Message<Bytes>.

Now, to get an impl ToDname from scratch (without manually writing out the bytes with the length of each label, which is tedious), there are 2 options as far as I can see:

  1. Dname::bytes_from_str(&str) -> Result<Dname<Bytes>, FromStrError>
    This would be the easiest! Except that's not what the function we want to test expects. There's Dname::into that can return a ParsedDname<Bytes>, but we need a ParsedDname<&Bytes>.

  2. Doing the above, but re-parsing the bytes:

        let dname = Dname::bytes_from_str("example.com").unwrap();
        let mut parser = Parser::from_ref(dname.as_octets());
        let question = Question::new_in(ParsedDname::parse(&mut parser).unwrap(), Rtype::A);

Is there a better way to do this?

This is not a big deal, we can abstract this out. This is only used in our tests, but I feel like it could be better ergonomically.

I think Dname::for_ref and impl From<Dname<Ref>> for ParsedDname<Ref> should do the trick:

    let dname = Dname::bytes_from_str("example.com").unwrap();
    let question = Question::new_in(ParsedDname::from(dname.for_ref()), Rtype::A);

Thank you, I missed that!

commented

An example of this would be very handy in domain::base::Dname. Having a &str, my first instinct was to try and use from_str, which I didn't work for me and I couldn't really figure out what I was doing wrong.

Fair point. We will have to go over the documentation for all the changes in 0.8, anyways. Better explanations on how to deal with those octets generics will be a point of that.

We might also provide a convenience module that provides types aliases for the types you are most likely to instantiate manually.