general_tools/conversions/
temperature.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! Module that provides functions for temperature conversion.
//! [Simple Example]:
//! ```
//! use general_tools::conversions::temperature_conversions::Temperature;
//! 
//! fn main() {
//! 	let temperature: Temperature{
//! 		temperature_type: String::from("Celsius"), value: 10.55
//! 	};
//! 
//! 	// or:
//! 	/*
//! 		let mut temperature = Temperature::new();
//! 
//! 		temperature.temperature_type = String::from("Celsius");
//! 		temperature.value = 10.55;
//! 	*/
//! 	println!("{} °C to Fahrenheit: {} °F", temperature.value, temperature.to_fahrenheit()));
//! }
//! ```
//! 
//! Output: 
//! 
//! ```
//! 10.55 °C to Fahrenheit: 50.99 °F
//! ```

pub struct Temperature {
	pub temperature_type: String,
	pub value: f64,
}

impl Temperature {
	/// Creates a new Temperature instance
	pub fn new() -> Temperature{
		Temperature{
			temperature_type: String::from("Celsius"),
			value: 0.0,
		}
	}
	/// Converts the granted temperature to Celsius
	pub fn to_celsius(&self) -> f64 {
		if self.temperature_type == String::from("Celsius") {
			return self.value;
		}
		else if self.temperature_type == String::from("Fahrenheit") {
			return (self.value - 32.0) / 1.8
		} 
		else if self.temperature_type == String::from("Kelvin") {
			return self.value - 273.15;
		}
		else if self.temperature_type == String::from("Rankine") {
			return self.value / 1.8 - 273.15;
		}
		else if self.temperature_type == String::from("Newton") {
			return self.value * (100.0 / 33.0);
		}
		else if self.temperature_type == String::from("Réaumur") {
			return self.value * 1.25;
		}
		else {
			println!("Unsupported temperature type");
			return 0.0;
		}
	}
	/// Converts the granted temperature to Fahrenheit
	pub fn to_fahrenheit(&self) -> f64 {
		self.to_celsius() * 1.8 + 32.0
	}
	/// Converts the granted temperature to Kelvin
	pub fn to_kelvin(&self) -> f64 {
		self.to_celsius() + 273.15
	}
	/// Converts the granted temperature to Rankine
	pub fn to_rankine(&self) -> f64 {
		self.to_celsius() * 2.25 + 491.67
	}
	/// Converts the granted temperature to Newton
	pub fn to_newton(&self) -> f64 {
		self.to_celsius() * (33.0 / 100.0)
	}
	/// Converts the granted temperature to Réaumur
	pub fn to_reaumur(&self) -> f64 {
		self.to_celsius() * 0.8
	}
}