Skip to content

Commit

Permalink
Merge pull request #2 from benzlokzik/dev
Browse files Browse the repository at this point in the history
Added Kelvins
  • Loading branch information
benzlokzik authored Nov 6, 2023
2 parents 5965c6a + 905d695 commit 0e597cc
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 40 deletions.
45 changes: 10 additions & 35 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Created by https://www.toptal.com/developers/gitignore/api/rust,visualstudiocode,clion
# Edit at https://www.toptal.com/developers/gitignore?templates=rust,visualstudiocode,clion
# Created by https://www.toptal.com/developers/gitignore/api/rust,visualstudiocode,jetbrains+all
# Edit at https://www.toptal.com/developers/gitignore?templates=rust,visualstudiocode,jetbrains+all

### CLion ###
### JetBrains+all ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

Expand Down Expand Up @@ -80,39 +80,14 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### CLion Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
### JetBrains+all Patch ###
# Ignore everything but code style settings and run configurations
# that are supposed to be shared within teams.

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
.idea/**/sonarlint/

# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
.idea/**/sonarIssues.xml

# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator-enh.xml
.idea/**/markdown-navigator/

# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
.idea/$CACHE_FILE$

# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
.idea/codestream.xml
.idea/*

# Azure Toolkit for IntelliJ plugin
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
.idea/**/azureSettings.xml
!.idea/codeStyles
!.idea/runConfigurations

### Rust ###
# Generated by Cargo
Expand Down Expand Up @@ -149,4 +124,4 @@ Cargo.lock
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/rust,visualstudiocode,clion
# End of https://www.toptal.com/developers/gitignore/api/rust,visualstudiocode,jetbrains+all
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fahrenheit_celsius_converter"
version = "0.1.0"
name = "fahrenheit_celsius_kelvin_converter"
version = "0.2.0"
edition = "2021"
license = "GPL-3"
description = "A simple web temperature converter written in Rust using Actix-Web"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

---

Simple http Fahrenheit to Celsius converter using [actix-web](https://actix.rs/).
Simple http Fahrenheit/Celsius/Kelvin converter using [actix-web](https://actix.rs/).


---
Expand Down
6 changes: 5 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ pub fn init(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/convert")
.route("/c_to_f/{temp}", web::get().to(temperature::celsius_to_fahrenheit))
.route("/f_to_c/{temp}", web::get().to(temperature::fahrenheit_to_celsius)),
.route("/f_to_c/{temp}", web::get().to(temperature::fahrenheit_to_celsius))
.route("/k_to_c/{temp}", web::get().to(temperature::kelvin_to_celsius))
.route("/c_to_k/{temp}", web::get().to(temperature::celsius_to_kelvin))
.route("/k_to_f/{temp}", web::get().to(temperature::kelvin_to_fahrenheit))
.route("/f_to_k/{temp}", web::get().to(temperature::fahrenheit_to_kelvin)),
);
}
26 changes: 25 additions & 1 deletion src/handlers/temperature.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use actix_web::{web, HttpResponse, Responder};
use crate::models::temperature_conversion::{Celsius, Fahrenheit};
use crate::models::temperature_conversion::{Celsius, Fahrenheit, Kelvin};

pub async fn celsius_to_fahrenheit(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
Expand All @@ -12,3 +12,27 @@ pub async fn fahrenheit_to_celsius(temp: web::Path<f64>) -> impl Responder {
let celsius = Fahrenheit::new(temp).to_celsius();
HttpResponse::Ok().json(celsius)
}

pub async fn kelvin_to_celsius(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
let celsius = Kelvin::new(temp).to_celsius();
HttpResponse::Ok().json(celsius)
}

pub async fn celsius_to_kelvin(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
let kelvin = Celsius::new(temp).to_kelvin();
HttpResponse::Ok().json(kelvin)
}

pub async fn kelvin_to_fahrenheit(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
let fahrenheit = Kelvin::new(temp).to_fahrenheit();
HttpResponse::Ok().json(fahrenheit)
}

pub async fn fahrenheit_to_kelvin(temp: web::Path<f64>) -> impl Responder {
let temp = temp.into_inner();
let kelvin = Fahrenheit::new(temp).to_kelvin();
HttpResponse::Ok().json(kelvin)
}
35 changes: 35 additions & 0 deletions src/models/temperature_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ pub struct Fahrenheit {
pub fahrenheit: f64,
}

#[derive(Serialize, Deserialize)]
pub struct Kelvin {
pub kelvin: f64,
}

impl Celsius {
pub fn new(celsius: f64) -> Self {
Celsius { celsius }
Expand All @@ -20,6 +25,12 @@ impl Celsius {
fahrenheit: (self.celsius * 1.8) + 32.0,
}
}

pub fn to_kelvin(&self) -> Kelvin {
Kelvin {
kelvin: self.celsius + 273.15,
}
}
}

impl Fahrenheit {
Expand All @@ -32,4 +43,28 @@ impl Fahrenheit {
celsius: (self.fahrenheit - 32.0) / 1.8,
}
}

pub fn to_kelvin(&self) -> Kelvin {
Kelvin {
kelvin: (self.fahrenheit - 32.0) / 1.8 + 273.15,
}
}
}

impl Kelvin {
pub fn new(kelvin: f64) -> Self {
Kelvin { kelvin }
}

pub fn to_celsius(&self) -> Celsius {
Celsius {
celsius: self.kelvin - 273.15,
}
}

pub fn to_fahrenheit(&self) -> Fahrenheit {
Fahrenheit {
fahrenheit: (self.kelvin - 273.15) * 1.8 + 32.0,
}
}
}

0 comments on commit 0e597cc

Please sign in to comment.