A Rust implementation of Twilio's Message Segment Calculator, originally written in JavaScript.
This library provides functionality to segment SMS messages, detect their character set, encoding, and other properties. It's particularly useful for applications that need to work with SMS messages at a low level, ensuring proper encoding and segmentation for delivery.
- Segment SMS messages
- Detect character encoding (GSM-7 or UCS-2)
- Smart character encoding
- Handle User Data Headers
- Calculate message size and segment count
Add the following to your Cargo.toml
:
[dependencies]
message_segment_calculator = "0.1.0"
Here's a basic example of how to use the library:
use message_segment_calculator::{SegmentedMessage, Encoding};
fn main() {
let message = "Hello, world! 👋";
let encoding = Encoding::Auto;
let smart_encoding = true;
match SegmentedMessage::new(message, encoding, smart_encoding) {
Ok(segmented_message) => {
println!("Encoding: {:?}", segmented_message.get_encoding_name());
println!("Number of segments: {}", segmented_message.segments_count());
println!("Total size in bits: {}", segmented_message.total_size());
}
Err(e) => println!("Error: {}", e),
}
}
The main struct is SegmentedMessage
. Here are some of its key methods:
- Initializes struct with a given message
- Returns the detected encoding
- Returns the total message size in bits
- Returns the message size in bits (minus UDH)
- Returns count of segments
- Returns vector of all UCS2 characters detected in the message
The other structs are exposed and can be used for their specific operations.
A simple CLI tool is included and can be accessed with cargo run -- "Message
. It will print out the statistics for a
given string.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE.md file for details.