diff --git a/src/api/metadata.rs b/src/api/metadata.rs index 80c27ecc..6c4ab254 100644 --- a/src/api/metadata.rs +++ b/src/api/metadata.rs @@ -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 for Metadata { - type Error = &'static str; - - fn try_from(expr: ExprStruct) -> Result { +impl From 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 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(), - }) } } diff --git a/src/api/mod.rs b/src/api/mod.rs index f3a871ae..35937030 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -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> for Api { - type Error = &'static str; - - fn try_from(exprs: Vec) -> Result { +impl From> for Api { + fn from(exprs: Vec) -> 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> 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(), - }) + } } }