Browse Source

Update thruster version (#3920)

* Update thruster version

Bump the thruster version so that it's pipeline compatible

* Move to set headers
Peter Mertz 7 years ago
parent
commit
9fc50aefcb

+ 2 - 1
frameworks/Rust/thruster/Cargo.toml

@@ -8,7 +8,8 @@ futures = "0.1"
 serde = "1.0"
 serde_derive = "1.0"
 serde_json = "1.0"
-thruster = "0.4"
+smallvec = "0.6.2"
+thruster = "0.5.0-beta5"
 
 [profile.release]
 codegen-units = 1

+ 16 - 7
frameworks/Rust/thruster/src/context.rs

@@ -1,3 +1,4 @@
+use smallvec::SmallVec;
 use std::collections::HashMap;
 use thruster::{Context, Request, Response};
 
@@ -8,22 +9,30 @@ pub struct Ctx {
     pub request_body: String,
     pub params: HashMap<String, String>,
     pub query_params: HashMap<String, String>,
-    pub headers: Vec<(String, String)>,
+    pub headers: SmallVec<[(String, String); 8]>,
     pub status_code: u32,
 }
 
+impl Ctx {
+    pub fn set_header(&mut self, key: String, val: String) {
+        self.headers.push((key, val));
+    }
+}
+
 impl Context for Ctx {
-    fn get_response(&self) -> Response<String> {
-        let mut builder = Response::builder();
-        builder.status(200);
+    fn get_response(&self) -> Response {
+        let mut response = Response::new();
+        response.status_code(200, "OK");
 
         for header_pair in &self.headers {
             let key: &str = &header_pair.0;
             let val: &str = &header_pair.1;
-            builder.header(key, val);
+            response.header(key, val);
         }
 
-        builder.body(self.body.clone()).unwrap()
+        response.body(&self.body);
+
+        response
     }
 
     fn set_body(&mut self, body: String) {
@@ -43,7 +52,7 @@ pub fn generate_context(request: Request) -> Ctx {
         params: request.params,
         query_params: request.query_params,
         request_body: request_body,
-        headers: Vec::new(),
+        headers: SmallVec::new(),
         status_code: 200,
     }
 }

+ 5 - 12
frameworks/Rust/thruster/src/main.rs

@@ -1,5 +1,6 @@
 extern crate futures;
 extern crate serde;
+extern crate smallvec;
 #[macro_use]
 extern crate serde_derive;
 extern crate serde_json;
@@ -25,12 +26,8 @@ fn json(mut context: Ctx, _chain: &MiddlewareChain<Ctx>) -> MiddlewareReturnValu
     let val = serde_json::to_string(&json).unwrap();
 
     context.body = val;
-    context
-        .headers
-        .push(("Server".to_owned(), "thruster".to_owned()));
-    context
-        .headers
-        .push(("Content-Type".to_owned(), "application/json".to_owned()));
+    context.set_header("Server".to_owned(), "thruster".to_owned());
+    context.set_header("Content-Type".to_owned(), "application/json".to_owned());
 
     Box::new(future::ok(context))
 }
@@ -39,12 +36,8 @@ fn plaintext(mut context: Ctx, _chain: &MiddlewareChain<Ctx>) -> MiddlewareRetur
     let val = "Hello, world!".to_owned();
 
     context.body = val;
-    context
-        .headers
-        .push(("Server".to_owned(), "thruster".to_owned()));
-    context
-        .headers
-        .push(("Content-Type".to_owned(), "text/plain".to_owned()));
+    context.set_header("Server".to_owned(), "thruster".to_owned());
+    context.set_header("Content-Type".to_owned(), "text/plain".to_owned());
 
     Box::new(future::ok(context))
 }