auyer / lagoinha-rs

Rust library that retrieve Addresses from the Brazilian Postal Code (CEP) using multiple APIs asynchronously, returning the result from the first one to respond.
Apache License 2.0
6 stars 1 forks source link

Add CEP type? #5

Open dbofmmbt opened 3 years ago

dbofmmbt commented 3 years ago

Hey, looking at the API I wondered if adding a CEP type would be a good idea. The type would be a guarantee that the inner &str looks like an address. You could receive a &CEP instead of a &str in get_address.

Something like:

impl FromStr for CEP {
    pub fn from_str(content: &str) -> Result<Self, Error> {...}
}

...
// We could use it like this
let cep: CEP = "20940040".parse().unwrap();

...
// And adapt the public interface (would be a breaking change)
pub fn get_address(cep: &CEP, timeout: ...) ....
auyer commented 3 years ago

That sounds like a good idea!

Having an option to skip validation would be nice in case someone deals with validation elsewhere. How would this proposal look like in this case ? Although, if parsing end up being cheap, this might not be needed at all.

dbofmmbt commented 3 years ago

That's a valid concern, but allowing to skip validation on the newtype's construction could be bad to reliability. Someone could pass data which was not properly validated. Since the type should uphold this guarantee, the rest of the system could behave in unexpected ways.

Would be good to know if an option to skip validation is common in the ecosystem and how it's done.

It's worth pointing that this resource wouldn't be a breaking change to that type if added after.

auyer commented 3 years ago

In terms of validation, there are at least two ways I believe should be acceptable (since all services support them both): 20940040 20940-040