http icon indicating copy to clipboard operation
http copied to clipboard

Allow using match with Scheme

Open anuraaga opened this issue 2 months ago • 3 comments

While working with the crate, I noticed that many types like Method, HeaderName and others have from_bytes, but Scheme only has from_str. Would it make sense to add from_bytes for consistency with the others? Personally my use case is parsing a scheme from [u8]. Luckily there are only two schemes so I can match myself and this isn't really tedious but wondering if it could make sense from a consistency perspective.

anuraaga avatar Oct 29 '25 10:10 anuraaga

Somewhat related but I also noticed that since the struct doesn't have PartialEq or similar, scheme can't be used in match. I guess this type may be a bit old compared to other ones in the crate.

Without using match, it seems we have to use if/else with dereferencing like this.

        if *scheme == uri::Scheme::HTTP {
            self.set_item(key, intern!(py, "http"))?;
        } else if *scheme == uri::Scheme::HTTPS {
            self.set_item(key, intern!(py, "https"))?;
        } else {
            self.set_item(key, scheme.as_str())?;
        }

anuraaga avatar Oct 29 '25 11:10 anuraaga

There's TryFrom<&[u8]> for Scheme which would do that you want.

As for the match part, the hard thing is that schemes are consider case-insensitive, but Scheme doesn't force the bytes to lower case, so PartialEq is not able to derive a byte-equivalency.

seanmonstar avatar Oct 29 '25 13:10 seanmonstar

Thanks @seanmonstar - I had missed TryFrom that works well.

I renamed the issue to focus on match - do you think it would make sense to have Scheme force the bytes to lower case to support match? In practice, I would be surprised if it causes allocations in any real code, i.e. there are actually non-lowercase schemes being parsed.

anuraaga avatar Oct 30 '25 04:10 anuraaga