Skip to content

Commit

Permalink
feat: add Into<tiny_http::Header> implementation for ContentType
Browse files Browse the repository at this point in the history
Signed-off-by: Malo Polese <malo.polese@gmail.com>
  • Loading branch information
MaloPolese committed May 10, 2023
1 parent e5e2302 commit 32f5be8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
6 changes: 3 additions & 3 deletions controller/src/api/external/routes/instance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use definition::workload::WorkloadDefinition;
use route_recognizer;
use rusqlite::Connection;
use std::str::FromStr;
use std::sync::mpsc::Sender;
use tracing::{event, Level};

Expand All @@ -13,6 +12,7 @@ use crate::api::types::instance::InstanceDefinition;
use crate::api::{ApiChannel, Crud};
use crate::core::instance::Instance;
use crate::database::RikRepository;
use tiny_http::Header;

use super::HttpResult;

Expand All @@ -28,7 +28,7 @@ pub fn get(

event!(Level::INFO, "instances.get, instances found");
Ok(tiny_http::Response::from_string(instances_json)
.with_header(tiny_http::Header::from_str(ContentType::JSON.into()).unwrap())
.with_header::<Header>(ContentType::JSON.into())
.with_status_code(tiny_http::StatusCode::from(200)))
} else {
Ok(tiny_http::Response::from_string("Cannot find instances")
Expand Down Expand Up @@ -102,7 +102,7 @@ pub fn create(

Ok(
tiny_http::Response::from_string(serde_json::to_string(&instance_names)?)
.with_header(tiny_http::Header::from_str(ContentType::JSON.into()).unwrap())
.with_header::<Header>(ContentType::JSON.into())
.with_status_code(tiny_http::StatusCode::from(201)),
)
}
Expand Down
9 changes: 6 additions & 3 deletions controller/src/api/external/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use route_recognizer;
use rusqlite::Connection;
use std::io;
use std::str::FromStr;
use std::sync::mpsc::Sender;
use tiny_http::Method;
use tiny_http::Response;
Expand All @@ -25,10 +26,12 @@ pub enum ContentType {
JSON,

Check warning on line 26 in controller/src/api/external/routes/mod.rs

View workflow job for this annotation

GitHub Actions / Validating code

name `JSON` contains a capitalized acronym
}

impl Into<&str> for ContentType {
fn into(self) -> &'static str {
impl Into<tiny_http::Header> for ContentType {

Check warning on line 29 in controller/src/api/external/routes/mod.rs

View workflow job for this annotation

GitHub Actions / Validating code

an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true
fn into(self) -> tiny_http::Header {
match self {
ContentType::JSON => "Content-Type: application/json",
ContentType::JSON => {
tiny_http::Header::from_str("Content-Type: application/json").unwrap()
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions controller/src/api/external/routes/tenant.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use route_recognizer;
use rusqlite::Connection;
use std::str::FromStr;
use std::sync::mpsc::Sender;
use tiny_http::Header;
use tracing::{event, Level};

use super::HttpResult;
Expand All @@ -23,7 +23,7 @@ pub fn get(
let tenants_json = serde_json::to_string(&tenants)?;
event!(Level::INFO, "tenants.get, tenants found");
Ok(tiny_http::Response::from_string(tenants_json)
.with_header(tiny_http::Header::from_str(ContentType::JSON.into()).unwrap())
.with_header::<Header>(ContentType::JSON.into())
.with_status_code(tiny_http::StatusCode::from(200)))
} else {
Ok(tiny_http::Response::from_string("Cannot find tenant")
Expand All @@ -44,7 +44,7 @@ pub fn create(
if RikRepository::insert(connection, &tenant.name, &tenant.value).is_ok() {
event!(Level::INFO, "Create tenant");
Ok(tiny_http::Response::from_string(content)
.with_header(tiny_http::Header::from_str(ContentType::JSON.into()).unwrap())
.with_header::<Header>(ContentType::JSON.into())
.with_status_code(tiny_http::StatusCode::from(200)))
} else {
event!(Level::ERROR, "Cannot create tenant");
Expand Down
8 changes: 4 additions & 4 deletions controller/src/api/external/routes/workload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use definition::workload::WorkloadDefinition;
use route_recognizer;
use rusqlite::Connection;
use serde_json::json;
use std::str::FromStr;
use std::sync::mpsc::Sender;
use tiny_http::Header;
use tracing::{event, Level};

pub fn get(
Expand All @@ -25,7 +25,7 @@ pub fn get(
event!(Level::INFO, "workloads.get, workloads found");

Ok(tiny_http::Response::from_string(workloads_json)
.with_header(tiny_http::Header::from_str(ContentType::JSON.into()).unwrap())
.with_header::<Header>(ContentType::JSON.into())
.with_status_code(tiny_http::StatusCode::from(200)))
} else {
Ok(tiny_http::Response::from_string("Cannot find workloads")
Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn get_instances(
let instances_json = json!({ "instances": instances }).to_string();

return Ok(tiny_http::Response::from_string(instances_json)
.with_header(tiny_http::Header::from_str(ContentType::JSON.into()).unwrap())
.with_header::<Header>(ContentType::JSON.into())
.with_status_code(tiny_http::StatusCode::from(200)));
}

Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn create(
);
Ok(
tiny_http::Response::from_string(serde_json::to_string(&workload_id)?)
.with_header(tiny_http::Header::from_str(ContentType::JSON.into()).unwrap())
.with_header::<Header>(ContentType::JSON.into())
.with_status_code(tiny_http::StatusCode::from(200)),
)
} else {
Expand Down

0 comments on commit 32f5be8

Please sign in to comment.