Add helper traits/macros to produce FieldResults

This commit is contained in:
Magnus Hallin 2016-12-05 22:18:42 +01:00
parent 3b71755521
commit f3e0acc742
2 changed files with 65 additions and 0 deletions

View file

@ -193,6 +193,7 @@ mod schema;
mod validation;
mod executor;
mod integrations;
#[macro_use] mod result_ext;
#[cfg(all(test, not(feature="expose-test-schema")))] mod tests;
#[cfg(feature="expose-test-schema")] pub mod tests;
@ -216,6 +217,7 @@ pub use executor::{
pub use validation::RuleError;
pub use types::scalars::ID;
pub use schema::model::RootNode;
pub use result_ext::ResultExt;
pub use schema::meta;

63
src/result_ext.rs Normal file
View file

@ -0,0 +1,63 @@
use std::fmt;
use std::result::Result;
/**
Helper trait to produce `FieldResult`s
`FieldResult` only have strings as errors as that's what's going out
in the GraphQL response. As such, all errors must be manually
converted to strings. Importing the `ResultExt` macro and using its
only method `to_field_err` can help with that:
```rust
use std::str::FromStr;
use juniper::{FieldResult, ResultExt};
fn sample_fn(s: &str) -> FieldResult<i64> {
i64::from_str(s).to_field_err()
}
# fn main() { assert_eq!(sample_fn("12"), Ok(12)); }
```
Alternatively, you can use the `jtry!` macro in all places you'd
normally use the regular `try!` macro:
```rust
#[macro_use] extern crate juniper;
use std::str::FromStr;
use juniper::{FieldResult, ResultExt};
fn sample_fn(s: &str) -> FieldResult<i64> {
let value = jtry!(i64::from_str(s));
Ok(value)
}
# fn main() { assert_eq!(sample_fn("12"), Ok(12)); }
```
*/
pub trait ResultExt<T, E: fmt::Display> {
/// Convert the error to a string by using it's `Display` implementation
fn to_field_err(self) -> Result<T, String>;
}
impl<T, E: fmt::Display> ResultExt<T, E> for Result<T, E> {
fn to_field_err(self) -> Result<T, String> {
self.map_err(|e| format!("{}", e))
}
}
/**
Helper macro to produce `FieldResult`s.
See the documentation for the [`ResultExt`](trait.ResultExt.html)
trait.
*/
#[macro_export]
macro_rules! jtry {
( $e:expr ) => { try!($crate::ResultExt::to_field_err($e)) }
}