Page MenuHomeDevCentral

D1869.diff
No OneTemporary

D1869.diff

diff --git a/Cargo.toml b/Cargo.toml
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -25,9 +25,11 @@
r2d2 = { version = "^0.8.2", optional = true }
rocket = "^0.3.16"
rocket_contrib = { version = "^0.3.16", features = [ "json" ] }
+serde = { version = "1.0", optional = true }
[features]
-default = ["pgsql"]
-minimal = []
+default = ["pgsql", "serialization"]
+minimal = ["serialization"]
pgsql = ["diesel", "r2d2"]
+serialization = ["serde"]
diff --git a/src/api/replies.rs b/src/api/replies.rs
--- a/src/api/replies.rs
+++ b/src/api/replies.rs
@@ -11,6 +11,9 @@
use rocket::response::Failure;
use rocket_contrib::Json;
+#[cfg(feature = "serialization")]
+use serde::Serialize;
+
#[cfg(feature = "pgsql")]
use std::error::Error;
@@ -24,6 +27,8 @@
API Response
:: Implementation for QueryResult (Diesel ORM)
+ :: Implementation for Json (Rocket contrib)
+ :: Implementation for Serialize (Serde)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/// This trait allows to consume an object into an HTTP response.
@@ -83,6 +88,29 @@
}
}
+/// Prepares an API response from a JSON.
+impl<T> ApiResponse<T> for Json<T> {
+ fn into_json_response(self) -> ApiJsonResponse<T> {
+ Ok(self)
+ }
+}
+
+/// Prepares an API response from a Serde-serializable result.
+///
+/// This is probably the easiest way to convert most struct
+/// into API responders.
+///
+/// # Examples
+///
+#[cfg(feature = "serialization")]
+impl<T> ApiResponse<T> for T
+ where T: Serialize
+{
+ fn into_json_response(self) -> ApiJsonResponse<T> {
+ Ok(Json(self))
+ }
+}
+
/* -------------------------------------------------------------
Failure response
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -41,6 +41,8 @@
extern crate r2d2;
extern crate rocket;
extern crate rocket_contrib;
+#[cfg(feature = "serialization")]
+extern crate serde;
/* -------------------------------------------------------------
Public modules offered by this crate

File Metadata

Mime Type
text/plain
Expires
Wed, Nov 27, 02:42 (20 h, 14 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2265696
Default Alt Text
D1869.diff (2 KB)

Event Timeline