byte_unit/lib.rs
1/*!
2# Byte Unit
3
4A library for interaction with units of bytes.
5
6The units are **B** for 1 byte, **KB** for 1000 bytes, **MiB** for 1048576 bytes, **GB** for 1000000000 bytes, etc, and up to **E** or **Y** (if the `u128` feature is enabled).
7
8## Usage
9
10The data types for storing the size in bits/bytes are `u64` by default, meaning the highest supported unit is up to **E**. If the `u128` feature is enabled, the data types will use `u128`, increasing the highest supported unit up to **Y**.
11
12### Unit
13
14The enum `Unit` can be used for representing the unit of bits/bytes.
15
16```rust
17use byte_unit::Unit;
18
19assert_eq!("KB", Unit::KB.as_str());
20assert_eq!("MiB", Unit::MiB.as_str());
21
22assert_eq!(Unit::KB, Unit::parse_str("K", true, true).unwrap());
23assert_eq!(Unit::Kbit, Unit::parse_str("K", true, false).unwrap());
24
25assert_eq!(Unit::KB, Unit::parse_str("KB", true, true).unwrap());
26assert_eq!(Unit::KB, Unit::parse_str("Kb", true, true).unwrap());
27assert_eq!(Unit::Kbit, Unit::parse_str("Kbit", true, true).unwrap());
28
29assert_eq!(Unit::KB, Unit::parse_str("KB", false, true).unwrap());
30assert_eq!(Unit::Kbit, Unit::parse_str("Kb", false, true).unwrap());
31```
32
33### Byte
34
35The `Byte` struct can be used for representing a size in bytes.
36
37The `from_*` associated functions can be used to create a `Byte` instance from different data types. The `as_*` methods can retrieve the size as a primitive type.
38
39```rust
40# #[cfg(feature = "byte")]
41# {
42use byte_unit::{Byte, Unit};
43
44assert_eq!(15000, Byte::from_u64(15000).as_u64());
45assert_eq!(15000, Byte::from_u64_with_unit(15, Unit::KB).unwrap().as_u64());
46# }
47```
48
49You can also parse a string to create a `Byte` instance.
50
51```rust
52# #[cfg(feature = "byte")]
53# {
54use byte_unit::Byte;
55
56assert_eq!(50840000, Byte::parse_str("50.84 MB", true).unwrap().as_u64());
57# }
58```
59
60A `Byte` instance can be formatted to string precisely. For more detailed usage, please refer to the implementation documentation of `Display::fmt` for `Byte`.
61
62```rust
63# #[cfg(feature = "byte")]
64# {
65use byte_unit::Byte;
66
67let byte = Byte::from_u64(15500);
68
69assert_eq!("15500", byte.to_string());
70
71assert_eq!("15.5 KB", format!("{byte:#}"));
72assert_eq!("15500 B", format!("{byte:#.0}"));
73# }
74```
75
76#### Arithmetic
77
78There are `add`, `subtract`, `multiply`, and `divide` methods.
79
80```rust
81# #[cfg(feature = "byte")]
82# {
83use byte_unit::Byte;
84
85let a = Byte::from_u64(15500);
86let b = Byte::from_u64(500);
87
88assert_eq!(16000, a.add(b).unwrap().as_u64());
89assert_eq!(15000, a.subtract(b).unwrap().as_u64());
90
91assert_eq!(31000, a.multiply(2).unwrap().as_u64());
92assert_eq!(3100, a.divide(5).unwrap().as_u64());
93# }
94```
95
96#### Find Out an Appropriate Unit
97
98The `get_exact_unit` and `get_recoverable_unit` methods is useful if you want to find out a unit that is appropriate for a `Byte` instance.
99
100```rust
101# #[cfg(feature = "byte")]
102# {
103use byte_unit::{Byte, Unit};
104
105let byte = Byte::from_u64(50840000);
106
107assert_eq!((50840, Unit::KB), byte.get_exact_unit(false));
108assert_eq!((50.84f64.try_into().unwrap(), Unit::MB), byte.get_recoverable_unit(false, 2));
109assert_eq!((50840.into(), Unit::KB), byte.get_recoverable_unit(false, 0));
110# }
111```
112
113#### AdjustedByte
114
115The `AdjustedByte` struct can be used for roughly representing a size of bytes with a unit.
116
117To change the unit of a `Byte` instance, you can use the `get_adjusted_unit` method.
118
119An `AdjustedByte` instance can be formatted to string. For more detailed usage, please refer to the implementation documentation of `Display::fmt` for `AdjustedByte`.
120
121```rust
122# #[cfg(feature = "byte")]
123# {
124use byte_unit::{Byte, Unit};
125
126let byte = Byte::parse_str("123KiB", true).unwrap();
127
128let adjusted_byte = byte.get_adjusted_unit(Unit::KB);
129
130assert_eq!("125.952 KB", adjusted_byte.to_string());
131assert_eq!("125.95 KB", format!("{adjusted_byte:.2}"));
132# }
133```
134
135The `get_appropriate_unit` method can be used to automatically find an appropriate unit for creating an `AdjustedByte` instance.
136
137```rust
138# #[cfg(feature = "byte")]
139# {
140use byte_unit::{Byte, Unit, UnitType};
141
142let byte = Byte::from_u64(1500000);
143
144let adjusted_byte = byte.get_appropriate_unit(UnitType::Binary);
145
146assert_eq!("1.43 MiB", format!("{adjusted_byte:.2}"));
147# }
148```
149
150### Bit
151
152The `Bit` struct can be used for representing a size in bits.
153
154The `bit` feature must be enabled.
155
156Usage of the `Bit` struct and the `Byte` struct is very similar. Also, There is the `AdjustedBit` struct. The difference lies in the fact that the `parse_str` method of the `Bit` struct cannot be configured to ignore case; it always does not ignore case.
157
158```rust
159# #[cfg(feature = "bit")]
160# {
161use byte_unit::{Bit, Unit};
162
163let bit = Bit::parse_str("123Kib").unwrap();
164
165let adjusted_bit = bit.get_adjusted_unit(Unit::Kbit);
166
167assert_eq!("125.952 Kb", adjusted_bit.to_string());
168assert_eq!("125.95 Kb", format!("{adjusted_bit:.2}"));
169# }
170```
171
172## No Std
173
174Disable the default features to compile this crate without std.
175
176```toml
177[dependencies.byte-unit]
178version = "*"
179default-features = false
180features = ["byte"]
181```
182
183## Serde Support
184
185Enable the `serde` feature to support the serde framework.
186
187```toml
188[dependencies.byte-unit]
189version = "*"
190features = ["serde"]
191```
192
193## Rocket Support
194
195Enable the `rocket` feature to support the Rocket framework.
196
197```toml
198[dependencies.byte-unit]
199version = "*"
200features = ["rocket"]
201```
202*/
203
204#![cfg_attr(not(feature = "std"), no_std)]
205#![cfg_attr(docsrs, feature(doc_auto_cfg))]
206
207#[cfg(all(feature = "serde", any(feature = "byte", feature = "bit")))]
208#[macro_use]
209extern crate alloc;
210#[cfg(feature = "rust_decimal")]
211pub extern crate rust_decimal;
212
213#[cfg(feature = "bit")]
214mod bit;
215#[cfg(feature = "byte")]
216mod byte;
217mod common;
218mod errors;
219mod unit;
220
221#[cfg(feature = "bit")]
222pub use bit::*;
223#[cfg(feature = "byte")]
224pub use byte::*;
225pub use errors::*;
226pub use unit::*;