Crates.io | overpunch_ng |
lib.rs | overpunch_ng |
version | 0.1.0 |
created_at | 2025-04-03 15:51:59.984707+00 |
updated_at | 2025-04-03 15:51:59.984707+00 |
description | Robust and extensible signed overpunch parsing and formatting for Rust Decimals |
homepage | https://github.com/copyleftdev/overpunch_ng |
repository | https://github.com/copyleftdev/overpunch_ng |
max_upload_size | |
id | 1618437 |
size | 85,409 |
A Rust library for handling overpunch encoding and decoding of decimal values. Overpunch encoding is a technique used in legacy COBOL and mainframe systems to efficiently represent signed decimal values.
Add the following to your Cargo.toml
:
[dependencies]
overpunch_ng = "0.1.0"
use overpunch_ng::{format, extract};
use rust_decimal::Decimal;
use std::str::FromStr;
// Format a decimal value with overpunch encoding
let value = Decimal::from_str("123.45").unwrap();
let formatted = format(value, 2).unwrap();
println!("Formatted: {}", formatted); // Output: "123E5"
// Extract a decimal value from an overpunched string
let extracted = extract("123E5", 2).unwrap();
println!("Extracted: {}", extracted); // Output: 123.45
use overpunch_ng::{format_with_encoding, extract_with_encoding, encoding::Encoding};
use rust_decimal::Decimal;
use std::str::FromStr;
// Format using EBCDIC encoding
let value = Decimal::from_str("-123.45").unwrap();
let formatted = format_with_encoding(value, 2, &overpunch_ng::encoding::Ebcdic).unwrap();
// Extract using EBCDIC encoding
let extracted = extract_with_encoding(&formatted, 2, &overpunch_ng::encoding::Ebcdic).unwrap();
You can implement your own encoding by implementing the Encoding
trait:
use overpunch_ng::{encoding::{Encoding, Sign}, error::Error};
struct MyCustomEncoding;
impl Encoding for MyCustomEncoding {
fn encode(&self, digit: u8, sign: Sign) -> Result<char, Error> {
// Your implementation here
}
fn decode(&self, c: char) -> Result<(u8, Sign), Error> {
// Your implementation here
}
fn decode_digit(&self, c: char) -> Result<u8, Error> {
// Your implementation here
}
}
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Author
Maintained by [copyleftdev](https://github.com/copyleftdev).
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.