Covertness/coap-rs

Provide easier way for setting CoAPOption::ContentFormat

gagath opened this issue · 4 comments

Hello and thanks for your great library. I managed to extend the provided example client and server with some features. One of them is setting the correct CoAPOption::ContentFormat header.

I managed to do it by consulting RFC7252 and by using the following code snippet:

fn set_content_type(resp: &mut CoAPResponse, content_type: u16) -> () {
    let msb = (content_type >> 8) as u8;
    let lsb = (content_type & 0xFF) as u8;

    let content_format: Vec<u8> = vec![msb, lsb];
    resp.add_option(CoAPOption::ContentFormat, content_format);
}

fn main() {
    // ...
    let content_format = 42; // octet-stream, cf. RFC7252
    set_content_type(&mut response, content_format);
}

However I think there is an abstraction to bring here. The first step is to create an enum for all the available registered format types, like this:

enum ContentFormat {
    TextPlain,
    ApplicationLinkFormat,
    ApplicationXML,
    ApplicationOctetStream,
    ApplicationEXI,
    ApplicationJSON
}

fn content_format_id(content_format: ContentFormat) -> u16 {
    match content_format {
        ContentFormat::TextPlain => 0,
        ContentFormat::ApplicationLinkFormat => 40,
        ContentFormat::ApplicationXML => 41,
        ContentFormat::ApplicationOctetStream => 42,
        ContentFormat::ApplicationEXI => 47,
        ContentFormat::ApplicationJSON => 50,
    }
}

And then we can think of helpers like a set_content_format method.

What do you think?

Hey @MicroJoe - we're not ignoring your requests, @Covertness and I are not very active on this project, but I'll try and take a look on this issue and the other one you commented on.

PRs are definitely welcome!

I was just measuring the temperature; I will consider PR for sure if you think this feature is welcome! I am still a Rust newbie so it won't be perfect but I am open for code reviews.

Looking forward to your PR.