parse-frequency
A simple, no-nonsense Rust library for parsing frequency strings like "1.5GHz" or "100 kHz" into strongly-typed frequency values. Case insensitive, of course.
Supports serialization, deserialization, CLI parsing, and conversions between Hz, kHz, MHz, and GHz.
Features
- Parse human-friendly strings like
"1GHz","2.5 MHz", or"42 kHz"into aFrequencytype - Convert between Hz, kHz, MHz, and GHz with ease
Displayimplementation (e.g.,"2.50 MHz")- Convert to
std::time::Duration(period) #[derive(Debug, Copy, Clone, ...)]with strong type guaranteesSend + Syncsupport for thread-safe usage in multithreaded environments- Optional support for the following features:
Example
use Frequency;
Installation
Add this to your Cargo.toml:
[]
= "2.0"
Enable optional features:
[]
= "1.0"
= ["serde", "clap", "num-traits", "schemars", "time", "chrono"]
Quick Start
Parse from a string
use Frequency;
let freq: Frequency = "2.5GHz".parse.unwrap;
assert_eq!;
Convert between units
use ;
let f = from_khz;
assert_eq!;
assert_eq!;
Format for display
Note that due to the 2 digit precision limitation, the result is rounded when displaying.
let f = from_mhz;
println!; // -> "1.34 GHz"
Derive a period as Duration
let f = from_ghz;
let duration = f.as_duration;
assert_eq!; // 1 GHz → 1 nanosecond period
Optional Integrations
parse-frequency a number of optional features that enable seamless integration with commonly used Rust libraries. Enable these via Cargo features.
serde
Enable the serde feature to serialize and deserialize Frequency as human-readable strings:
= { = "...", = ["serde"] }
use Frequency;
use ;
This allows values like:
clap
Enable the clap feature to use Frequency in CLI arguments:
= { = "...", = ["clap"] }
use Parser;
use Frequency;
enum-traits
Enable num-traits to use Frequency in generic numeric code (e.g. scientific, DSP, or math contexts):
= { = "...", = ["num-traits"] }
Implements:
Zero,OneNum,FromStrRadixMul,Div,Rem
Note: Arithmetic is defined in terms of absolute frequency values. Multiplying or dividing two
Frequencyvalues may not make semantic sense but is supported for compatibility.
chrono
Enable the chrono feature to convert a Frequency into a time period:
= { = "...", = ["chrono"] }
use Frequency;
let freq = from_ghz;
let period = freq.as_chrono_duration; // chrono::Duration of 1 ns
Handles both low and high frequencies safely using nanosecond/picosecond precision.
time
Enable the time feature to convert a Frequency into a time::Duration:
= { = "...", = ["time"] }
use Frequency;
let freq = from_mhz;
let duration = freq.as_time_duration; // time::Duration of 1000 ns
schemars
Enable the schemars feature to use Frequency with OpenAPI / JSON schema generation:
= { = "...", = ["schemars"] }
use Frequency;
use JsonSchema;
Generates schema like:
Constants
For convenience, the following constants are available:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
Error Handling
The Frequency::from_str and parse_frequency functions return a custom error enum:
match "bad input".
Example error variants:
Error::UnknownUnit("abc")Error::InvalidValue("GHz")