Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F54782501
D1869.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
D1869.diff
View Options
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
Details
Attached
Mime Type
text/plain
Expires
Sat, Oct 24, 16:13 (9 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4214997
Default Alt Text
D1869.diff (2 KB)
Attached To
Mode
D1869: Implement ApiJsonResponse for Serialize and Json
Attached
Detach File
Event Timeline
Log In to Comment