Parse and validate the url.
(url_expr: &syn::Expr)
| 56 | |
| 57 | /// Parse and validate the url. |
| 58 | pub fn parse_url(url_expr: &syn::Expr) -> syn::Result<proc_macro2::TokenStream> { |
| 59 | return match url_expr { |
| 60 | // An assignment url: url = xxx. |
| 61 | syn::Expr::Assign(assign) => { |
| 62 | let left = &assign.left; |
| 63 | if let syn::Expr::Path(ref k) = **left { |
| 64 | let key = k.path.segments.last().unwrap().ident.to_string(); |
| 65 | if key != "url" { |
| 66 | return Err(syn::Error::new( |
| 67 | proc_macro2::Span::call_site(), |
| 68 | "metadata url not specified", |
| 69 | )); |
| 70 | } |
| 71 | } |
| 72 | let right = &assign.right; |
| 73 | Ok(right.to_token_stream()) |
| 74 | } |
| 75 | // A literal url: `"http://xxx"`. |
| 76 | syn::Expr::Lit(lit) => Ok(lit.to_token_stream()), |
| 77 | // A variable url: URL. |
| 78 | syn::Expr::Path(path) => Ok(path.to_token_stream()), |
| 79 | syn::Expr::Field(field) => Ok(field.to_token_stream()), |
| 80 | _ => Err(syn::Error::new( |
| 81 | proc_macro2::Span::call_site(), |
| 82 | "metadata url is invalid", |
| 83 | )), |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | pub fn remove_url_attr(attr: &str) -> String { |
| 88 | let attrs = attr.split(","); |