Closed snaeil closed 4 months ago
You are not using validator in the 0.16 example, just calling one validator function. You can import https://docs.rs/validator/latest/validator/trait.ValidateEmail.html and the code would be very similar to the 0.16 example.
As for structs with no named fields I don't believe they were supported in 0.16 (I think? I can't remember) and still aren't in 0.17. It could be added but first we need to revamp the whole error interface since it does expect a name
Thanks @Keats !
I now managed to make it work like this:
use validator::ValidateEmail;
#[derive(Debug, Clone)]
pub struct SubscriberEmail(String);
impl SubscriberEmail {
pub fn parse(s: String) -> Result<SubscriberEmail, String> {
let result = Self(s.clone());
if result.validate_email() {
Ok(result)
} else {
Err(format!("{} is not a valid subscriber email.", s))
}
}
}
impl ValidateEmail for SubscriberEmail {
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
Some(self.0.as_str().into())
}
}
impl AsRef<str> for SubscriberEmail {
fn as_ref(&self) -> &str {
&self.0
}
}
Summary
I didn't find a good way to migrate a struct without named fields to validator of version
0.17
. See below my previous version and what I tried to do (but failed). Is it still possible to use a struct without named fields as in0.16.0
?How it was with
0.16
What I tried with
0.17
(failing)I am getting
and