Switch TryFrom back to From since proc macros must panic on errors.

This commit is contained in:
Jimmy Cuadra 2018-05-04 17:07:35 -07:00
parent 4db09dac8d
commit ba6eef9c76
2 changed files with 25 additions and 56 deletions

View File

@ -1,5 +1,3 @@
use std::convert::TryFrom;
use quote::{ToTokens, Tokens}; use quote::{ToTokens, Tokens};
use syn::synom::Synom; use syn::synom::Synom;
use syn::{Expr, ExprStruct, Ident, Member}; use syn::{Expr, ExprStruct, Ident, Member};
@ -13,10 +11,8 @@ pub struct Metadata {
pub requires_authentication: Expr, pub requires_authentication: Expr,
} }
impl TryFrom<ExprStruct> for Metadata { impl From<ExprStruct> for Metadata {
type Error = &'static str; fn from(expr: ExprStruct) -> Self {
fn try_from(expr: ExprStruct) -> Result<Self, Self::Error> {
let mut description = None; let mut description = None;
let mut method = None; let mut method = None;
let mut name = None; let mut name = None;
@ -34,41 +30,18 @@ impl TryFrom<ExprStruct> for Metadata {
"path" => path = Some(field.expr), "path" => path = Some(field.expr),
"rate_limited" => rate_limited = Some(field.expr), "rate_limited" => rate_limited = Some(field.expr),
"requires_authentication" => requires_authentication = Some(field.expr), "requires_authentication" => requires_authentication = Some(field.expr),
_ => return Err("ruma_api! metadata included unexpected field"), _ => panic!("ruma_api! metadata included unexpected field"),
} }
} }
if description.is_none() { Metadata {
return Err("ruma_api! metadata is missing description"); description: description.expect("ruma_api! `metadata` is missing `description`"),
} method: method.expect("ruma_api! `metadata` is missing `method`"),
name: name.expect("ruma_api! `metadata` is missing `name`"),
if method.is_none() { path: path.expect("ruma_api! `metadata` is missing `path`"),
return Err("ruma_api! metadata is missing method"); rate_limited: rate_limited.expect("ruma_api! `metadata` is missing `rate_limited`"),
} requires_authentication: requires_authentication
.expect("ruma_api! `metadata` is missing `requires_authentication`"),
if name.is_none() { }
return Err("ruma_api! metadata is missing name");
}
if path.is_none() {
return Err("ruma_api! metadata is missing path");
}
if rate_limited.is_none() {
return Err("ruma_api! metadata is missing rate_limited");
}
if requires_authentication.is_none() {
return Err("ruma_api! metadata is missing requires_authentication");
}
Ok(Metadata {
description: description.unwrap(),
method: method.unwrap(),
name: name.unwrap(),
path: path.unwrap(),
rate_limited: rate_limited.unwrap(),
requires_authentication: requires_authentication.unwrap(),
})
} }
} }

View File

@ -1,5 +1,3 @@
use std::convert::{TryFrom, TryInto};
use quote::{ToTokens, Tokens}; use quote::{ToTokens, Tokens};
use syn::punctuated::Pair; use syn::punctuated::Pair;
use syn::synom::Synom; use syn::synom::Synom;
@ -41,12 +39,10 @@ pub struct Api {
response: Response, response: Response,
} }
impl TryFrom<Vec<Expr>> for Api { impl From<Vec<Expr>> for Api {
type Error = &'static str; fn from(exprs: Vec<Expr>) -> Self {
fn try_from(exprs: Vec<Expr>) -> Result<Self, Self::Error> {
if exprs.len() != 3 { if exprs.len() != 3 {
return Err("ruma_api! expects 3 blocks: metadata, request, and response"); panic!("ruma_api! expects 3 blocks: metadata, request, and response");
} }
let mut metadata = None; let mut metadata = None;
@ -56,42 +52,42 @@ impl TryFrom<Vec<Expr>> for Api {
for expr in exprs { for expr in exprs {
let expr = match expr { let expr = match expr {
Expr::Struct(expr) => expr, Expr::Struct(expr) => expr,
_ => return Err("ruma_api! blocks should use struct syntax"), _ => panic!("ruma_api! blocks should use struct syntax"),
}; };
let segments = expr.path.segments; let segments = expr.path.segments;
if segments.len() != 1 { if segments.len() != 1 {
return Err("ruma_api! blocks must be one of: metadata, request, or response"); panic!("ruma_api! blocks must be one of: metadata, request, or response");
} }
let Pair::End(last_segment) = segments.last().unwrap(); let Pair::End(last_segment) = segments.last().unwrap();
match last_segment.ident.as_ref() { match last_segment.ident.as_ref() {
"metadata" => metadata = Some(expr.try_into()?), "metadata" => metadata = Some(expr.into()),
"request" => request = Some(expr.try_into()?), "request" => request = Some(expr.into()),
"response" => response = Some(expr.try_into()?), "response" => response = Some(expr.into()),
_ => return Err("ruma_api! blocks must be one of: metadata, request, or response"), _ => panic!("ruma_api! blocks must be one of: metadata, request, or response"),
} }
} }
if metadata.is_none() { if metadata.is_none() {
return Err("ruma_api! is missing metadata"); panic!("ruma_api! is missing metadata");
} }
if request.is_none() { if request.is_none() {
return Err("ruma_api! is missing request"); panic!("ruma_api! is missing request");
} }
if response.is_none() { if response.is_none() {
return Err("ruma_api! is missing response"); panic!("ruma_api! is missing response");
} }
Ok(Api { Api {
metadata: metadata.unwrap(), metadata: metadata.unwrap(),
request: request.unwrap(), request: request.unwrap(),
response: response.unwrap(), response: response.unwrap(),
}) }
} }
} }