Slightly simplify attribute parsing code
This commit is contained in:
parent
e8858f119d
commit
47267cc2ba
@ -14,23 +14,27 @@ pub enum Meta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Meta {
|
impl Meta {
|
||||||
/// Check if the given attribute is a ruma_api attribute. If it is, parse it, if not, return
|
/// Check if the given attribute is a ruma_api attribute. If it is, parse it.
|
||||||
/// it unchanged. Panics if the argument is an invalid ruma_api attribute.
|
///
|
||||||
pub fn from_attribute(attr: syn::Attribute) -> Result<Self, syn::Attribute> {
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the given attribute is a ruma_api attribute, but fails to parse.
|
||||||
|
pub fn from_attribute(attr: &syn::Attribute) -> Option<Self> {
|
||||||
match &attr.path {
|
match &attr.path {
|
||||||
syn::Path {
|
syn::Path {
|
||||||
leading_colon: None,
|
leading_colon: None,
|
||||||
segments,
|
segments,
|
||||||
} => {
|
} => {
|
||||||
if segments.len() == 1 && segments[0].ident == "ruma_api" {
|
if segments.len() == 1 && segments[0].ident == "ruma_api" {
|
||||||
Ok(attr
|
Some(
|
||||||
.parse_args()
|
attr.parse_args()
|
||||||
.expect("ruma_api! could not parse request field attributes"))
|
.expect("ruma_api! could not parse request field attributes"),
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
Err(attr)
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => Err(attr),
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -134,10 +134,10 @@ impl From<Vec<Field>> for Request {
|
|||||||
let mut field_kind = RequestFieldKind::Body;
|
let mut field_kind = RequestFieldKind::Body;
|
||||||
let mut header = None;
|
let mut header = None;
|
||||||
|
|
||||||
field.attrs = field.attrs.into_iter().filter_map(|attr| {
|
field.attrs.retain(|attr| {
|
||||||
let meta = match Meta::from_attribute(attr) {
|
let meta = match Meta::from_attribute(attr) {
|
||||||
Ok(meta) => meta,
|
Some(meta) => meta,
|
||||||
Err(attr) => return Some(attr),
|
None => return true,
|
||||||
};
|
};
|
||||||
|
|
||||||
match meta {
|
match meta {
|
||||||
@ -163,8 +163,8 @@ impl From<Vec<Field>> for Request {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
false
|
||||||
}).collect();
|
});
|
||||||
|
|
||||||
if field_kind == RequestFieldKind::Body {
|
if field_kind == RequestFieldKind::Body {
|
||||||
assert!(
|
assert!(
|
||||||
|
@ -104,10 +104,10 @@ impl From<Vec<Field>> for Response {
|
|||||||
let mut field_kind = ResponseFieldKind::Body;
|
let mut field_kind = ResponseFieldKind::Body;
|
||||||
let mut header = None;
|
let mut header = None;
|
||||||
|
|
||||||
field.attrs = field.attrs.into_iter().filter_map(|attr| {
|
field.attrs.retain(|attr| {
|
||||||
let meta = match Meta::from_attribute(attr) {
|
let meta = match Meta::from_attribute(attr) {
|
||||||
Ok(meta) => meta,
|
Some(meta) => meta,
|
||||||
Err(attr) => return Some(attr),
|
None => return true,
|
||||||
};
|
};
|
||||||
|
|
||||||
match meta {
|
match meta {
|
||||||
@ -131,8 +131,8 @@ impl From<Vec<Field>> for Response {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
false
|
||||||
}).collect();
|
});
|
||||||
|
|
||||||
match field_kind {
|
match field_kind {
|
||||||
ResponseFieldKind::Body => {
|
ResponseFieldKind::Body => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user