Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

Description

This PR adds a new module for converting between IPv4 addresses and their decimal representations.

Features Added:

  • ipv4_to_decimal: Convert IPv4 address string to u32 decimal
  • alt_ipv4_to_decimal: Alternative implementation using hexadecimal conversion
  • decimal_to_ipv4: Convert u32 decimal back to IPv4 address string
  • Custom error types for proper error handling
  • Comprehensive test coverage (10 unit tests)

Type of Change

  • New algorithm/data structure
  • Documentation
  • Tests

Testing

All tests pass:

cargo test ipv4_conversion

Results:

  • 10 unit tests passed
  • Test coverage includes:
    • Valid conversions
    • Invalid format handling
    • Invalid octet handling
    • Parse error handling
    • Round-trip conversions
    • Edge cases (0.0.0.0, 255.255.255.255)
    • Both implementation methods produce same results

Algorithm Complexity

  • Time Complexity: $O(1)$ - always processes exactly 4 octets
  • Space Complexity: $O(1)$ - uses fixed-size vectors

Reference

Based on: https://www.geeksforgeeks.org/convert-ip-address-to-integer-and-vice-versa/

Checklist

  • Code follows Rust idioms and best practices
  • Self-documenting code with comprehensive doc comments
  • All functions have doc comments
  • Tests cover normal cases, edge cases, and error cases
  • Error types implement Display and Error traits
  • Code compiles without warnings
  • All tests pass

Example Usage

use ipv4_conversion::{ipv4_to_decimal, decimal_to_ipv4};

// Convert IP to decimal
let decimal = ipv4_to_decimal("192.168.0.1").unwrap();
assert_eq!(decimal, 3232235521);

// Convert decimal back to IP
let ip = decimal_to_ipv4(3232235521).unwrap();
assert_eq!(ip, "192.168.0.1");

Additional Notes

  • The module uses proper Rust error handling with Result types
  • Custom error enum provides specific error information
  • Both a standard and alternative implementation are provided
  • Comprehensive documentation includes algorithm explanation

@codecov-commenter
Copy link

codecov-commenter commented Jan 15, 2026

Codecov Report

❌ Patch coverage is 94.69697% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.96%. Comparing base (47ea8bd) to head (c963932).

Files with missing lines Patch % Lines
src/conversions/ipv4_conversion.rs 94.69% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #994      +/-   ##
==========================================
- Coverage   95.96%   95.96%   -0.01%     
==========================================
  Files         364      365       +1     
  Lines       24810    24942     +132     
==========================================
+ Hits        23810    23935     +125     
- Misses       1000     1007       +7     

☔ 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.

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged.

@siriak siriak merged commit 5a4e21f into TheAlgorithms:master Jan 16, 2026
7 checks passed
@AliAlimohammadi AliAlimohammadi deleted the add-ipv4-conversion branch January 16, 2026 18:10
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.

3 participants