Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

Description

This PR adds a new weight conversion module to the conversions directory, providing support for 23 different weight units across metric, imperial, troy, and scientific measurement systems.

What's New

Units Added (23 Total)

Large Metric Units:

  • Gigatonne (Gt) - 1,000,000,000,000,000 g
  • Megatonne (Mt) - 1,000,000,000,000 g
  • Metric Ton (t) - 1,000,000 g

Standard Metric Units:

  • Kilogram (kg) - 1,000 g
  • Gram (g) - 1 g
  • Milligram (mg) - 0.001 g
  • Microgram (μg) - 0.000001 g
  • Nanogram (ng) - 0.000000001 g
  • Picogram (pg) - 0.000000000001 g

Imperial/US Large Units:

  • Long Ton (UK) - 1,016 kg
  • Short Ton (US) - 907.185 kg
  • Hundredweight (cwt) - 50.8 kg
  • Quarter (qtr) - 12.7 kg

Imperial/US Common Units:

  • Stone (st) - 6.35 kg
  • Pound (lb) - 0.454 kg
  • Ounce (oz) - 0.02835 kg
  • Dram (dr) - 0.001772 kg
  • Grain (gr) - 0.0000648 kg

Troy Weight System:

  • Troy Pound (lb t) - 0.373 kg
  • Troy Ounce (oz t) - 0.031103 kg
  • Pennyweight (dwt) - 0.001555 kg

Other Units:

  • Carat (ct) - 0.0002 kg
  • Atomic Mass Unit (amu) - 1.66054×10⁻²⁷ kg

Features

Type-safe conversions using Rust enums
String-based conversions for convenience
Flexible API supporting both WeightUnit enums and string identifiers
Comprehensive aliases (e.g., "kg", "lb", "oz", "μg", "ozt")
Extensive test coverage with 100+ test cases
Full documentation with usage examples and references
Zero dependencies beyond std library
High precision conversions based on official standards

Usage Examples

Type-Safe Conversion

use conversions::weight::{convert_weight, WeightUnit};

// Convert 100 pounds to kilograms
let result = convert_weight(100.0, WeightUnit::Pound, WeightUnit::Kilogram)?;
assert_eq!(result, 45.4);

String-Based Conversion

use conversions::weight::convert_weight;

// Convert using string identifiers
let result = convert_weight(1.0, "kilogram", "pound")?;
assert_eq!(result, 2.20462);

// Alternative names work too
let result = convert_weight(16.0, "oz", "lb")?;
assert_eq!(result, 1.0);

Mixed Usage

// Mix enums and strings
let result = convert_weight(1.0, WeightUnit::Kilogram, "pound")?;
let result = convert_weight(1.0, "kg", WeightUnit::Pound)?;

Testing

All tests pass with comprehensive coverage:

cargo test weight

Test Coverage:

  • ✅ All 23 units tested
  • ✅ Bidirectional conversions verified
  • ✅ Roundtrip conversions (A→B→A) validated
  • ✅ Alternative unit names tested
  • ✅ Error handling verified
  • ✅ Display formatting tested
  • ✅ Edge cases covered

Implementation Details

The module follows the same pattern as the existing pressure.rs module:

  1. Two-step conversion: Source → Kilogram → Target
  2. Trait-based design: IntoWeightUnit trait for polymorphism
  3. Comprehensive error handling: Descriptive error messages
  4. Clean API: Consistent with other conversion modules

References

All conversion factors are sourced from official references:

Checklist

  • Code compiles without errors
  • All tests pass
  • Code formatted with cargo fmt
  • No clippy warnings
  • Module added to src/conversions/mod.rs
  • Comprehensive documentation included
  • All references cited
  • Follows existing code patterns
  • Test coverage is comprehensive

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged.

@AliAlimohammadi
Copy link
Contributor Author

clippy warnings are a CI issue.

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.10%. Comparing base (fb5784f) to head (dc9fc65).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1012      +/-   ##
==========================================
+ Coverage   96.02%   96.10%   +0.07%     
==========================================
  Files         377      378       +1     
  Lines       27217    27643     +426     
==========================================
+ Hits        26135    26566     +431     
+ Misses       1082     1077       -5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants