diff --git a/src/lib.rs b/src/lib.rs index a77ec2ed..5a42fbd2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/result_ext.rs b/src/result_ext.rs new file mode 100644 index 00000000..650d42e6 --- /dev/null +++ b/src/result_ext.rs @@ -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)) } +}