|
|
|
|
|
by bombela
700 days ago
|
|
> In the string -> Email example, it's probably enough to parse your string and just call it an email. You don't need to try to encode all the rules about an email into the type itself. There is also the in-between Rust approach. Start with the user input as a byte array. Pass it to a validation function, which returns it encapsulated within a new type. #[derive(Clone, Hash, Ord, Eq...)]
struct Email(Box<str>);
// validate UTF-8 +
fn validate(raw: &[u8]) -> Result<Email, EmailValidationError>;
What is annoying is the boilerplate required. You usually want your new type to behave like a (read-only) version of the original type, and you want some error type with various level of details.You can macro your way out of most of the boilerplate, with the downside that it quickly becomes a domain specific language. |
|
1: https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...