-
-
Notifications
You must be signed in to change notification settings - Fork 841
Open
Labels
Description
In servo/rust-url#252, @Manishearth is writing a serde deserializer for urls since parsing a string can be expensive. However, to do this safely from untrusted sources, they need to validate that the data is semantically correct. We unfortunately don't have an easy way of doing this without writing a custom deserializer. It'd be pretty simple to do as an attribute, however. Here's my idea:
#[derive(Serialize, Deserialize)]
#[serde(finalizer="check_greater_than_10")]
struct GreaterThan10 {
n: usize,
}
fn greater_than_10<D>(item: GreaterThan10) -> Result<GreaterThan10, D::Error>
where D: Deserializer
{
if item.n > 10 {
Ok(item)
} else {
Err(Error::invalid_value("less than or equal to 10"))
}
}
radix, lucab, alexreg, raphaelcohn, Binero and 58 moreMolotovCherryMolotovCherryMolotovCherry and iantbutler01