Crates.io | carbon_footprint_calculator |
lib.rs | carbon_footprint_calculator |
version | 0.1.0 |
source | src |
created_at | 2024-08-27 03:14:36.079564 |
updated_at | 2024-08-27 03:14:36.079564 |
description | A Carbon Footprint Calculator implemented in Rust and compiled to WASM |
homepage | |
repository | https://github.com/janpreet/carbon-footprint-calculator |
max_upload_size | |
id | 1352824 |
size | 962,214 |
This project is a Carbon Footprint Calculator implemented using Rust WebAssembly and React.
src/
: Rust WASMtests/
: testswww/
: frontendThe carbon footprint is calculated based on four main factors:
The total carbon footprint (in tons of CO2 equivalent per year) is calculated as follows:
Carbon Footprint = (Electricity * 0.0005) + (Gas * 0.005) + (CarMiles * 0.000404) + (Flights * 0.24)
Where:
Electricity (0.0005 tons CO2e/kWh): This is based on the average CO2 emissions per kWh in the US. It may vary depending on the energy mix in different regions.
Natural Gas (0.005 tons CO2e/therm): This coefficient is derived from the CO2 emissions produced by burning natural gas.
Car Miles (0.000404 tons CO2e/mile): This is based on the average emissions of a passenger vehicle. It may vary depending on the specific vehicle's fuel efficiency.
Flights (0.24 tons CO2e/flight): This is a rough estimate for an average flight. Long-haul flights would have a higher impact, while short flights would have a lower impact.
Please note that this is a basic approximation and may not reflect the most up-to-date or regionally specific emissions factors. For more accurate calculations, consider using data from reputable sources such as:
Future versions of this calculator aim to incorporate more accurate and region-specific data for improved estimations.
The Carbon Footprint Calculator is available as a Rust crate that can be easily integrated into your Rust projects, especially those targeting WebAssembly.
To use the Carbon Footprint Calculator in your Rust project, add it to your Cargo.toml
:
[dependencies]
carbon_footprint_calculator = "0.1.0"
Here's a basic example of how to use the Carbon Footprint Calculator in your Rust code:
use carbon_footprint_calculator::Calculator;
fn main() {
let calculator = Calculator::new();
let input = serde_json::json!({
"electricity": 500.0, // kWh per month
"gas": 50.0, // therms per month
"car_miles": 1000.0, // miles per month
"flights": 2.0 // flights per year
});
match calculator.calculate(input.into()) {
Ok(result) => {
let footprint: f64 = result.into_serde().unwrap();
println!("Your carbon footprint is approximately {} tons CO2e/year", footprint);
},
Err(e) => println!("Error calculating footprint: {:?}", e),
}
}
This crate is particularly useful in WebAssembly contexts. Here's how you might use it in a web application:
import init, { Calculator } from 'carbon_footprint_calculator';
async function calculateFootprint() {
await init();
const calculator = new Calculator();
const input = {
electricity: 500, // kWh per month
gas: 50, // therms per month
car_miles: 1000, // miles per month
flights: 2 // flights per year
};
const result = calculator.calculate(input);
console.log(`Your carbon footprint is approximately ${result.footprint.toFixed(2)} tons CO2e/year`);
}
calculateFootprint();
For more detailed usage instructions and API documentation, please refer to the crate documentation.
The project is automatically deployed to GitHub Pages when changes are pushed to the main branch.