iridium_units/lib.rs
1//! # iridium-units
2//!
3//! A high-performance runtime dimensional analysis library for Rust.
4//!
5//! This library provides physical units and quantities with automatic dimensional
6//! analysis at runtime. It supports SI, CGS, and astrophysical unit systems,
7//! as well as equivalencies for converting between different physical domains
8//! (e.g., wavelength to frequency).
9//!
10//! ## Quick Start
11//!
12//! ```
13//! use iridium_units::prelude::*;
14//!
15//! // Create quantities by multiplying values with units
16//! // Units are const values — use them directly
17//! let distance = 100.0 * M;
18//! let time = 9.58 * S;
19//! let speed = &distance / &time;
20//!
21//! // Convert between units
22//! let speed_kmh = speed.to(&(KM / H)).unwrap();
23//! println!("{}", speed_kmh); // ~37.58 km / h
24//!
25//! // Dimensional analysis is automatic
26//! let energy = 10.0 * KG * M.pow(2) / S.pow(2);
27//! let in_joules = energy.to(J).unwrap();
28//! ```
29//!
30//! ## Unit Systems
31//!
32//! The library provides units from multiple systems:
33//!
34//! - **SI units**: meter, second, kilogram, ampere, kelvin, etc.
35//! - **CGS units**: centimeter, gram, dyne, erg, etc.
36//! - **Astrophysical units**: parsec, AU, solar mass, light year, etc.
37//! - **Imperial units**: mile, foot, inch, pound, etc.
38//!
39//! ## Parsing Units and Quantities
40//!
41//! Units and quantities can be parsed from strings with flexible syntax support:
42//!
43//! ```no_run
44//! # #[cfg(feature = "astrophysics")]
45//! # fn main() {
46//! use iridium_units::prelude::*;
47//! use std::str::FromStr;
48//!
49//! // Parse units from strings
50//! let meter = Unit::from_str("m").unwrap();
51//! let velocity_unit = Unit::from_str("km/s").unwrap();
52//! let accel_unit = Unit::from_str("m/s^2").unwrap();
53//!
54//! // Parse quantities (value + unit)
55//! let distance: Quantity = "100 km".parse().unwrap();
56//! let speed: Quantity = "9.8 m/s^2".parse().unwrap();
57//!
58//! // Unicode and technical formats are supported
59//! let area = parse_unit("m²").unwrap(); // Unicode superscript
60//! let wavelength = parse_unit("µm").unwrap(); // Unicode micro
61//! let flux = parse_unit("erg/cm²/s").unwrap(); // Astrophysical
62//! let latex = parse_unit("kg m^{2} / s^{2}").unwrap(); // LaTeX braces
63//! let natural = parse_unit("km per hour").unwrap(); // Natural language
64//! let astro = parse_unit("M_sun").unwrap(); // Astrophysical subscripts
65//! let parens = parse_unit("(kg m)/s^2").unwrap(); // Parentheses
66//! # }
67//! # #[cfg(not(feature = "astrophysics"))]
68//! # fn main() {}
69//! ```
70//!
71//! See the [`parsing`] module for comprehensive documentation of supported formats.
72//!
73//! ## Custom Unit Registry
74//!
75//! For applications needing custom units (e.g., loaded from a database):
76//!
77//! ```
78//! use iridium_units::prelude::*;
79//!
80//! let registry = UnitRegistry::with_builtins()
81//! .with_unit(&["my_unit", "mu"], Unit::from(M));
82//!
83//! let unit = registry.parse_unit("my_unit").unwrap();
84//! ```
85//!
86//! ## Equivalencies
87//!
88//! Some physical quantities can be converted through physical laws even though
89//! they have different dimensions:
90//!
91//! ```no_run
92//! # #[cfg(feature = "astrophysics")]
93//! # fn main() {
94//! use iridium_units::prelude::*;
95//! use iridium_units::equivalencies::spectral;
96//!
97//! let wavelength = 500.0 * NM;
98//! let frequency = wavelength.to_equiv(HZ, spectral()).unwrap();
99//! # }
100//! # #[cfg(not(feature = "astrophysics"))]
101//! # fn main() {}
102//! ```
103
104pub mod dimension;
105pub mod error;
106pub mod quantity;
107pub mod unit;
108
109pub mod constants;
110pub mod equivalencies;
111pub mod parsing;
112pub mod systems;
113
114// Re-export main types
115pub use dimension::{Dimension, Rational16};
116pub use error::{UnitError, UnitResult};
117pub use quantity::{batch_convert, batch_convert_into, conversion_factor, Quantity};
118pub use unit::base::BaseUnit;
119pub use unit::Unit;
120
121// Re-export parsing functions and types
122pub use parsing::{lookup_unit, parse_quantity, parse_unit, register_unit, UnitRegistry};
123
124/// Prelude module for convenient imports.
125///
126/// This module re-exports the most commonly used types and units.
127///
128/// ```
129/// use iridium_units::prelude::*;
130/// ```
131pub mod prelude {
132 pub use crate::dimension::{Dimension, Rational16};
133 pub use crate::error::{UnitError, UnitResult};
134 pub use crate::quantity::{batch_convert, batch_convert_into, conversion_factor, Quantity};
135 pub use crate::unit::base::BaseUnit;
136 pub use crate::unit::Unit;
137
138 // Re-export parsing functions and types
139 pub use crate::parsing::{
140 lookup_unit, parse_quantity, parse_unit, register_unit, UnitRegistry,
141 };
142
143 // Re-export common SI units
144 pub use crate::systems::si::{
145 // Base units
146 A,
147 // Derived units
148 C,
149 CD,
150 // Length
151 CM,
152 // Time
153 DAY,
154 // Temperature
155 DEG_C,
156 DEG_F,
157 F,
158 GHZ,
159 H,
160 HZ,
161 J,
162 K,
163 KG,
164 KHZ,
165 KM,
166 M,
167 MHZ,
168 MIN,
169 MM,
170 MOL,
171 MS,
172 N,
173 NM,
174 NS,
175 OHM,
176 PA,
177 RAD,
178 S,
179 SR,
180 THZ,
181 UM,
182 US,
183 V,
184 W,
185 YR,
186 };
187
188 // Re-export astrophysical units
189 #[cfg(feature = "astrophysics")]
190 pub use crate::systems::astrophysical::{
191 ANGSTROM, AU, BARN, DYN, ERG, GAUSS, JANSKY, LIGHT_YEAR, PARSEC, SOLAR_LUMINOSITY,
192 SOLAR_MASS, SOLAR_RADIUS,
193 };
194
195 // Re-export CGS units
196 #[cfg(feature = "cgs")]
197 pub use crate::systems::cgs::{CENTIMETER, DYNE, ERG as ERG_CGS, GRAM};
198
199 // Re-export imperial units
200 pub use crate::systems::imperial::{FOOT, INCH, MILE, POUND, YARD};
201}