Skip to content
This repository has been archived by the owner on Sep 22, 2020. It is now read-only.

Commit

Permalink
allow to rtype(result=Result)
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Feb 17, 2018
1 parent 4d18ae4 commit e927c21
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ version = "0.2.0"
authors = ["Callym <hi@callym.com>", "Nikolay Kim <fafhrd91@gmail.com>"]
description = "Actor framework for Rust"
readme = "README.md"
keywords = ["actor", "futures", "actix", "async", "tokio"]
keywords = ["actix"]
homepage = "https://github.com/actix/actix-derive/"
repository = "https://github.com/actix/actix-derive.git"
documentation = "https://docs.rs/actix-derive/"
categories = ["network-programming", "asynchronous"]
license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
build = "build.rs"
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Actix is licensed under the [Apache-2.0 license](http://opensource.org/licenses/
use std::io::Error;

#[derive(Message)]
#[rtype(usize, Error)]
#[rtype(result="Result<usize, Error>")]
struct Sum(usize, usize);

fn main() {}
Expand All @@ -33,13 +33,12 @@ This code expands into following code:
```rust
extern crate actix;
use std::io::Error;
use actix::ResponseType;
use actix::Message;

struct Sum(usize, Error);

impl ResponseType for Sum {
type Item = usize;
type Error = Error;
impl Message for Sum {
type Result = Result<usize, Error>;
}

fn main() {}
Expand Down
30 changes: 20 additions & 10 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ pub fn expand(ast: &syn::DeriveInput) -> quote::Tokens {
Some(ty) => {
match ty.len() {
1 => ty[0].clone(),
_ => panic!("#[{}(type, type)] takes 2 parameters, given {}", MESSAGE_ATTR, ty.len()),
_ => panic!("#[{}(type)] takes 1 parameters, given {}", MESSAGE_ATTR, ty.len()),
}
},
None => None,
}

};

let name = &ast.ident;
Expand Down Expand Up @@ -53,14 +52,25 @@ fn get_attribute_type_multiple(ast: &syn::DeriveInput, name: &str) -> Option<Vec
}

fn meta_item_to_ty(meta_item: &syn::NestedMetaItem, name: &str) -> Option<syn::Ty> {
if let syn::NestedMetaItem::MetaItem(syn::MetaItem::Word(ref i)) = *meta_item {
let ty = syn::parse::ty(i.as_ref());
match ty {
syn::parse::IResult::Done(_, ty) => Some(ty),
_ => None,
}
} else {
panic!("The correct syntax is #[{}(type)]", name);
match *meta_item {
syn::NestedMetaItem::MetaItem(syn::MetaItem::Word(ref i)) => {
let ty = syn::parse::ty(i.as_ref());
match ty {
syn::parse::IResult::Done(_, ty) => Some(ty),
_ => None,
}
},
syn::NestedMetaItem::MetaItem(syn::MetaItem::NameValue(ref ident, ref i)) =>
{
if ident == "result" {
if let &syn::Lit::Str(ref s, _) = i {
let ty = syn::parse_type(s).unwrap();
return Some(ty);
}
}
panic!("The correct syntax is #[{}(result=\"TYPE\")]", name);
},
_ => panic!("The correct syntax is #[{}(type)]", name),
}
}

Expand Down

0 comments on commit e927c21

Please sign in to comment.