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 syn::synom::Synom;
use syn::{Expr, ExprStruct, Ident, Member};
@ -13,10 +11,8 @@ pub struct Metadata {
pub requires_authentication: Expr,
}
impl TryFrom<ExprStruct> for Metadata {
type Error = &'static str;
fn try_from(expr: ExprStruct) -> Result<Self, Self::Error> {
impl From<ExprStruct> for Metadata {
fn from(expr: ExprStruct) -> Self {
let mut description = None;
let mut method = None;
let mut name = None;
@ -34,41 +30,18 @@ impl TryFrom<ExprStruct> for Metadata {
"path" => path = Some(field.expr),
"rate_limited" => rate_limited = 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() {
return Err("ruma_api! metadata is missing description");
Metadata {
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`"),
path: path.expect("ruma_api! `metadata` is missing `path`"),
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 method.is_none() {
return Err("ruma_api! metadata is missing method");
}
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 syn::punctuated::Pair;
use syn::synom::Synom;
@ -41,12 +39,10 @@ pub struct Api {
response: Response,
}
impl TryFrom<Vec<Expr>> for Api {
type Error = &'static str;
fn try_from(exprs: Vec<Expr>) -> Result<Self, Self::Error> {
impl From<Vec<Expr>> for Api {
fn from(exprs: Vec<Expr>) -> Self {
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;
@ -56,42 +52,42 @@ impl TryFrom<Vec<Expr>> for Api {
for expr in exprs {
let expr = match 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;
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();
match last_segment.ident.as_ref() {
"metadata" => metadata = Some(expr.try_into()?),
"request" => request = Some(expr.try_into()?),
"response" => response = Some(expr.try_into()?),
_ => return Err("ruma_api! blocks must be one of: metadata, request, or response"),
"metadata" => metadata = Some(expr.into()),
"request" => request = Some(expr.into()),
"response" => response = Some(expr.into()),
_ => panic!("ruma_api! blocks must be one of: metadata, request, or response"),
}
}
if metadata.is_none() {
return Err("ruma_api! is missing metadata");
panic!("ruma_api! is missing metadata");
}
if request.is_none() {
return Err("ruma_api! is missing request");
panic!("ruma_api! is missing request");
}
if response.is_none() {
return Err("ruma_api! is missing response");
panic!("ruma_api! is missing response");
}
Ok(Api {
Api {
metadata: metadata.unwrap(),
request: request.unwrap(),
response: response.unwrap(),
})
}
}
}