Perform all validation up front

This commit is contained in:
David Tolnay 2019-10-12 11:30:03 -07:00
parent 90020d4804
commit 1764ddeae4
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
4 changed files with 64 additions and 28 deletions

36
impl/src/valid.rs Normal file
View file

@ -0,0 +1,36 @@
use crate::ast::{Enum, Input, Struct};
use syn::{Error, Result};
pub(crate) const CHECKED: &str = "checked in validation";
impl Input<'_> {
pub(crate) fn validate(&self) -> Result<()> {
match self {
Input::Struct(input) => input.validate(),
Input::Enum(input) => input.validate(),
}
}
}
impl Struct<'_> {
fn validate(&self) -> Result<()> {
// nothing for now
Ok(())
}
}
impl Enum<'_> {
fn validate(&self) -> Result<()> {
if self.has_display() {
for variant in &self.variants {
if variant.attrs.display.is_none() {
return Err(Error::new_spanned(
variant.original,
"missing #[error(\"...\")] display attribute",
));
}
}
}
Ok(())
}
}