From 3ef52dac42744a380ba6fbfcf989febb52c6f39d Mon Sep 17 00:00:00 2001 From: undef1nd Date: Wed, 12 Aug 2020 07:07:00 +0000 Subject: [PATCH] Bug 1631722 - Vendor sfv crate,r=valentin *** Vendor Differential Revision: https://phabricator.services.mozilla.com/D83502 --- Cargo.lock | 27 + .../rust/data-encoding/.cargo-checksum.json | 1 + third_party/rust/data-encoding/Cargo.toml | 30 + third_party/rust/data-encoding/LICENSE | 22 + third_party/rust/data-encoding/README.md | 111 + third_party/rust/data-encoding/src/lib.rs | 2387 +++++++++++++ .../rust/rust_decimal/.cargo-checksum.json | 1 + .../rust/rust_decimal/CODE_OF_CONDUCT.md | 46 + third_party/rust/rust_decimal/CONTRIBUTING.md | 40 + third_party/rust/rust_decimal/Cargo.toml | 82 + third_party/rust/rust_decimal/LICENSE | 21 + third_party/rust/rust_decimal/README.md | 84 + third_party/rust/rust_decimal/VERSION.md | 274 ++ .../rust/rust_decimal/benches/lib_benches.rs | 194 + third_party/rust/rust_decimal/rustfmt.toml | 1 + third_party/rust/rust_decimal/src/decimal.rs | 3161 +++++++++++++++++ third_party/rust/rust_decimal/src/error.rs | 31 + third_party/rust/rust_decimal/src/lib.rs | 56 + third_party/rust/rust_decimal/src/postgres.rs | 856 +++++ .../rust/rust_decimal/src/serde_types.rs | 218 ++ .../rust/rust_decimal/tests/decimal_tests.rs | 1633 +++++++++ third_party/rust/sfv/.cargo-checksum.json | 1 + third_party/rust/sfv/Cargo.toml | 46 + third_party/rust/sfv/LICENSE | 373 ++ third_party/rust/sfv/README.md | 10 + third_party/rust/sfv/benches/bench.rs | 171 + third_party/rust/sfv/src/lib.rs | 389 ++ third_party/rust/sfv/src/parser.rs | 477 +++ third_party/rust/sfv/src/ref_serializer.rs | 310 ++ third_party/rust/sfv/src/serializer.rs | 320 ++ third_party/rust/sfv/src/test_parser.rs | 850 +++++ third_party/rust/sfv/src/test_serializer.rs | 531 +++ third_party/rust/sfv/src/utils.rs | 44 + 33 files changed, 12798 insertions(+) create mode 100644 third_party/rust/data-encoding/.cargo-checksum.json create mode 100644 third_party/rust/data-encoding/Cargo.toml create mode 100644 third_party/rust/data-encoding/LICENSE create mode 100644 third_party/rust/data-encoding/README.md create mode 100644 third_party/rust/data-encoding/src/lib.rs create mode 100644 third_party/rust/rust_decimal/.cargo-checksum.json create mode 100644 third_party/rust/rust_decimal/CODE_OF_CONDUCT.md create mode 100644 third_party/rust/rust_decimal/CONTRIBUTING.md create mode 100644 third_party/rust/rust_decimal/Cargo.toml create mode 100644 third_party/rust/rust_decimal/LICENSE create mode 100644 third_party/rust/rust_decimal/README.md create mode 100644 third_party/rust/rust_decimal/VERSION.md create mode 100644 third_party/rust/rust_decimal/benches/lib_benches.rs create mode 100644 third_party/rust/rust_decimal/rustfmt.toml create mode 100644 third_party/rust/rust_decimal/src/decimal.rs create mode 100644 third_party/rust/rust_decimal/src/error.rs create mode 100644 third_party/rust/rust_decimal/src/lib.rs create mode 100644 third_party/rust/rust_decimal/src/postgres.rs create mode 100644 third_party/rust/rust_decimal/src/serde_types.rs create mode 100644 third_party/rust/rust_decimal/tests/decimal_tests.rs create mode 100644 third_party/rust/sfv/.cargo-checksum.json create mode 100644 third_party/rust/sfv/Cargo.toml create mode 100644 third_party/rust/sfv/LICENSE create mode 100644 third_party/rust/sfv/README.md create mode 100644 third_party/rust/sfv/benches/bench.rs create mode 100644 third_party/rust/sfv/src/lib.rs create mode 100644 third_party/rust/sfv/src/parser.rs create mode 100644 third_party/rust/sfv/src/ref_serializer.rs create mode 100644 third_party/rust/sfv/src/serializer.rs create mode 100644 third_party/rust/sfv/src/test_parser.rs create mode 100644 third_party/rust/sfv/src/test_serializer.rs create mode 100644 third_party/rust/sfv/src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index 72831d0f7de4..03fa8e44a98d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1072,6 +1072,12 @@ dependencies = [ "syn", ] +[[package]] +name = "data-encoding" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d0e2d24e5ee3b23a01de38eefdcd978907890701f08ffffd4cb457ca4ee8d6" + [[package]] name = "dbus" version = "0.6.4" @@ -4230,6 +4236,16 @@ dependencies = [ "sha2", ] +[[package]] +name = "rust_decimal" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95ba36e8c41bf675947e200af432325f332f60a0aea0ef2dc456636c2f6037d7" +dependencies = [ + "num-traits", + "serde", +] + [[package]] name = "rustc-demangle" version = "0.1.8" @@ -4446,6 +4462,17 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "sfv" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13ed1dd5a626253083678d21b5c38dd94f8717b961d4b7469eb96b41173cc148" +dependencies = [ + "data-encoding", + "indexmap", + "rust_decimal", +] + [[package]] name = "sha-1" version = "0.8.1" diff --git a/third_party/rust/data-encoding/.cargo-checksum.json b/third_party/rust/data-encoding/.cargo-checksum.json new file mode 100644 index 000000000000..2b2462e9e185 --- /dev/null +++ b/third_party/rust/data-encoding/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"9897a27476c1fa6def578e2c860dc0c62cf774927a635104c0b327550fde6b98","LICENSE":"d9ae65b8784809f801d67a1805ba55e5c911978aae6173c85f4ce9bccfaa7373","README.md":"b05c574a5e4c467a37e691a76546961ce1970afee5b2eb9a8751d97030d6eba3","src/lib.rs":"13252a2117cb3aaf803b307b577ff550be030a8cb8d2f4dada9b5df13e355611"},"package":"d4d0e2d24e5ee3b23a01de38eefdcd978907890701f08ffffd4cb457ca4ee8d6"} \ No newline at end of file diff --git a/third_party/rust/data-encoding/Cargo.toml b/third_party/rust/data-encoding/Cargo.toml new file mode 100644 index 000000000000..4f9a0c5c8610 --- /dev/null +++ b/third_party/rust/data-encoding/Cargo.toml @@ -0,0 +1,30 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +edition = "2018" +name = "data-encoding" +version = "2.3.0" +authors = ["Julien Cretin "] +include = ["Cargo.toml", "LICENSE", "README.md", "src/lib.rs"] +description = "Efficient and customizable data-encoding functions like base64, base32, and hex" +documentation = "https://docs.rs/data-encoding" +readme = "README.md" +keywords = ["no_std", "base64", "base32", "hex"] +categories = ["encoding", "no-std"] +license = "MIT" +repository = "https://github.com/ia0/data-encoding" + +[features] +alloc = [] +default = ["std"] +std = ["alloc"] diff --git a/third_party/rust/data-encoding/LICENSE b/third_party/rust/data-encoding/LICENSE new file mode 100644 index 000000000000..8e376ebd45c2 --- /dev/null +++ b/third_party/rust/data-encoding/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015-2017 Julien Cretin +Copyright (c) 2017 Google Inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/rust/data-encoding/README.md b/third_party/rust/data-encoding/README.md new file mode 100644 index 000000000000..93b5a3cf0058 --- /dev/null +++ b/third_party/rust/data-encoding/README.md @@ -0,0 +1,111 @@ +[![Build Status][travis_badge]][travis] +[![Build Status][appveyor_badge]][appveyor] +[![Coverage Status][coveralls_badge]][coveralls] + +## Common use-cases + +This library provides the following common encodings: + +- `HEXLOWER`: lowercase hexadecimal +- `HEXLOWER_PERMISSIVE`: lowercase hexadecimal with case-insensitive decoding +- `HEXUPPER`: uppercase hexadecimal +- `HEXUPPER_PERMISSIVE`: uppercase hexadecimal with case-insensitive decoding +- `BASE32`: RFC4648 base32 +- `BASE32_NOPAD`: RFC4648 base32 without padding +- `BASE32_DNSSEC`: RFC5155 base32 +- `BASE32_DNSCURVE`: DNSCurve base32 +- `BASE32HEX`: RFC4648 base32hex +- `BASE32HEX_NOPAD`: RFC4648 base32hex without padding +- `BASE64`: RFC4648 base64 +- `BASE64_NOPAD`: RFC4648 base64 without padding +- `BASE64_MIME`: RFC2045-like base64 +- `BASE64URL`: RFC4648 base64url +- `BASE64URL_NOPAD`: RFC4648 base64url without padding + +Typical usage looks like: + +```rust +// allocating functions +BASE64.encode(&input_to_encode) +HEXLOWER.decode(&input_to_decode) +// in-place functions +BASE32.encode_mut(&input_to_encode, &mut encoded_output) +BASE64_URL.decode_mut(&input_to_decode, &mut decoded_output) +``` + +See the [documentation] or the [changelog] for more details. + +## Custom use-cases + +This library also provides the possibility to define custom little-endian ASCII +base-conversion encodings for bases of size 2, 4, 8, 16, 32, and 64 (for which +all above use-cases are particular instances). It supports: + +- padded and unpadded encodings +- canonical encodings (e.g. trailing bits are checked) +- in-place encoding and decoding functions +- partial decoding functions (e.g. for error recovery) +- character translation (e.g. for case-insensitivity) +- most and least significant bit-order +- ignoring characters when decoding (e.g. for skipping newlines) +- wrapping the output when encoding + +The typical definition of a custom encoding looks like: + +```rust +lazy_static! { + static ref HEX: Encoding = { + let mut spec = Specification::new(); + spec.symbols.push_str("0123456789abcdef"); + spec.translate.from.push_str("ABCDEF"); + spec.translate.to.push_str("abcdef"); + spec.encoding().unwrap() + }; + static ref BASE64: Encoding = { + let mut spec = Specification::new(); + spec.symbols.push_str( + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); + spec.padding = Some('='); + spec.encoding().unwrap() + }; +} +``` + +You may also use the [macro] library to define a compile-time custom encoding: + +```rust +const HEX: Encoding = new_encoding!{ + symbols: "0123456789abcdef", + translate_from: "ABCDEF", + translate_to: "abcdef", +}; +const BASE64: Encoding = new_encoding!{ + symbols: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", + padding: '=', +}; +``` + +See the [documentation] or the [changelog] for more details. + +## Performance + +The performance of the encoding and decoding functions (for both common and +custom encodings) are similar to existing implementations in C, Rust, and other +high-performance languages (see how to run the benchmarks on [github]). + +## Swiss-knife binary + +This crate is a library. If you are looking for the [binary] using this library, +see the installation instructions on [github]. + +[appveyor]: https://ci.appveyor.com/project/ia0/data-encoding +[appveyor_badge]:https://ci.appveyor.com/api/projects/status/wm4ga69xnlriukhl/branch/master?svg=true +[binary]: https://crates.io/crates/data-encoding-bin +[changelog]: https://github.com/ia0/data-encoding/blob/master/lib/CHANGELOG.md +[coveralls]: https://coveralls.io/github/ia0/data-encoding?branch=master +[coveralls_badge]: https://coveralls.io/repos/github/ia0/data-encoding/badge.svg?branch=master +[documentation]: https://docs.rs/data-encoding +[github]: https://github.com/ia0/data-encoding +[macro]: https://crates.io/crates/data-encoding-macro +[travis]: https://travis-ci.org/ia0/data-encoding +[travis_badge]: https://travis-ci.org/ia0/data-encoding.svg?branch=master diff --git a/third_party/rust/data-encoding/src/lib.rs b/third_party/rust/data-encoding/src/lib.rs new file mode 100644 index 000000000000..e39f1c02bead --- /dev/null +++ b/third_party/rust/data-encoding/src/lib.rs @@ -0,0 +1,2387 @@ +//! Efficient and customizable data-encoding functions like base64, base32, and hex +//! +//! This [crate] provides little-endian ASCII base-conversion encodings for +//! bases of size 2, 4, 8, 16, 32, and 64. It supports: +//! +//! - padded and unpadded encodings +//! - canonical encodings (e.g. trailing bits are checked) +//! - in-place encoding and decoding functions +//! - partial decoding functions (e.g. for error recovery) +//! - character translation (e.g. for case-insensitivity) +//! - most and least significant bit-order +//! - ignoring characters when decoding (e.g. for skipping newlines) +//! - wrapping the output when encoding +//! - no-std with `std` and `alloc` features +//! +//! The performance of the encoding and decoding functions are similar to +//! existing implementations (see how to run the benchmarks on [github]). +//! +//! This is the library documentation. If you are looking for the [binary], see +//! the installation instructions on [github]. +//! +//! # Examples +//! +//! This crate provides predefined encodings as [constants]. These constants are +//! of type [`Encoding`]. This type provides encoding and decoding functions +//! with in-place or allocating variants. Here is an example using the +//! allocating encoding function of [base64]: +//! +//! ```rust +//! use data_encoding::BASE64; +//! assert_eq!(BASE64.encode(b"Hello world"), "SGVsbG8gd29ybGQ="); +//! ``` +//! +//! Here is an example using the in-place decoding function of [base32]: +//! +//! ```rust +//! use data_encoding::BASE32; +//! let input = b"JBSWY3DPEB3W64TMMQ======"; +//! let mut output = vec![0; BASE32.decode_len(input.len()).unwrap()]; +//! let len = BASE32.decode_mut(input, &mut output).unwrap(); +//! assert_eq!(&output[0 .. len], b"Hello world"); +//! ``` +//! +//! You are not limited to the predefined encodings. You may define your own +//! encodings (with the same correctness and performance properties as the +//! predefined ones) using the [`Specification`] type: +//! +//! ```rust +//! use data_encoding::Specification; +//! let hex = { +//! let mut spec = Specification::new(); +//! spec.symbols.push_str("0123456789abcdef"); +//! spec.encoding().unwrap() +//! }; +//! assert_eq!(hex.encode(b"hello"), "68656c6c6f"); +//! ``` +//! +//! If you use the [`lazy_static`] crate, you can define a global encoding: +//! +//! ```rust,ignore +//! lazy_static! { +//! static ref HEX: Encoding = { +//! let mut spec = Specification::new(); +//! spec.symbols.push_str("0123456789abcdef"); +//! spec.translate.from.push_str("ABCDEF"); +//! spec.translate.to.push_str("abcdef"); +//! spec.encoding().unwrap() +//! }; +//! } +//! ``` +//! +//! You may also use the [macro] library to define a compile-time custom encoding: +//! +//! ```rust,ignore +//! const HEX: Encoding = new_encoding!{ +//! symbols: "0123456789abcdef", +//! translate_from: "ABCDEF", +//! translate_to: "abcdef", +//! }; +//! const BASE64: Encoding = new_encoding!{ +//! symbols: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", +//! padding: '=', +//! }; +//! ``` +//! +//! # Properties +//! +//! The [base16], [base32], [base32hex], [base64], and [base64url] predefined +//! encodings are conform to [RFC4648]. +//! +//! In general, the encoding and decoding functions satisfy the following +//! properties: +//! +//! - They are deterministic: their output only depends on their input +//! - They have no side-effects: they do not modify a hidden mutable state +//! - They are correct: encoding then decoding gives the initial data +//! - They are canonical (unless [`is_canonical`] returns false): decoding then +//! encoding gives the initial data +//! +//! This last property is usually not satisfied by common base64 implementations +//! (like the `rustc-serialize` crate, the `base64` crate, or the `base64` GNU +//! program). This is a matter of choice and this crate has made the choice to +//! let the user choose. Support for canonical encoding as described by the +//! [RFC][canonical] is provided. But it is also possible to disable checking +//! trailing bits, to add characters translation, to decode concatenated padded +//! inputs, and to ignore some characters. +//! +//! Since the RFC specifies the encoding function on all inputs and the decoding +//! function on all possible encoded outputs, the differences between +//! implementations come from the decoding function which may be more or less +//! permissive. In this crate, the decoding function of canonical encodings +//! rejects all inputs that are not a possible output of the encoding function. +//! Here are some concrete examples of decoding differences between this crate, +//! the `rustc-serialize` crate, the `base64` crate, and the `base64` GNU +//! program: +//! +//! | Input | `data-encoding` | `rustc` | `base64` | GNU `base64` | +//! | ---------- | --------------- | -------- | -------- | ------------- | +//! | `AAB=` | `Trailing(2)` | `[0, 0]` | `Err(2)` | `\x00\x00` | +//! | `AA\nB=` | `Length(4)` | `[0, 0]` | `Length` | `\x00\x00` | +//! | `AAB` | `Length(0)` | `[0, 0]` | `Err(2)` | Invalid input | +//! | `A\rA\nB=` | `Length(4)` | `[0, 0]` | `Err(1)` | Invalid input | +//! | `-_\r\n` | `Symbol(0)` | `[251]` | `Err(0)` | Invalid input | +//! | `AA==AA==` | `[0, 0]` | `Err` | `Err(2)` | `\x00\x00` | +//! +//! We can summarize these discrepancies as follows: +//! +//! | Discrepancy | `data-encoding` | `rustc` | `base64` | GNU `base64` | +//! | ----------- | --------------- | ------- | -------- | ------------ | +//! | Check trailing bits | Yes | No | No | No | +//! | Ignored characters | None | `\r` and `\n` | None | `\n` | +//! | Translated characters | None | `-_` mapped to `+/` | None | None | +//! | Check padding | Yes | No | No | Yes | +//! | Support concatenated input | Yes | No | No | Yes | +//! +//! This crate permits to disable checking trailing bits. It permits to ignore +//! some characters. It permits to translate characters. It permits to use +//! unpadded encodings. However, for padded encodings, support for concatenated +//! inputs cannot be disabled. This is simply because it doesn't make sense to +//! use padding if it is not to support concatenated inputs. +//! +//! # Migration +//! +//! The [changelog] describes the changes between v1 and v2. Here are the +//! migration steps for common usage: +//! +//! | v1 | v2 | +//! | --------------------------- | --------------------------- | +//! | `use data_encoding::baseNN` | `use data_encoding::BASENN` | +//! | `baseNN::function` | `BASENN.method` | +//! | `baseNN::function_nopad` | `BASENN_NOPAD.method` | +//! +//! [`Encoding`]: struct.Encoding.html +//! [`Specification`]: struct.Specification.html +//! [`is_canonical`]: struct.Encoding.html#method.is_canonical +//! [`lazy_static`]: https://crates.io/crates/lazy_static +//! [RFC4648]: https://tools.ietf.org/html/rfc4648 +//! [base16]: constant.HEXUPPER.html +//! [base32]: constant.BASE32.html +//! [base32hex]: constant.BASE32HEX.html +//! [base64]: constant.BASE64.html +//! [base64url]: constant.BASE64URL.html +//! [binary]: https://crates.io/crates/data-encoding-bin +//! [canonical]: https://tools.ietf.org/html/rfc4648#section-3.5 +//! [changelog]: +//! https://github.com/ia0/data-encoding/blob/master/lib/CHANGELOG.md +//! [constants]: index.html#constants +//! [crate]: https://crates.io/crates/data-encoding +//! [github]: https://github.com/ia0/data-encoding +//! [macro]: https://crates.io/crates/data-encoding-macro + +#![cfg_attr(not(feature = "std"), no_std)] +#![warn(unused_results, missing_docs)] + +#[cfg(all(feature = "alloc", not(feature = "std")))] +extern crate alloc; + +#[cfg(all(feature = "alloc", not(feature = "std")))] +mod prelude { + pub use alloc::borrow::Cow; + pub use alloc::borrow::ToOwned; + pub use alloc::string::String; + pub use alloc::vec; + pub use alloc::vec::Vec; +} +#[cfg(feature = "std")] +mod prelude { + pub use std::borrow::Cow; +} + +#[cfg(not(feature = "std"))] +use core as std; +#[cfg(feature = "alloc")] +use prelude::*; + +macro_rules! check { + ($e: expr, $c: expr) => { + if !$c { + return Err($e); + } + }; +} + +trait Static: Copy { + fn val(self) -> T; +} + +macro_rules! define { + ($name: ident: $type: ty = $val: expr) => { + #[derive(Copy, Clone)] + struct $name; + impl Static<$type> for $name { + fn val(self) -> $type { + $val + } + } + }; +} + +define!(Bf: bool = false); +define!(Bt: bool = true); +define!(N1: usize = 1); +define!(N2: usize = 2); +define!(N3: usize = 3); +define!(N4: usize = 4); +define!(N5: usize = 5); +define!(N6: usize = 6); + +#[derive(Copy, Clone)] +struct On; +impl Static> for On { + fn val(self) -> Option { + None + } +} + +#[derive(Copy, Clone)] +struct Os(T); +impl Static> for Os { + fn val(self) -> Option { + Some(self.0) + } +} + +macro_rules! dispatch { + (let $var: ident: bool = $val: expr; $($body: tt)*) => { + if $val { + let $var = Bt; dispatch!($($body)*) + } else { + let $var = Bf; dispatch!($($body)*) + } + }; + (let $var: ident: usize = $val: expr; $($body: tt)*) => { + match $val { + 1 => { let $var = N1; dispatch!($($body)*) }, + 2 => { let $var = N2; dispatch!($($body)*) }, + 3 => { let $var = N3; dispatch!($($body)*) }, + 4 => { let $var = N4; dispatch!($($body)*) }, + 5 => { let $var = N5; dispatch!($($body)*) }, + 6 => { let $var = N6; dispatch!($($body)*) }, + _ => panic!(), + } + }; + (let $var: ident: Option<$type: ty> = $val: expr; $($body: tt)*) => { + match $val { + None => { let $var = On; dispatch!($($body)*) }, + Some(x) => { let $var = Os(x); dispatch!($($body)*) }, + } + }; + ($body: expr) => { $body }; +} + +unsafe fn chunk_unchecked(x: &[u8], n: usize, i: usize) -> &[u8] { + debug_assert!((i + 1) * n <= x.len()); + let ptr = x.as_ptr().add(n * i); + std::slice::from_raw_parts(ptr, n) +} +unsafe fn chunk_mut_unchecked(x: &mut [u8], n: usize, i: usize) -> &mut [u8] { + debug_assert!((i + 1) * n <= x.len()); + let ptr = x.as_mut_ptr().add(n * i); + std::slice::from_raw_parts_mut(ptr, n) +} +unsafe fn as_array(x: &[u8]) -> &[u8; 256] { + debug_assert_eq!(x.len(), 256); + &*(x.as_ptr() as *const [u8; 256]) +} +fn div_ceil(x: usize, m: usize) -> usize { + (x + m - 1) / m +} +fn floor(x: usize, m: usize) -> usize { + x / m * m +} + +fn vectorize(n: usize, bs: usize, mut f: F) { + for k in 0 .. n / bs { + for i in k * bs .. (k + 1) * bs { + f(i); + } + } + for i in floor(n, bs) .. n { + f(i); + } +} + +/// Decoding error kind +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum DecodeKind { + /// Invalid length + Length, + /// Invalid symbol + Symbol, + /// Non-zero trailing bits + Trailing, + /// Invalid padding length + Padding, +} +#[cfg(feature = "std")] +impl std::fmt::Display for DecodeKind { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let description = match self { + DecodeKind::Length => "invalid length", + DecodeKind::Symbol => "invalid symbol", + DecodeKind::Trailing => "non-zero trailing bits", + DecodeKind::Padding => "invalid padding length", + }; + write!(f, "{}", description) + } +} + +/// Decoding error +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub struct DecodeError { + /// Error position + /// + /// This position is always a valid input position and represents the first + /// encountered error. + pub position: usize, + /// Error kind + pub kind: DecodeKind, +} +#[cfg(feature = "std")] +impl std::error::Error for DecodeError {} +#[cfg(feature = "std")] +impl std::fmt::Display for DecodeError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{} at {}", self.kind, self.position) + } +} + +/// Decoding error with partial result +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub struct DecodePartial { + /// Number of bytes read from input + /// + /// This number does not exceed the error position: `read <= + /// error.position`. + pub read: usize, + + /// Number of bytes written to output + /// + /// This number does not exceed the decoded length: `written <= + /// decode_len(read)`. + pub written: usize, + + /// Decoding error + pub error: DecodeError, +} + +const INVALID: u8 = 128; +const IGNORE: u8 = 129; +const PADDING: u8 = 130; + +fn order(msb: bool, n: usize, i: usize) -> usize { + if msb { + n - 1 - i + } else { + i + } +} +fn enc(bit: usize) -> usize { + debug_assert!(1 <= bit && bit <= 6); + match bit { + 1 | 2 | 4 => 1, + 3 | 6 => 3, + 5 => 5, + _ => unreachable!(), + } +} +fn dec(bit: usize) -> usize { + enc(bit) * 8 / bit +} + +fn encode_len>(bit: B, len: usize) -> usize { + div_ceil(8 * len, bit.val()) +} +fn encode_block, M: Static>( + bit: B, msb: M, symbols: &[u8; 256], input: &[u8], output: &mut [u8], +) { + debug_assert!(input.len() <= enc(bit.val())); + debug_assert_eq!(output.len(), encode_len(bit, input.len())); + let bit = bit.val(); + let msb = msb.val(); + let mut x = 0u64; + for (i, input) in input.iter().enumerate() { + x |= u64::from(*input) << (8 * order(msb, enc(bit), i)); + } + for (i, output) in output.iter_mut().enumerate() { + let y = x >> (bit * order(msb, dec(bit), i)); + *output = symbols[y as usize % 256]; + } +} +fn encode_mut, M: Static>( + bit: B, msb: M, symbols: &[u8; 256], input: &[u8], output: &mut [u8], +) { + debug_assert_eq!(output.len(), encode_len(bit, input.len())); + let enc = enc(bit.val()); + let dec = dec(bit.val()); + let n = input.len() / enc; + let bs = match bit.val() { + 5 => 2, + 6 => 4, + _ => 1, + }; + vectorize(n, bs, |i| { + let input = unsafe { chunk_unchecked(input, enc, i) }; + let output = unsafe { chunk_mut_unchecked(output, dec, i) }; + encode_block(bit, msb, symbols, input, output); + }); + encode_block(bit, msb, symbols, &input[enc * n ..], &mut output[dec * n ..]); +} + +// Fails if an input character does not translate to a symbol. The error is the +// lowest index of such character. The output is not written to. +fn decode_block, M: Static>( + bit: B, msb: M, values: &[u8; 256], input: &[u8], output: &mut [u8], +) -> Result<(), usize> { + debug_assert!(output.len() <= enc(bit.val())); + debug_assert_eq!(input.len(), encode_len(bit, output.len())); + let bit = bit.val(); + let msb = msb.val(); + let mut x = 0u64; + for j in 0 .. input.len() { + let y = values[input[j] as usize]; + check!(j, y < 1 << bit); + x |= u64::from(y) << (bit * order(msb, dec(bit), j)); + } + for (j, output) in output.iter_mut().enumerate() { + *output = (x >> (8 * order(msb, enc(bit), j))) as u8; + } + Ok(()) +} +// Fails if an input character does not translate to a symbol. The error `pos` +// is the lowest index of such character. The output is valid up to `pos / dec * +// enc` excluded. +fn decode_mut, M: Static>( + bit: B, msb: M, values: &[u8; 256], input: &[u8], output: &mut [u8], +) -> Result<(), usize> { + debug_assert_eq!(input.len(), encode_len(bit, output.len())); + let enc = enc(bit.val()); + let dec = dec(bit.val()); + let n = input.len() / dec; + for i in 0 .. n { + let input = unsafe { chunk_unchecked(input, dec, i) }; + let output = unsafe { chunk_mut_unchecked(output, enc, i) }; + decode_block(bit, msb, values, input, output).map_err(|e| dec * i + e)?; + } + decode_block(bit, msb, values, &input[dec * n ..], &mut output[enc * n ..]) + .map_err(|e| dec * n + e) +} +// Fails if there are non-zero trailing bits. +fn check_trail, M: Static>( + bit: B, msb: M, ctb: bool, values: &[u8; 256], input: &[u8], +) -> Result<(), ()> { + if 8 % bit.val() == 0 || !ctb { + return Ok(()); + } + let trail = bit.val() * input.len() % 8; + if trail == 0 { + return Ok(()); + } + let mut mask = (1 << trail) - 1; + if !msb.val() { + mask <<= bit.val() - trail; + } + check!((), values[input[input.len() - 1] as usize] & mask == 0); + Ok(()) +} +// Fails if the padding length is invalid. The error is the index of the first +// padding character. +fn check_pad>(bit: B, values: &[u8; 256], input: &[u8]) -> Result { + let bit = bit.val(); + debug_assert_eq!(input.len(), dec(bit)); + let is_pad = |x: &&u8| values[**x as usize] == PADDING; + let count = input.iter().rev().take_while(is_pad).count(); + let len = input.len() - count; + check!(len, len > 0 && bit * len % 8 < bit); + Ok(len) +} + +fn encode_base_len>(bit: B, len: usize) -> usize { + encode_len(bit, len) +} +fn encode_base, M: Static>( + bit: B, msb: M, symbols: &[u8; 256], input: &[u8], output: &mut [u8], +) { + debug_assert_eq!(output.len(), encode_base_len(bit, input.len())); + encode_mut(bit, msb, symbols, input, output); +} + +fn encode_pad_len, P: Static>>(bit: B, pad: P, len: usize) -> usize { + match pad.val() { + None => encode_base_len(bit, len), + Some(_) => div_ceil(len, enc(bit.val())) * dec(bit.val()), + } +} +fn encode_pad, M: Static, P: Static>>( + bit: B, msb: M, symbols: &[u8; 256], spad: P, input: &[u8], output: &mut [u8], +) { + let pad = match spad.val() { + None => return encode_base(bit, msb, symbols, input, output), + Some(pad) => pad, + }; + debug_assert_eq!(output.len(), encode_pad_len(bit, spad, input.len())); + let olen = encode_base_len(bit, input.len()); + encode_base(bit, msb, symbols, input, &mut output[.. olen]); + for output in output.iter_mut().skip(olen) { + *output = pad; + } +} + +fn encode_wrap_len< + 'a, + B: Static, + P: Static>, + W: Static>, +>( + bit: B, pad: P, wrap: W, ilen: usize, +) -> usize { + let olen = encode_pad_len(bit, pad, ilen); + match wrap.val() { + None => olen, + Some((col, end)) => olen + end.len() * div_ceil(olen, col), + } +} +fn encode_wrap_mut< + 'a, + B: Static, + M: Static, + P: Static>, + W: Static>, +>( + bit: B, msb: M, symbols: &[u8; 256], pad: P, wrap: W, input: &[u8], output: &mut [u8], +) { + let (col, end) = match wrap.val() { + None => return encode_pad(bit, msb, symbols, pad, input, output), + Some((col, end)) => (col, end), + }; + debug_assert_eq!(output.len(), encode_wrap_len(bit, pad, wrap, input.len())); + debug_assert_eq!(col % dec(bit.val()), 0); + let col = col / dec(bit.val()); + let enc = col * enc(bit.val()); + let dec = col * dec(bit.val()) + end.len(); + let olen = dec - end.len(); + let n = input.len() / enc; + for i in 0 .. n { + let input = unsafe { chunk_unchecked(input, enc, i) }; + let output = unsafe { chunk_mut_unchecked(output, dec, i) }; + encode_base(bit, msb, symbols, input, &mut output[.. olen]); + output[olen ..].copy_from_slice(end); + } + if input.len() > enc * n { + let olen = dec * n + encode_pad_len(bit, pad, input.len() - enc * n); + encode_pad(bit, msb, symbols, pad, &input[enc * n ..], &mut output[dec * n .. olen]); + output[olen ..].copy_from_slice(end); + } +} + +// Returns the longest valid input length and associated output length. +fn decode_wrap_len, P: Static>( + bit: B, pad: P, len: usize, +) -> (usize, usize) { + let bit = bit.val(); + if pad.val() { + (floor(len, dec(bit)), len / dec(bit) * enc(bit)) + } else { + let trail = bit * len % 8; + (len - trail / bit, bit * len / 8) + } +} + +// Fails with Length if length is invalid. The error is the largest valid +// length. +fn decode_pad_len, P: Static>( + bit: B, pad: P, len: usize, +) -> Result { + let (ilen, olen) = decode_wrap_len(bit, pad, len); + check!(DecodeError { position: ilen, kind: DecodeKind::Length }, ilen == len); + Ok(olen) +} + +// Fails with Length if length is invalid. The error is the largest valid +// length. +fn decode_base_len>(bit: B, len: usize) -> Result { + decode_pad_len(bit, Bf, len) +} +// Fails with Symbol if an input character does not translate to a symbol. The +// error is the lowest index of such character. +// Fails with Trailing if there are non-zero trailing bits. +fn decode_base_mut, M: Static>( + bit: B, msb: M, ctb: bool, values: &[u8; 256], input: &[u8], output: &mut [u8], +) -> Result { + debug_assert_eq!(Ok(output.len()), decode_base_len(bit, input.len())); + let fail = |pos, kind| DecodePartial { + read: pos / dec(bit.val()) * dec(bit.val()), + written: pos / dec(bit.val()) * enc(bit.val()), + error: DecodeError { position: pos, kind }, + }; + decode_mut(bit, msb, values, input, output).map_err(|pos| fail(pos, DecodeKind::Symbol))?; + check_trail(bit, msb, ctb, values, input) + .map_err(|()| fail(input.len() - 1, DecodeKind::Trailing))?; + Ok(output.len()) +} + +// Fails with Symbol if an input character does not translate to a symbol. The +// error is the lowest index of such character. +// Fails with Padding if some padding length is invalid. The error is the index +// of the first padding character of the invalid padding. +// Fails with Trailing if there are non-zero trailing bits. +fn decode_pad_mut, M: Static, P: Static>( + bit: B, msb: M, ctb: bool, values: &[u8; 256], pad: P, input: &[u8], output: &mut [u8], +) -> Result { + if !pad.val() { + return decode_base_mut(bit, msb, ctb, values, input, output); + } + debug_assert_eq!(Ok(output.len()), decode_pad_len(bit, pad, input.len())); + let enc = enc(bit.val()); + let dec = dec(bit.val()); + let mut inpos = 0; + let mut outpos = 0; + let mut outend = output.len(); + while inpos < input.len() { + match decode_base_mut( + bit, + msb, + ctb, + values, + &input[inpos ..], + &mut output[outpos .. outend], + ) { + Ok(written) => { + if cfg!(debug_assertions) { + inpos = input.len(); + } + outpos += written; + break; + } + Err(partial) => { + inpos += partial.read; + outpos += partial.written; + } + } + let inlen = + check_pad(bit, values, &input[inpos .. inpos + dec]).map_err(|pos| DecodePartial { + read: inpos, + written: outpos, + error: DecodeError { position: inpos + pos, kind: DecodeKind::Padding }, + })?; + let outlen = decode_base_len(bit, inlen).unwrap(); + let written = decode_base_mut( + bit, + msb, + ctb, + values, + &input[inpos .. inpos + inlen], + &mut output[outpos .. outpos + outlen], + ) + .map_err(|partial| { + debug_assert_eq!(partial.read, 0); + debug_assert_eq!(partial.written, 0); + DecodePartial { + read: inpos, + written: outpos, + error: DecodeError { + position: inpos + partial.error.position, + kind: partial.error.kind, + }, + } + })?; + debug_assert_eq!(written, outlen); + inpos += dec; + outpos += outlen; + outend -= enc - outlen; + } + debug_assert_eq!(inpos, input.len()); + debug_assert_eq!(outpos, outend); + Ok(outend) +} + +fn skip_ignore(values: &[u8; 256], input: &[u8], mut inpos: usize) -> usize { + while inpos < input.len() && values[input[inpos] as usize] == IGNORE { + inpos += 1; + } + inpos +} +// Returns next input and output position. +// Fails with Symbol if an input character does not translate to a symbol. The +// error is the lowest index of such character. +// Fails with Padding if some padding length is invalid. The error is the index +// of the first padding character of the invalid padding. +// Fails with Trailing if there are non-zero trailing bits. +fn decode_wrap_block, M: Static, P: Static>( + bit: B, msb: M, ctb: bool, values: &[u8; 256], pad: P, input: &[u8], output: &mut [u8], +) -> Result<(usize, usize), DecodeError> { + let dec = dec(bit.val()); + let mut buf = [0u8; 8]; + let mut shift = [0usize; 8]; + let mut bufpos = 0; + let mut inpos = 0; + while bufpos < dec { + inpos = skip_ignore(values, input, inpos); + if inpos == input.len() { + break; + } + shift[bufpos] = inpos; + buf[bufpos] = input[inpos]; + bufpos += 1; + inpos += 1; + } + let olen = decode_pad_len(bit, pad, bufpos).map_err(|mut e| { + e.position = shift[e.position]; + e + })?; + let written = decode_pad_mut(bit, msb, ctb, values, pad, &buf[.. bufpos], &mut output[.. olen]) + .map_err(|partial| { + debug_assert_eq!(partial.read, 0); + debug_assert_eq!(partial.written, 0); + DecodeError { position: shift[partial.error.position], kind: partial.error.kind } + })?; + Ok((inpos, written)) +} +// Fails with Symbol if an input character does not translate to a symbol. The +// error is the lowest index of such character. +// Fails with Padding if some padding length is invalid. The error is the index +// of the first padding character of the invalid padding. +// Fails with Trailing if there are non-zero trailing bits. +// Fails with Length if input length (without ignored characters) is invalid. +#[allow(clippy::too_many_arguments)] +fn decode_wrap_mut, M: Static, P: Static, I: Static>( + bit: B, msb: M, ctb: bool, values: &[u8; 256], pad: P, has_ignore: I, input: &[u8], + output: &mut [u8], +) -> Result { + if !has_ignore.val() { + return decode_pad_mut(bit, msb, ctb, values, pad, input, output); + } + debug_assert_eq!(output.len(), decode_wrap_len(bit, pad, input.len()).1); + let mut inpos = 0; + let mut outpos = 0; + while inpos < input.len() { + let (inlen, outlen) = decode_wrap_len(bit, pad, input.len() - inpos); + match decode_pad_mut( + bit, + msb, + ctb, + values, + pad, + &input[inpos .. inpos + inlen], + &mut output[outpos .. outpos + outlen], + ) { + Ok(written) => { + inpos += inlen; + outpos += written; + break; + } + Err(partial) => { + inpos += partial.read; + outpos += partial.written; + } + } + let (ipos, opos) = + decode_wrap_block(bit, msb, ctb, values, pad, &input[inpos ..], &mut output[outpos ..]) + .map_err(|mut error| { + error.position += inpos; + DecodePartial { read: inpos, written: outpos, error } + })?; + inpos += ipos; + outpos += opos; + } + let inpos = skip_ignore(values, input, inpos); + if inpos == input.len() { + Ok(outpos) + } else { + Err(DecodePartial { + read: inpos, + written: outpos, + error: DecodeError { position: inpos, kind: DecodeKind::Length }, + }) + } +} + +/// Order in which bits are read from a byte +/// +/// The base-conversion encoding is always little-endian. This means that the +/// least significant *byte* is always first. However, we can still choose +/// whether, within a byte, this is the most significant or the least +/// significant *bit* that is first. If the terminology is confusing, testing on +/// an asymmetrical example should be enough to choose the correct value. +/// +/// # Examples +/// +/// In the following example, we can see that a base with the +/// `MostSignificantFirst` bit-order has the most significant bit first in the +/// encoded output. In particular, the output is in the same order as the bits +/// in the byte. The opposite happens with the `LeastSignificantFirst` +/// bit-order. The least significant bit is first and the output is in the +/// reverse order. +/// +/// ```rust +/// use data_encoding::{BitOrder, Specification}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("01"); +/// // spec.bit_order = BitOrder::MostSignificantFirst; // default +/// let msb = spec.encoding().unwrap(); +/// spec.bit_order = BitOrder::LeastSignificantFirst; +/// let lsb = spec.encoding().unwrap(); +/// assert_eq!(msb.encode(&[0b01010011]), "01010011"); +/// assert_eq!(lsb.encode(&[0b01010011]), "11001010"); +/// ``` +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[cfg(feature = "alloc")] +pub enum BitOrder { + /// Most significant bit first + /// + /// This is the most common and most intuitive bit-order. In particular, + /// this is the bit-order used by [RFC4648] and thus the usual hexadecimal, + /// base64, base32, base64url, and base32hex encodings. This is the default + /// bit-order when [specifying](struct.Specification.html) a base. + /// + /// [RFC4648]: https://tools.ietf.org/html/rfc4648 + MostSignificantFirst, + + /// Least significant bit first + /// + /// # Examples + /// + /// DNSCurve [base32] uses least significant bit first: + /// + /// ```rust + /// use data_encoding::BASE32_DNSCURVE; + /// assert_eq!(BASE32_DNSCURVE.encode(&[0x64, 0x88]), "4321"); + /// assert_eq!(BASE32_DNSCURVE.decode(b"4321").unwrap(), vec![0x64, 0x88]); + /// ``` + /// + /// [base32]: constant.BASE32_DNSCURVE.html + LeastSignificantFirst, +} +#[cfg(feature = "alloc")] +use crate::BitOrder::*; + +#[doc(hidden)] +#[cfg(feature = "alloc")] +pub type InternalEncoding = Cow<'static, [u8]>; + +#[doc(hidden)] +#[cfg(not(feature = "alloc"))] +pub type InternalEncoding = &'static [u8]; + +/// Base-conversion encoding +/// +/// See [Specification](struct.Specification.html) for technical details or how +/// to define a new one. +// Required fields: +// 0 - 256 (256) symbols +// 256 - 512 (256) values +// 512 - 513 ( 1) padding +// 513 - 514 ( 1) reserved(3),ctb(1),msb(1),bit(3) +// Optional fields: +// 514 - 515 ( 1) width +// 515 - * ( N) separator +// Invariants: +// - symbols is 2^bit unique characters repeated 2^(8-bit) times +// - values[128 ..] are INVALID +// - values[0 .. 128] are either INVALID, IGNORE, PADDING, or < 2^bit +// - padding is either < 128 or INVALID +// - values[padding] is PADDING if padding < 128 +// - values and symbols are inverse +// - ctb is true if 8 % bit == 0 +// - width is present if there is x such that values[x] is IGNORE +// - width % dec(bit) == 0 +// - for all x in separator values[x] is IGNORE +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Encoding(pub InternalEncoding); + +/// How to translate characters when decoding +/// +/// The order matters. The first character of the `from` field is translated to +/// the first character of the `to` field. The second to the second. Etc. +/// +/// See [Specification](struct.Specification.html) for more information. +#[derive(Debug, Clone)] +#[cfg(feature = "alloc")] +pub struct Translate { + /// Characters to translate from + pub from: String, + /// Characters to translate to + pub to: String, +} + +/// How to wrap the output when encoding +/// +/// See [Specification](struct.Specification.html) for more information. +#[derive(Debug, Clone)] +#[cfg(feature = "alloc")] +pub struct Wrap { + /// Wrapping width + /// + /// Must be a multiple of: + /// + /// - 8 for a bit-width of 1 (binary), 3 (octal), and 5 (base32) + /// - 4 for a bit-width of 2 (base4) and 6 (base64) + /// - 2 for a bit-width of 4 (hexadecimal) + /// + /// Wrapping is disabled if null. + pub width: usize, + + /// Wrapping characters + /// + /// Wrapping is disabled if empty. + pub separator: String, +} + +/// Base-conversion specification +/// +/// It is possible to define custom encodings given a specification. To do so, +/// it is important to understand the theory first. +/// +/// # Theory +/// +/// Each subsection has an equivalent subsection in the [Practice](#practice) +/// section. +/// +/// ## Basics +/// +/// The main idea of a [base-conversion] encoding is to see `[u8]` as numbers +/// written in little-endian base256 and convert them in another little-endian +/// base. For performance reasons, this crate restricts this other base to be of +/// size 2 (binary), 4 (base4), 8 (octal), 16 (hexadecimal), 32 (base32), or 64 +/// (base64). The converted number is written as `[u8]` although it doesn't use +/// all the 256 possible values of `u8`. This crate encodes to ASCII, so only +/// values smaller than 128 are allowed. +/// +/// More precisely, we need the following elements: +/// +/// - The bit-width N: 1 for binary, 2 for base4, 3 for octal, 4 for +/// hexadecimal, 5 for base32, and 6 for base64 +/// - The [bit-order](enum.BitOrder.html): most or least significant bit first +/// - The symbols function S from [0, 2N) (called values and written +/// `uN`) to symbols (represented as `u8` although only ASCII symbols are +/// allowed, i.e. smaller than 128) +/// - The values partial function V from ASCII to [0, 2N), i.e. from +/// `u8` to `uN` +/// - Whether trailing bits are checked: trailing bits are leading zeros in +/// theory, but since numbers are little-endian they come last +/// +/// For the encoding to be correct (i.e. encoding then decoding gives back the +/// initial input), V(S(i)) must be defined and equal to i for all i in [0, +/// 2N). For the encoding to be [canonical][canonical] (i.e. +/// different inputs decode to different outputs, or equivalently, decoding then +/// encoding gives back the initial input), trailing bits must be checked and if +/// V(i) is defined then S(V(i)) is equal to i for all i. +/// +/// Encoding and decoding are given by the following pipeline: +/// +/// ```text +/// [u8] <--1--> [[bit; 8]] <--2--> [[bit; N]] <--3--> [uN] <--4--> [u8] +/// 1: Map bit-order between each u8 and [bit; 8] +/// 2: Base conversion between base 2^8 and base 2^N (check trailing bits) +/// 3: Map bit-order between each [bit; N] and uN +/// 4: Map symbols/values between each uN and u8 (values must be defined) +/// ``` +/// +/// ## Extensions +/// +/// All these extensions make the encoding not canonical. +/// +/// ### Padding +/// +/// Padding is useful if the following conditions are met: +/// +/// - the bit-width is 3 (octal), 5 (base32), or 6 (base64) +/// - the length of the data to encode is not known in advance +/// +/// Bases for which the bit-width N does not divide 8 may not concatenate +/// encoded data. This comes from the fact that it is not possible to make the +/// difference between trailing bits and encoding bits. Padding solves this +/// issue by adding a new character (which is not a symbol) to discriminate +/// between trailing bits and encoding bits. The idea is to work by blocks of +/// lcm(8, N) bits, where lcm(8, N) is the least common multiple of 8 and N. +/// When such block is not complete, it is padded. +/// +/// To preserve correctness, the padding character must not be a symbol. +/// +/// ### Ignore characters when decoding +/// +/// Ignoring characters when decoding is useful if after encoding some +/// characters are added for convenience or any other reason (like wrapping). In +/// that case we want to first ignore thoses characters before decoding. +/// +/// To preserve correctness, ignored characters must not contain symbols or the +/// padding character. +/// +/// ### Wrap output when encoding +/// +/// Wrapping output when encoding is useful if the output is meant to be printed +/// in a document where width is limited (typically 80-columns documents). In +/// that case, the wrapping width and the wrapping separator have to be defined. +/// +/// To preserve correctness, the wrapping separator characters must be ignored +/// (see previous subsection). As such, wrapping separator characters must also +/// not contain symbols or the padding character. +/// +/// ### Translate characters when decoding +/// +/// Translating characters when decoding is useful when encoded data may be +/// copied by a humain instead of a machine. Humans tend to confuse some +/// characters for others. In that case we want to translate those characters +/// before decoding. +/// +/// To preserve correctness, the characters we translate from must not contain +/// symbols or the padding character, and the characters we translate to must +/// only contain symbols or the padding character. +/// +/// # Practice +/// +/// ## Basics +/// +/// ```rust +/// use data_encoding::{Encoding, Specification}; +/// fn make_encoding(symbols: &str) -> Encoding { +/// let mut spec = Specification::new(); +/// spec.symbols.push_str(symbols); +/// spec.encoding().unwrap() +/// } +/// let binary = make_encoding("01"); +/// let octal = make_encoding("01234567"); +/// let hexadecimal = make_encoding("0123456789abcdef"); +/// assert_eq!(binary.encode(b"Bit"), "010000100110100101110100"); +/// assert_eq!(octal.encode(b"Bit"), "20464564"); +/// assert_eq!(hexadecimal.encode(b"Bit"), "426974"); +/// ``` +/// +/// The `binary` base has 2 symbols `0` and `1` with value 0 and 1 respectively. +/// The `octal` base has 8 symbols `0` to `7` with value 0 to 7. The +/// `hexadecimal` base has 16 symbols `0` to `9` and `a` to `f` with value 0 to +/// 15. The following diagram gives the idea of how encoding works in the +/// previous example (note that we can actually write such diagram only because +/// the bit-order is most significant first): +/// +/// ```text +/// [ octal] | 2 : 0 : 4 : 6 : 4 : 5 : 6 : 4 | +/// [ binary] |0 1 0 0 0 0 1 0|0 1 1 0 1 0 0 1|0 1 1 1 0 1 0 0| +/// [hexadecimal] | 4 : 2 | 6 : 9 | 7 : 4 | +/// ^-- LSB ^-- MSB +/// ``` +/// +/// Note that in theory, these little-endian numbers are read from right to left +/// (the most significant bit is at the right). Since leading zeros are +/// meaningless (in our usual decimal notation 0123 is the same as 123), it +/// explains why trailing bits must be zero. Trailing bits may occur when the +/// bit-width of a base does not divide 8. Only binary, base4, and hexadecimal +/// don't have trailing bits issues. So let's consider octal and base64, which +/// have trailing bits in similar circumstances: +/// +/// ```rust +/// use data_encoding::{Specification, BASE64_NOPAD}; +/// let octal = { +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("01234567"); +/// spec.encoding().unwrap() +/// }; +/// assert_eq!(BASE64_NOPAD.encode(b"B"), "Qg"); +/// assert_eq!(octal.encode(b"B"), "204"); +/// ``` +/// +/// We have the following diagram, where the base64 values are written between +/// parentheses: +/// +/// ```text +/// [base64] | Q(16) : g(32) : [has 4 zero trailing bits] +/// [ octal] | 2 : 0 : 4 : [has 1 zero trailing bit ] +/// |0 1 0 0 0 0 1 0|0 0 0 0 +/// [ ascii] | B | +/// ^-^-^-^-- leading zeros / trailing bits +/// ``` +/// +/// ## Extensions +/// +/// ### Padding +/// +/// For octal and base64, lcm(8, 3) == lcm(8, 6) == 24 bits or 3 bytes. For +/// base32, lcm(8, 5) is 40 bits or 5 bytes. Let's consider octal and base64: +/// +/// ```rust +/// use data_encoding::{Specification, BASE64}; +/// let octal = { +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("01234567"); +/// spec.padding = Some('='); +/// spec.encoding().unwrap() +/// }; +/// // We start encoding but we only have "B" for now. +/// assert_eq!(BASE64.encode(b"B"), "Qg=="); +/// assert_eq!(octal.encode(b"B"), "204====="); +/// // Now we have "it". +/// assert_eq!(BASE64.encode(b"it"), "aXQ="); +/// assert_eq!(octal.encode(b"it"), "322720=="); +/// // By concatenating everything, we may decode the original data. +/// assert_eq!(BASE64.decode(b"Qg==aXQ=").unwrap(), b"Bit"); +/// assert_eq!(octal.decode(b"204=====322720==").unwrap(), b"Bit"); +/// ``` +/// +/// We have the following diagrams: +/// +/// ```text +/// [base64] | Q(16) : g(32) : = : = | +/// [ octal] | 2 : 0 : 4 : = : = : = : = : = | +/// |0 1 0 0 0 0 1 0|. . . . . . . .|. . . . . . . .| +/// [ ascii] | B | end of block aligned --^ +/// ^-- beginning of block aligned +/// +/// [base64] | a(26) : X(23) : Q(16) : = | +/// [ octal] | 3 : 2 : 2 : 7 : 2 : 0 : = : = | +/// |0 1 1 0 1 0 0 1|0 1 1 1 0 1 0 0|. . . . . . . .| +/// [ ascii] | i | t | +/// ``` +/// +/// ### Ignore characters when decoding +/// +/// The typical use-case is to ignore newlines (`\r` and `\n`). But to keep the +/// example small, we will ignore spaces. +/// +/// ```rust +/// let mut spec = data_encoding::HEXLOWER.specification(); +/// spec.ignore.push_str(" \t"); +/// let base = spec.encoding().unwrap(); +/// assert_eq!(base.decode(b"42 69 74"), base.decode(b"426974")); +/// ``` +/// +/// ### Wrap output when encoding +/// +/// The typical use-case is to wrap after 64 or 76 characters with a newline +/// (`\r\n` or `\n`). But to keep the example small, we will wrap after 8 +/// characters with a space. +/// +/// ```rust +/// let mut spec = data_encoding::BASE64.specification(); +/// spec.wrap.width = 8; +/// spec.wrap.separator.push_str(" "); +/// let base64 = spec.encoding().unwrap(); +/// assert_eq!(base64.encode(b"Hey you"), "SGV5IHlv dQ== "); +/// ``` +/// +/// Note that the output always ends with the separator. +/// +/// ### Translate characters when decoding +/// +/// The typical use-case is to translate lowercase to uppercase or reciprocally, +/// but it is also used for letters that look alike, like `O0` or `Il1`. Let's +/// illustrate both examples. +/// +/// ```rust +/// let mut spec = data_encoding::HEXLOWER.specification(); +/// spec.translate.from.push_str("ABCDEFOIl"); +/// spec.translate.to.push_str("abcdef011"); +/// let base = spec.encoding().unwrap(); +/// assert_eq!(base.decode(b"BOIl"), base.decode(b"b011")); +/// ``` +/// +/// [base-conversion]: +/// https://en.wikipedia.org/wiki/Positional_notation#Base_conversion +/// [canonical]: https://tools.ietf.org/html/rfc4648#section-3.5 +#[derive(Debug, Clone)] +#[cfg(feature = "alloc")] +pub struct Specification { + /// Symbols + /// + /// The number of symbols must be 2, 4, 8, 16, 32, or 64. Symbols must be + /// ASCII characters (smaller than 128) and they must be unique. + pub symbols: String, + + /// Bit-order + /// + /// The default is to use most significant bit first since it is the most + /// common. + pub bit_order: BitOrder, + + /// Check trailing bits + /// + /// The default is to check trailing bits. This field is ignored when + /// unnecessary (i.e. for base2, base4, and base16). + pub check_trailing_bits: bool, + + /// Padding + /// + /// The default is to not use padding. The padding character must be ASCII + /// and must not be a symbol. + pub padding: Option, + + /// Characters to ignore when decoding + /// + /// The default is to not ignore characters when decoding. The characters to + /// ignore must be ASCII and must not be symbols or the padding character. + pub ignore: String, + + /// How to wrap the output when encoding + /// + /// The default is to not wrap the output when encoding. The wrapping + /// characters must be ASCII and must not be symbols or the padding + /// character. + pub wrap: Wrap, + + /// How to translate characters when decoding + /// + /// The default is to not translate characters when decoding. The characters + /// to translate from must be ASCII and must not have already been assigned + /// a semantics. The characters to translate to must be ASCII and must have + /// been assigned a semantics (symbol, padding character, or ignored + /// character). + pub translate: Translate, +} + +#[cfg(feature = "alloc")] +impl Default for Specification { + fn default() -> Self { + Self::new() + } +} + +impl Encoding { + fn sym(&self) -> &[u8; 256] { + unsafe { as_array(&self.0[0 .. 256]) } + } + fn val(&self) -> &[u8; 256] { + unsafe { as_array(&self.0[256 .. 512]) } + } + fn pad(&self) -> Option { + if self.0[512] < 128 { + Some(self.0[512]) + } else { + None + } + } + fn ctb(&self) -> bool { + self.0[513] & 0x10 != 0 + } + fn msb(&self) -> bool { + self.0[513] & 0x8 != 0 + } + fn bit(&self) -> usize { + (self.0[513] & 0x7) as usize + } + fn wrap(&self) -> Option<(usize, &[u8])> { + if self.0.len() <= 515 { + return None; + } + Some((self.0[514] as usize, &self.0[515 ..])) + } + fn has_ignore(&self) -> bool { + self.0.len() >= 515 + } + + /// Returns the encoded length of an input of length `len` + /// + /// See [`encode_mut`] for when to use it. + /// + /// [`encode_mut`]: struct.Encoding.html#method.encode_mut + pub fn encode_len(&self, len: usize) -> usize { + dispatch! { + let bit: usize = self.bit(); + let pad: Option = self.pad(); + let wrap: Option<(usize, &[u8])> = self.wrap(); + encode_wrap_len(bit, pad, wrap, len) + } + } + + /// Encodes `input` in `output` + /// + /// # Panics + /// + /// Panics if the `output` length does not match the result of + /// [`encode_len`] for the `input` length. + /// + /// # Examples + /// + /// ```rust + /// use data_encoding::BASE64; + /// # let mut buffer = vec![0; 100]; + /// let input = b"Hello world"; + /// let output = &mut buffer[0 .. BASE64.encode_len(input.len())]; + /// BASE64.encode_mut(input, output); + /// assert_eq!(output, b"SGVsbG8gd29ybGQ="); + /// ``` + /// + /// [`encode_len`]: struct.Encoding.html#method.encode_len + #[allow(clippy::cognitive_complexity)] + pub fn encode_mut(&self, input: &[u8], output: &mut [u8]) { + assert_eq!(output.len(), self.encode_len(input.len())); + dispatch! { + let bit: usize = self.bit(); + let msb: bool = self.msb(); + let pad: Option = self.pad(); + let wrap: Option<(usize, &[u8])> = self.wrap(); + encode_wrap_mut(bit, msb, self.sym(), pad, wrap, input, output) + } + } + + /// Appends the encoding of `input` to `output` + /// + /// # Examples + /// + /// ```rust + /// use data_encoding::BASE64; + /// # let mut buffer = vec![0; 100]; + /// let input = b"Hello world"; + /// let mut output = "Result: ".to_string(); + /// BASE64.encode_append(input, &mut output); + /// assert_eq!(output, "Result: SGVsbG8gd29ybGQ="); + /// ``` + #[cfg(feature = "alloc")] + pub fn encode_append(&self, input: &[u8], output: &mut String) { + let output = unsafe { output.as_mut_vec() }; + let output_len = output.len(); + output.resize(output_len + self.encode_len(input.len()), 0u8); + self.encode_mut(input, &mut output[output_len ..]); + } + + /// Returns encoded `input` + /// + /// # Examples + /// + /// ```rust + /// use data_encoding::BASE64; + /// assert_eq!(BASE64.encode(b"Hello world"), "SGVsbG8gd29ybGQ="); + /// ``` + #[cfg(feature = "alloc")] + pub fn encode(&self, input: &[u8]) -> String { + let mut output = vec![0u8; self.encode_len(input.len())]; + self.encode_mut(input, &mut output); + unsafe { String::from_utf8_unchecked(output) } + } + + /// Returns the decoded length of an input of length `len` + /// + /// See [`decode_mut`] for when to use it. + /// + /// # Errors + /// + /// Returns an error if `len` is invalid. The error kind is [`Length`] and + /// the [position] is the greatest valid input length. + /// + /// [`decode_mut`]: struct.Encoding.html#method.decode_mut + /// [`Length`]: enum.DecodeKind.html#variant.Length + /// [position]: struct.DecodeError.html#structfield.position + pub fn decode_len(&self, len: usize) -> Result { + let (ilen, olen) = dispatch! { + let bit: usize = self.bit(); + let pad: bool = self.pad().is_some(); + decode_wrap_len(bit, pad, len) + }; + check!( + DecodeError { position: ilen, kind: DecodeKind::Length }, + self.has_ignore() || len == ilen + ); + Ok(olen) + } + + /// Decodes `input` in `output` + /// + /// Returns the length of the decoded output. This length may be smaller + /// than the output length if the input contained padding or ignored + /// characters. The output bytes after the returned length are not + /// initialized and should not be read. + /// + /// # Panics + /// + /// Panics if the `output` length does not match the result of + /// [`decode_len`] for the `input` length. Also panics if `decode_len` fails + /// for the `input` length. + /// + /// # Errors + /// + /// Returns an error if `input` is invalid. See [`decode`] for more details. + /// The are two differences though: + /// + /// - [`Length`] may be returned only if the encoding allows ignored + /// characters, because otherwise this is already checked by [`decode_len`]. + /// - The [`read`] first bytes of the input have been successfully decoded + /// to the [`written`] first bytes of the output. + /// + /// # Examples + /// + /// ```rust + /// use data_encoding::BASE64; + /// # let mut buffer = vec![0; 100]; + /// let input = b"SGVsbA==byB3b3JsZA=="; + /// let output = &mut buffer[0 .. BASE64.decode_len(input.len()).unwrap()]; + /// let len = BASE64.decode_mut(input, output).unwrap(); + /// assert_eq!(&output[0 .. len], b"Hello world"); + /// ``` + /// + /// [`decode_len`]: struct.Encoding.html#method.decode_len + /// [`decode`]: struct.Encoding.html#method.decode + /// [`Length`]: enum.DecodeKind.html#variant.Length + /// [`read`]: struct.DecodePartial.html#structfield.read + /// [`written`]: struct.DecodePartial.html#structfield.written + #[allow(clippy::cognitive_complexity)] + pub fn decode_mut(&self, input: &[u8], output: &mut [u8]) -> Result { + assert_eq!(Ok(output.len()), self.decode_len(input.len())); + dispatch! { + let bit: usize = self.bit(); + let msb: bool = self.msb(); + let pad: bool = self.pad().is_some(); + let has_ignore: bool = self.has_ignore(); + decode_wrap_mut(bit, msb, self.ctb(), self.val(), pad, has_ignore, + input, output) + } + } + + /// Returns decoded `input` + /// + /// # Errors + /// + /// Returns an error if `input` is invalid. The error kind can be: + /// + /// - [`Length`] if the input length is invalid. The [position] is the + /// greatest valid input length. + /// - [`Symbol`] if the input contains an invalid character. The [position] + /// is the first invalid character. + /// - [`Trailing`] if the input has non-zero trailing bits. This is only + /// possible if the encoding checks trailing bits. The [position] is the + /// first character containing non-zero trailing bits. + /// - [`Padding`] if the input has an invalid padding length. This is only + /// possible if the encoding uses padding. The [position] is the first + /// padding character of the first padding of invalid length. + /// + /// # Examples + /// + /// ```rust + /// use data_encoding::BASE64; + /// assert_eq!(BASE64.decode(b"SGVsbA==byB3b3JsZA==").unwrap(), b"Hello world"); + /// ``` + /// + /// [`Length`]: enum.DecodeKind.html#variant.Length + /// [`Symbol`]: enum.DecodeKind.html#variant.Symbol + /// [`Trailing`]: enum.DecodeKind.html#variant.Trailing + /// [`Padding`]: enum.DecodeKind.html#variant.Padding + /// [position]: struct.DecodeError.html#structfield.position + #[cfg(feature = "alloc")] + pub fn decode(&self, input: &[u8]) -> Result, DecodeError> { + let mut output = vec![0u8; self.decode_len(input.len())?]; + let len = self.decode_mut(input, &mut output).map_err(|partial| partial.error)?; + output.truncate(len); + Ok(output) + } + + /// Returns the bit-width + pub fn bit_width(&self) -> usize { + self.bit() + } + + /// Returns whether the encoding is canonical + /// + /// An encoding is not canonical if one of the following conditions holds: + /// + /// - trailing bits are not checked + /// - padding is used + /// - characters are ignored + /// - characters are translated + pub fn is_canonical(&self) -> bool { + if !self.ctb() { + return false; + } + let bit = self.bit(); + let sym = self.sym(); + let val = self.val(); + for i in 0 .. 256 { + if val[i] == INVALID { + continue; + } + if val[i] >= 1 << bit { + return false; + } + if sym[val[i] as usize] != i as u8 { + return false; + } + } + true + } + + /// Returns the encoding specification + #[cfg(feature = "alloc")] + pub fn specification(&self) -> Specification { + let mut specification = Specification::new(); + specification + .symbols + .push_str(std::str::from_utf8(&self.sym()[0 .. 1 << self.bit()]).unwrap()); + specification.bit_order = + if self.msb() { MostSignificantFirst } else { LeastSignificantFirst }; + specification.check_trailing_bits = self.ctb(); + if let Some(pad) = self.pad() { + specification.padding = Some(pad as char); + } + for i in 0 .. 128u8 { + if self.val()[i as usize] != IGNORE { + continue; + } + specification.ignore.push(i as char); + } + if let Some((col, end)) = self.wrap() { + specification.wrap.width = col; + specification.wrap.separator = std::str::from_utf8(end).unwrap().to_owned(); + } + for i in 0 .. 128u8 { + let canonical = if self.val()[i as usize] < 1 << self.bit() { + self.sym()[self.val()[i as usize] as usize] + } else if self.val()[i as usize] == PADDING { + self.pad().unwrap() + } else { + continue; + }; + if i == canonical { + continue; + } + specification.translate.from.push(i as char); + specification.translate.to.push(canonical as char); + } + specification + } + + #[doc(hidden)] + pub const fn internal_new(implementation: &'static [u8]) -> Encoding { + #[cfg(feature = "alloc")] + let encoding = Encoding(Cow::Borrowed(implementation)); + #[cfg(not(feature = "alloc"))] + let encoding = Encoding(implementation); + encoding + } + + #[doc(hidden)] + pub fn internal_implementation(&self) -> &[u8] { + &self.0 + } +} + +#[derive(Debug, Copy, Clone)] +#[cfg(feature = "alloc")] +enum SpecificationErrorImpl { + BadSize, + NotAscii, + Duplicate(u8), + ExtraPadding, + WrapLength, + WrapWidth(u8), + FromTo, + Undefined(u8), +} +#[cfg(feature = "alloc")] +use crate::SpecificationErrorImpl::*; + +/// Specification error +#[derive(Debug, Copy, Clone)] +#[cfg(feature = "alloc")] +pub struct SpecificationError(SpecificationErrorImpl); + +#[cfg(feature = "alloc")] +impl std::fmt::Display for SpecificationError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self.0 { + BadSize => write!(f, "invalid number of symbols"), + NotAscii => write!(f, "non-ascii character"), + Duplicate(c) => write!(f, "{:?} has conflicting definitions", c as char), + ExtraPadding => write!(f, "unnecessary padding"), + WrapLength => write!(f, "invalid wrap width or separator length"), + WrapWidth(x) => write!(f, "wrap width not a multiple of {}", x), + FromTo => write!(f, "translate from/to length mismatch"), + Undefined(c) => write!(f, "{:?} is undefined", c as char), + } + } +} + +#[cfg(feature = "std")] +impl std::error::Error for SpecificationError { + fn description(&self) -> &str { + match self.0 { + BadSize => "invalid number of symbols", + NotAscii => "non-ascii character", + Duplicate(_) => "conflicting definitions", + ExtraPadding => "unnecessary padding", + WrapLength => "invalid wrap width or separator length", + WrapWidth(_) => "wrap width not a multiple", + FromTo => "translate from/to length mismatch", + Undefined(_) => "undefined character", + } + } +} + +#[cfg(feature = "alloc")] +impl Specification { + /// Returns a default specification + pub fn new() -> Specification { + Specification { + symbols: String::new(), + bit_order: MostSignificantFirst, + check_trailing_bits: true, + padding: None, + ignore: String::new(), + wrap: Wrap { width: 0, separator: String::new() }, + translate: Translate { from: String::new(), to: String::new() }, + } + } + + /// Returns the specified encoding + /// + /// # Errors + /// + /// Returns an error if the specification is invalid. + pub fn encoding(&self) -> Result { + let symbols = self.symbols.as_bytes(); + let bit: usize = match symbols.len() { + 2 => 1, + 4 => 2, + 8 => 3, + 16 => 4, + 32 => 5, + 64 => 6, + _ => return Err(SpecificationError(BadSize)), + }; + let mut values = [INVALID; 128]; + let set = |v: &mut [u8; 128], i: u8, x: u8| { + check!(SpecificationError(NotAscii), i < 128); + if v[i as usize] == x { + return Ok(()); + } + check!(SpecificationError(Duplicate(i)), v[i as usize] == INVALID); + v[i as usize] = x; + Ok(()) + }; + for (v, symbols) in symbols.iter().enumerate() { + set(&mut values, *symbols, v as u8)?; + } + let msb = self.bit_order == MostSignificantFirst; + let ctb = self.check_trailing_bits || 8 % bit == 0; + let pad = match self.padding { + None => None, + Some(pad) => { + check!(SpecificationError(ExtraPadding), 8 % bit != 0); + check!(SpecificationError(NotAscii), pad.len_utf8() == 1); + set(&mut values, pad as u8, PADDING)?; + Some(pad as u8) + } + }; + for i in self.ignore.bytes() { + set(&mut values, i, IGNORE)?; + } + let wrap = if self.wrap.separator.is_empty() || self.wrap.width == 0 { + None + } else { + Some((self.wrap.width, self.wrap.separator.as_bytes())) + }; + if let Some((col, end)) = wrap { + check!(SpecificationError(WrapLength), col < 256 && end.len() < 256); + check!(SpecificationError(WrapWidth(dec(bit) as u8)), col % dec(bit) == 0); + for i in end.iter() { + set(&mut values, *i, IGNORE)?; + } + } + let from = self.translate.from.as_bytes(); + let to = self.translate.to.as_bytes(); + check!(SpecificationError(FromTo), from.len() == to.len()); + for i in 0 .. from.len() { + check!(SpecificationError(NotAscii), to[i] < 128); + let v = values[to[i] as usize]; + check!(SpecificationError(Undefined(to[i])), v != INVALID); + set(&mut values, from[i], v)?; + } + let mut encoding = Vec::new(); + for _ in 0 .. 256 / symbols.len() { + encoding.extend_from_slice(symbols); + } + encoding.extend_from_slice(&values); + encoding.extend_from_slice(&[INVALID; 128]); + match pad { + None => encoding.push(INVALID), + Some(pad) => encoding.push(pad), + } + encoding.push(bit as u8); + if msb { + encoding[513] |= 0x08; + } + if ctb { + encoding[513] |= 0x10; + } + if let Some((col, end)) = wrap { + encoding.push(col as u8); + encoding.extend_from_slice(end); + } else if values.contains(&IGNORE) { + encoding.push(0); + } + Ok(Encoding(Cow::Owned(encoding))) + } +} + +/// Lowercase hexadecimal encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, HEXLOWER}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("0123456789abcdef"); +/// assert_eq!(HEXLOWER, spec.encoding().unwrap()); +/// ``` +/// +/// # Examples +/// +/// ```rust +/// use data_encoding::HEXLOWER; +/// let deadbeef = vec![0xde, 0xad, 0xbe, 0xef]; +/// assert_eq!(HEXLOWER.decode(b"deadbeef").unwrap(), deadbeef); +/// assert_eq!(HEXLOWER.encode(&deadbeef), "deadbeef"); +/// ``` +pub const HEXLOWER: Encoding = Encoding::internal_new(HEXLOWER_IMPL); +const HEXLOWER_IMPL: &[u8] = &[ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, + 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, + 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, + 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, + 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, + 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, + 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, + 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 97, 98, 99, 100, 101, 102, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 1, 2, + 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 28, +]; + +/// Lowercase hexadecimal encoding with case-insensitive decoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, HEXLOWER_PERMISSIVE}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("0123456789abcdef"); +/// spec.translate.from.push_str("ABCDEF"); +/// spec.translate.to.push_str("abcdef"); +/// assert_eq!(HEXLOWER_PERMISSIVE, spec.encoding().unwrap()); +/// ``` +/// +/// # Examples +/// +/// ```rust +/// use data_encoding::HEXLOWER_PERMISSIVE; +/// let deadbeef = vec![0xde, 0xad, 0xbe, 0xef]; +/// assert_eq!(HEXLOWER_PERMISSIVE.decode(b"DeadBeef").unwrap(), deadbeef); +/// assert_eq!(HEXLOWER_PERMISSIVE.encode(&deadbeef), "deadbeef"); +/// ``` +/// +/// You can also define a shorter name: +/// +/// ```rust +/// use data_encoding::{Encoding, HEXLOWER_PERMISSIVE}; +/// const HEX: Encoding = HEXLOWER_PERMISSIVE; +/// ``` +pub const HEXLOWER_PERMISSIVE: Encoding = Encoding::internal_new(HEXLOWER_PERMISSIVE_IMPL); +const HEXLOWER_PERMISSIVE_IMPL: &[u8] = &[ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, + 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, + 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, + 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, + 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, + 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, + 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, + 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, + 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 97, 98, 99, 100, 101, 102, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 1, 2, + 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 28, +]; + +/// Uppercase hexadecimal encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, HEXUPPER}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("0123456789ABCDEF"); +/// assert_eq!(HEXUPPER, spec.encoding().unwrap()); +/// ``` +/// +/// It is compliant with [RFC4648] and known as "base16" or "hex". +/// +/// # Examples +/// +/// ```rust +/// use data_encoding::HEXUPPER; +/// let deadbeef = vec![0xde, 0xad, 0xbe, 0xef]; +/// assert_eq!(HEXUPPER.decode(b"DEADBEEF").unwrap(), deadbeef); +/// assert_eq!(HEXUPPER.encode(&deadbeef), "DEADBEEF"); +/// ``` +/// +/// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-8 +pub const HEXUPPER: Encoding = Encoding::internal_new(HEXUPPER_IMPL); +const HEXUPPER_IMPL: &[u8] = &[ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11, + 12, 13, 14, 15, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 28, +]; + +/// Uppercase hexadecimal encoding with case-insensitive decoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, HEXUPPER_PERMISSIVE}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("0123456789ABCDEF"); +/// spec.translate.from.push_str("abcdef"); +/// spec.translate.to.push_str("ABCDEF"); +/// assert_eq!(HEXUPPER_PERMISSIVE, spec.encoding().unwrap()); +/// ``` +/// +/// # Examples +/// +/// ```rust +/// use data_encoding::HEXUPPER_PERMISSIVE; +/// let deadbeef = vec![0xde, 0xad, 0xbe, 0xef]; +/// assert_eq!(HEXUPPER_PERMISSIVE.decode(b"DeadBeef").unwrap(), deadbeef); +/// assert_eq!(HEXUPPER_PERMISSIVE.encode(&deadbeef), "DEADBEEF"); +/// ``` +pub const HEXUPPER_PERMISSIVE: Encoding = Encoding::internal_new(HEXUPPER_PERMISSIVE_IMPL); +const HEXUPPER_PERMISSIVE_IMPL: &[u8] = &[ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11, + 12, 13, 14, 15, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 28, +]; + +/// Padded base32 encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE32}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"); +/// spec.padding = Some('='); +/// assert_eq!(BASE32, spec.encoding().unwrap()); +/// ``` +/// +/// It is conform to [RFC4648]. +/// +/// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-6 +pub const BASE32: Encoding = Encoding::internal_new(BASE32_IMPL); +const BASE32_IMPL: &[u8] = &[ + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, + 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, + 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 128, 128, 128, 128, 128, 130, 128, 128, + 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 29, +]; + +/// Unpadded base32 encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE32_NOPAD}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"); +/// assert_eq!(BASE32_NOPAD, spec.encoding().unwrap()); +/// ``` +pub const BASE32_NOPAD: Encoding = Encoding::internal_new(BASE32_NOPAD_IMPL); +const BASE32_NOPAD_IMPL: &[u8] = &[ + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, + 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, + 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 50, 51, 52, 53, 54, 55, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, + 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 50, 51, 52, 53, 54, 55, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, + 25, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 29, +]; + +/// Padded base32hex encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE32HEX}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("0123456789ABCDEFGHIJKLMNOPQRSTUV"); +/// spec.padding = Some('='); +/// assert_eq!(BASE32HEX, spec.encoding().unwrap()); +/// ``` +/// +/// It is conform to [RFC4648]. +/// +/// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-7 +pub const BASE32HEX: Encoding = Encoding::internal_new(BASE32HEX_IMPL); +const BASE32HEX_IMPL: &[u8] = &[ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, + 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, + 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, + 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 130, 128, 128, 128, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 29, +]; + +/// Unpadded base32hex encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE32HEX_NOPAD}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("0123456789ABCDEFGHIJKLMNOPQRSTUV"); +/// assert_eq!(BASE32HEX_NOPAD, spec.encoding().unwrap()); +/// ``` +pub const BASE32HEX_NOPAD: Encoding = Encoding::internal_new(BASE32HEX_NOPAD_IMPL); +const BASE32HEX_NOPAD_IMPL: &[u8] = &[ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, + 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, + 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, + 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, + 79, 80, 81, 82, 83, 84, 85, 86, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, + 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11, + 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 29, +]; + +/// DNSSEC base32 encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE32_DNSSEC}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("0123456789abcdefghijklmnopqrstuv"); +/// spec.translate.from.push_str("ABCDEFGHIJKLMNOPQRSTUV"); +/// spec.translate.to.push_str("abcdefghijklmnopqrstuv"); +/// assert_eq!(BASE32_DNSSEC, spec.encoding().unwrap()); +/// ``` +/// +/// It is conform to [RFC5155]: +/// +/// - It uses a base32 extended hex alphabet. +/// - It is case-insensitive when decoding and uses lowercase when encoding. +/// - It does not use padding. +/// +/// [RFC5155]: https://tools.ietf.org/html/rfc5155 +pub const BASE32_DNSSEC: Encoding = Encoding::internal_new(BASE32_DNSSEC_IMPL); +const BASE32_DNSSEC_IMPL: &[u8] = &[ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, + 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, + 116, 117, 118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, + 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 48, 49, 50, 51, 52, 53, + 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, + 113, 114, 115, 116, 117, 118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, + 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, + 110, 111, 112, 113, 114, 115, 116, 117, 118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, + 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, + 118, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, + 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, + 26, 27, 28, 29, 30, 31, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 29, +]; + +/// DNSCurve base32 encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{BitOrder, Specification, BASE32_DNSCURVE}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("0123456789bcdfghjklmnpqrstuvwxyz"); +/// spec.bit_order = BitOrder::LeastSignificantFirst; +/// spec.translate.from.push_str("BCDFGHJKLMNPQRSTUVWXYZ"); +/// spec.translate.to.push_str("bcdfghjklmnpqrstuvwxyz"); +/// assert_eq!(BASE32_DNSCURVE, spec.encoding().unwrap()); +/// ``` +/// +/// It is conform to [DNSCurve]. +/// +/// [DNSCurve]: https://dnscurve.org/in-implement.html +pub const BASE32_DNSCURVE: Encoding = Encoding::internal_new(BASE32_DNSCURVE_IMPL); +const BASE32_DNSCURVE_IMPL: &[u8] = &[ + 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107, 108, 109, 110, + 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, + 98, 99, 100, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, + 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107, + 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, + 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, + 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, + 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, + 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113, + 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, + 100, 102, 103, 104, 106, 107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, + 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106, 107, 108, 109, + 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128, 128, 128, 10, 11, + 12, 128, 13, 14, 15, 128, 16, 17, 18, 19, 20, 128, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, + 128, 128, 128, 128, 128, 128, 128, 10, 11, 12, 128, 13, 14, 15, 128, 16, 17, 18, 19, 20, 128, + 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 21, +]; + +/// Padded base64 encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE64}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); +/// spec.padding = Some('='); +/// assert_eq!(BASE64, spec.encoding().unwrap()); +/// ``` +/// +/// It is conform to [RFC4648]. +/// +/// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-4 +pub const BASE64: Encoding = Encoding::internal_new(BASE64_IMPL); +const BASE64_IMPL: &[u8] = &[ + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, + 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, + 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, + 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, + 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, + 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, + 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, + 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, 68, + 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, + 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, + 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 130, 128, + 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 30, +]; + +/// Unpadded base64 encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE64_NOPAD}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); +/// assert_eq!(BASE64_NOPAD, spec.encoding().unwrap()); +/// ``` +pub const BASE64_NOPAD: Encoding = Encoding::internal_new(BASE64_NOPAD_IMPL); +const BASE64_NOPAD_IMPL: &[u8] = &[ + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, + 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, + 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, + 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, + 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, + 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, + 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, + 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, 68, + 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, + 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, + 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 128, 128, + 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 30, +]; + +/// MIME base64 encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, Wrap, BASE64_MIME}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); +/// spec.padding = Some('='); +/// spec.wrap.width = 76; +/// spec.wrap.separator.push_str("\r\n"); +/// assert_eq!(BASE64_MIME, spec.encoding().unwrap()); +/// ``` +/// +/// It is not exactly conform to [RFC2045] because it does not print the header +/// and does not ignore all characters. +/// +/// [RFC2045]: https://tools.ietf.org/html/rfc2045 +pub const BASE64_MIME: Encoding = Encoding::internal_new(BASE64_MIME_IMPL); +const BASE64_MIME_IMPL: &[u8] = &[ + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, + 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, + 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, + 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, + 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, + 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, + 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, + 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, 68, + 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, + 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, + 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 129, 128, 128, 129, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 130, 128, + 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 30, 76, 13, 10, +]; + +/// Padded base64url encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE64URL}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"); +/// spec.padding = Some('='); +/// assert_eq!(BASE64URL, spec.encoding().unwrap()); +/// ``` +/// +/// It is conform to [RFC4648]. +/// +/// [RFC4648]: https://tools.ietf.org/html/rfc4648#section-5 +pub const BASE64URL: Encoding = Encoding::internal_new(BASE64URL_IMPL); +const BASE64URL_IMPL: &[u8] = &[ + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, + 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, + 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, + 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, + 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, 67, + 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, + 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, + 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, 67, 68, + 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, + 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, + 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 62, 128, 128, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 130, 128, + 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 128, 128, 128, 128, 63, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 30, +]; + +/// Unpadded base64url encoding +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE64URL_NOPAD}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"); +/// assert_eq!(BASE64URL_NOPAD, spec.encoding().unwrap()); +/// ``` +pub const BASE64URL_NOPAD: Encoding = Encoding::internal_new(BASE64URL_NOPAD_IMPL); +const BASE64URL_NOPAD_IMPL: &[u8] = &[ + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, + 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, + 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, + 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, + 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, 67, + 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, + 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, + 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 65, 66, 67, 68, + 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, + 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, + 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 62, 128, 128, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 128, 128, + 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 128, 128, 128, 128, 63, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 30, +]; diff --git a/third_party/rust/rust_decimal/.cargo-checksum.json b/third_party/rust/rust_decimal/.cargo-checksum.json new file mode 100644 index 000000000000..a3aae2c2ab1b --- /dev/null +++ b/third_party/rust/rust_decimal/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"CODE_OF_CONDUCT.md":"64765f10290cfce7191b4208cb21698b708a118568f5016602cccc304846a09a","CONTRIBUTING.md":"471d6281fb5038e17e32d3b4450aacf542a396709605aa170e07d3971d70b9c1","Cargo.toml":"5bcdb31d3230d6592b1940f0730bc6a0a07c05ef245a4a71ae9ff48b83cc5f38","LICENSE":"f8218253704e32441cafea1b9b3bcb2c6a3c51c5553cd8513d179290202bccb2","README.md":"2e6fc38c2289725da3fea1e2429fdc6482484e32b3e11d0216b719d871193fc5","VERSION.md":"172eea9bab41bd1493cd6a4a03a6df5cdfba66a9f02ec79b776fe71ad55d5be8","benches/lib_benches.rs":"39a5a691cd614aee08c0be202d715045dfe1d27e0a998fd983b8cc2ceaca7b55","rustfmt.toml":"f33bda44a494d17c95b7bc1b3dd88c203030b75be766f3a7f9b63ef45d960bb0","src/decimal.rs":"23b00c66f1024c7883f654d492fa6563173b47aa7ad26b4641315883a8278ea4","src/error.rs":"7f546cbfb6b1fdc6bb7bb3d6ef9f1a2462e30beba6f561e1890e7515c9bfb640","src/lib.rs":"104050f8a7d36317da0021dd4b42973e5f6cd928d748f3d0621f100d8d66fa6e","src/postgres.rs":"454630887e43403011dacee0682d163e92aed0071f3258ee616624ec11b82eb6","src/serde_types.rs":"9eadeca56538e69cd909853dd378bffecf2acc98c651ca2eec3192b81044b0a1","tests/decimal_tests.rs":"6c2d5a178a064e4a5e1131ed0d6c14527e9ac819f52379c0225872fa23788bcf"},"package":"95ba36e8c41bf675947e200af432325f332f60a0aea0ef2dc456636c2f6037d7"} \ No newline at end of file diff --git a/third_party/rust/rust_decimal/CODE_OF_CONDUCT.md b/third_party/rust/rust_decimal/CODE_OF_CONDUCT.md new file mode 100644 index 000000000000..c86080edf95e --- /dev/null +++ b/third_party/rust/rust_decimal/CODE_OF_CONDUCT.md @@ -0,0 +1,46 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at paul@form1.co.nz. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] + +[homepage]: http://contributor-covenant.org +[version]: http://contributor-covenant.org/version/1/4/ diff --git a/third_party/rust/rust_decimal/CONTRIBUTING.md b/third_party/rust/rust_decimal/CONTRIBUTING.md new file mode 100644 index 000000000000..fc8aa310ca16 --- /dev/null +++ b/third_party/rust/rust_decimal/CONTRIBUTING.md @@ -0,0 +1,40 @@ +# Contributing to Rust Decimal + +Rust Decimal welcomes contributions from everyone. Here are the guidelines if you are +thinking of helping us: + +## Contributions + +Contributions to Rust Decimal or its dependencies should be made in the form of GitHub +pull requests. Each pull request will be reviewed by a core contributor +(someone with permission to land patches) and either landed in the main tree or +given feedback for changes that would be required. All contributions should +follow this format, even those from core contributors. + +Should you wish to work on an issue, please claim it first by commenting on +the GitHub issue that you want to work on it. This is to prevent duplicated +efforts from contributors on the same issue. + +## Pull Request Checklist + +- Branch from the master branch and, if needed, rebase to the current master + branch before submitting your pull request. If it doesn't merge cleanly with + master you may be asked to rebase your changes. + +- If your patch is not getting reviewed or you need a specific person to review + it, you can @-reply a reviewer asking for a review in the pull request or inside a + comment. + +- Add tests relevant to the fixed bug or new feature. + +## Conduct + +In all Rust Decimal related forums, we follow the [Rust Code of +Conduct](https://www.rust-lang.org/conduct.html). For escalation or moderation of +issues, please contact Paul (paul@form1.co.nz) instead of the Rust +moderation team. + +## Communication + +Opening tickets on the +[paupino/rust-decimal](https://github.com/paupino/rust-decimal) project is the preferred method of communication. \ No newline at end of file diff --git a/third_party/rust/rust_decimal/Cargo.toml b/third_party/rust/rust_decimal/Cargo.toml new file mode 100644 index 000000000000..23c99a12f476 --- /dev/null +++ b/third_party/rust/rust_decimal/Cargo.toml @@ -0,0 +1,82 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +edition = "2018" +name = "rust_decimal" +version = "1.7.0" +authors = ["Paul Mason "] +description = "A Decimal Implementation written in pure Rust suitable for financial calculations." +documentation = "https://docs.rs/rust_decimal/" +readme = "./README.md" +keywords = ["decimal", "financial", "fixed", "precision"] +categories = ["science", "data-structures"] +license = "MIT" +repository = "https://github.com/paupino/rust-decimal" +[dependencies.byteorder] +version = "1.3" +optional = true + +[dependencies.bytes] +version = "0.5" +optional = true + +[dependencies.diesel] +version = "1.4" +features = ["postgres"] +optional = true +default-features = false + +[dependencies.num-traits] +version = "0.2" + +[dependencies.postgres] +version = "0.17" +optional = true + +[dependencies.serde] +version = "1.0" +optional = true + +[dependencies.tokio-postgres] +version = "0.5" +optional = true +[dev-dependencies.bincode] +version = "1.3" + +[dev-dependencies.bytes] +version = "0.5" + +[dev-dependencies.futures] +version = "0.3" + +[dev-dependencies.rand] +version = "0.7" + +[dev-dependencies.serde_derive] +version = "1.0" + +[dev-dependencies.serde_json] +version = "1.0" + +[dev-dependencies.tokio] +version = "0.2" +features = ["rt-threaded", "test-util", "macros"] + +[features] +db-diesel-postgres = ["diesel"] +db-postgres = ["postgres", "bytes", "byteorder"] +db-tokio-postgres = ["postgres", "tokio-postgres", "bytes", "byteorder"] +default = ["serde"] +serde-bincode = ["serde"] +serde-float = ["serde"] +tokio-pg = ["db-tokio-postgres"] diff --git a/third_party/rust/rust_decimal/LICENSE b/third_party/rust/rust_decimal/LICENSE new file mode 100644 index 000000000000..68364efa4e57 --- /dev/null +++ b/third_party/rust/rust_decimal/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Paul Mason + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/rust/rust_decimal/README.md b/third_party/rust/rust_decimal/README.md new file mode 100644 index 000000000000..1a713b2369f2 --- /dev/null +++ b/third_party/rust/rust_decimal/README.md @@ -0,0 +1,84 @@ +# Decimal   [![Build Status]][actions] [![Latest Version]][crates.io] + +[Build Status]: https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fpaupino%2Frust-decimal%2Fbadge&label=build&logo=none +[actions]: https://actions-badge.atrox.dev/paupino/rust-decimal/goto +[Latest Version]: https://img.shields.io/crates/v/rust-decimal.svg +[crates.io]: https://crates.io/crates/rust-decimal + +A Decimal implementation written in pure Rust suitable for financial calculations that require significant integral and fractional digits with no round-off errors. + +The binary representation consists of a 96 bit integer number, a scaling factor used to specify the decimal fraction and a 1 bit sign. Because of this representation, trailing zeros are preserved and may be exposed when in string form. These can be truncated using the `normalize` or `round_dp` functions. + +[Documentation](https://docs.rs/rust_decimal/) + +## Usage + +Decimal numbers can be created in a few distinct ways. The easiest and most optimal method of creating a Decimal is to use the procedural macro within the `rust_decimal_macros` crate: + +```rust +// Procedural macros need importing directly +use rust_decimal_macros::*; + +let number = dec!(-1.23); +``` + +Alternatively you can also use one of the Decimal number convenience functions: + +```rust +use rust_decimal::prelude::*; + +// Using an integer followed by the decimal points +let scaled = Decimal::new(202, 2); // 2.02 + +// From a string representation +let from_string = Decimal::from_str("2.02").unwrap(); // 2.02 + +// Using the `Into` trait +let my_int : Decimal = 3i32.into(); + +// Using the raw decimal representation +// 3.1415926535897932384626433832 +let pi = Decimal::from_parts(1102470952, 185874565, 1703060790, false, 28); +``` + +## Features + +* [db-postgres](#db-postgres) +* [db-tokio-postgres](#db-tokio-postgres) +* [db-diesel-postgres](#db-diesel-postgres) +* [serde-float](#serde-float) +* [serde-bincode](#serde-bincode) + +## `db-postgres` + +This feature enables a PostgreSQL communication module. It allows for reading and writing the `Decimal` +type by transparently serializing/deserializing into the `NUMERIC` data type within PostgreSQL. + +## `db-tokio-postgres` + +Enables the tokio postgres module allowing for async communication with PostgreSQL. + +## `db-diesel-postgres` + +Enable `diesel` PostgreSQL support. + +## `serde-float` + +Enable this so that JSON serialization of Decimal types are sent as a float instead of a string (default). + +e.g. with this turned on, JSON serialization would output: +``` +{ + "value": 1.234 +} +``` + +## `serde-bincode` + +Since `bincode` does not specify type information, we need to ensure that a type hint is provided in order to +correctly be able to deserialize. Enabling this feature on it's own will force deserialization to use `deserialize_str` +instead of `deserialize_any`. + +If, for some reason, you also have `serde-float` enabled then this will use `deserialize_f64` as a type hint. Because +converting to `f64` _loses_ precision, it's highly recommended that you do NOT enable this feature when working with +`bincode`. That being said, this will only use 8 bytes so is slightly more efficient in regards to storage size. diff --git a/third_party/rust/rust_decimal/VERSION.md b/third_party/rust/rust_decimal/VERSION.md new file mode 100644 index 000000000000..2396fd8040f5 --- /dev/null +++ b/third_party/rust/rust_decimal/VERSION.md @@ -0,0 +1,274 @@ +# Version History + +## 1.7.0 + +* Enables `bincode` support via the feature `serde-bincode`. This provides a long term fix for a regression + that was introduced in version `0.6.5` (tests now cover this case!). [Issue 43](https://github.com/paupino/rust-decimal/issues/43). +* Fixes issue where `rescale` on zero would not have an affect. This was due to an early exit condition which failed to + set the new scale. [Issue 253](https://github.com/paupino/rust-decimal/issues/253). +* Add `min` and `max` functions, similar to what `f32` and `f64` provide. Thank you [@michalsieron](https://github.com/michalsieron). +* Updates documentation for `is_sign_positive` and `is_sign_negative` to specify that the sign bit is being checked. + +Please note: feature naming conventions have been modified, however backwards compatible aliases have been created where +necessary. It's highly recommended that you move over to the new naming conventions as these aliases may be removed at a +later date. + +## 1.6.0 + +* Fixes issue with PostgreSQL conversions whereby certain inputs would cause unexpected + outputs. [Issue 241](https://github.com/paupino/rust-decimal/issues/241). +* Fixes issue with `from_str_radix` whereby rounding logic would kick in too early, + especially with radix less than 10. [Issue 242](https://github.com/paupino/rust-decimal/issues/242). +* Fixes issue whereby `from_str` (implicity `from_str_radix`) would panic when there was overflow + and overflow significant digit was < 5. [Issue 246](https://github.com/paupino/rust-decimal/issues/246). +* Make `bytes` and `byteorder` optional since they're only used in the `postgres` feature and tests. +* Fix edge case in `from_i128_with_scale` when `i128::MIN` was provided. + +Thank you to [@serejkaaa512](https://github.com/serejkaaa512), [@AbsurdlySuspicious](https://github.com/AbsurdlySuspicious) and [@0e4ef622]((https://github.com/0e4ef622)) for your contributions! + +## 1.5.0 + +* Added additional `RoundStrategy` abilities: `RoundUp` to always round up and `RoundDown` to always round down. +* Updated prelude to include expected structs and traits by default. + +Special thank you to [@jean-airoldie](https://github.com/jean-airoldie) for adding the additional rounding strategies and to [@pfrenssen](https://github.com/pfrenssen) for fixing an +issue in the README. + +## 1.4.1 + +* Performance improvements for `to_f64` when using a scale > 0. + +Special thank you to [@hengchu](https://github.com/hengchu) who discovered and resolved the issue! + +## 1.4.0 + +* Allow uppercase "E" in scientific notation. +* Allow scientific notation in `dec!` macro. +* Deprecate `set_sign` and replace with `set_sign_positive` and `set_sign_negative`. This is intended + to improve the readability of the API. +* Fixes precision issue when parsing `f64` values. The base 2 mantissa of the float was assuming guaranteed accuracy + of 53 bit precision, however 52 bit precision is more accurate (`f64` only). +* Removes deprecated usage of `Error::description`. + +## 1.3.0 + +* Replace `num` dependency with `num_trait` - implemented `Signed` and `Num` traits. + +## 1.2.1 + +* Fixes issue whereby overflow would occur reading from PostgreSQL with high precision. The library now + handles this by rounding high precision numbers as they're read as opposed to crashing (similar to other + underflow situations e.g. 1/3). + +## 1.2.0 + +* Retain trailing zeros from PostgreSQL. This ensures that the scale is maintained when serialized into the Decimal type. +* Fixes issue where -0 != 0 (these are now equivalent - thank you @hengchu for discovering). +* Improve hashing function so that the following property is true: `k1 == k2 -> hash(k1) == hash(k2)` +* Update normalize function so that -0 normalizes to 0. + +Special thanks to @hathawsh for their help in this release! + +## 1.1.0 + +* Update to Postgres 0.17 and add postgres async/await support via `tokio-pg` +* Added option for serializing decimals as float via `serde-float` + +Special thanks to @pimeys and @kaibyao! + +## 1.0.3 + +Updates dependencies to prevent build issues. + +## 1.0.2 + +Bug fix release: + +* Fixes issue where scaling logic produced incorrect results when one arm was a high precision zero. Thank you @KonishchevDmitry! + +## 1.0.1 + +Bug fix release: + +* Fixes issue where `ToSql` was incorrectly calculating weight when whole portion = numeric portion. +* Fixes issue where `Decimal::new` incorrectly handled `i64::max_value()` and `i64::min_value()`. +* Fixes issue where `rem` operation incorrectly returned results when `scale` was required. + +## 1.0.0 + +This release represents the start of semantic versioning and allows the library to start making fundamental improvements under +the guise of V2.0. Leading up to that I expect to release 1.x versions which will include adding +various mathematical functions such as `pow`, `ln`, `log10` etc. + +Version `1.0.0` does come with some new features: + +* Checked Operations! This implements `checked_add`, `checked_sub`, `checked_mul`, `checked_div` and `checked_rem`. +* Fixes overflow from `max_value()` and `min_value()` for `i32` and `i64`. +* Minor documentation improvements and test coverage. + +Special thanks to @0e4ef622 for their help with this release! + +## 0.11.3 + +* Add prelude to help num trait inclusion (`use rust_decimal::prelude::*`) +* Add `Default` trait to the library. This is equivalent to using `Decimal::zero()` +* Added assignment operators for references. + +Special thanks to @jean-airoldie for his help with this release! + +## 0.11.2 + +* Fall back to `from_scientific` when `from_str` fails during deserialization. Thanks @mattjbray! +* Added basic `Sum` trait implementation + +## 0.11.1 + +* Fixes a bug in `floor` and `ceil` where negative numbers were incorrectly handled. + +## 0.11.0 + +* Macros are now supported on stable. This does use a [hack](https://github.com/dtolnay/proc-macro-hack) for the meantime +so due diligence is required before usage. +* Fixes issue when parsing strings where an underscore preceded a decimal point. +* `const_fn` support via a feature flag. In the future this will be the default option however in order to support older +compiler versions is behind a feature flag. + +## 0.10.2 + +* Macros (nightly) now output structural data as opposed to serialized data. This is fully backwards compatible and results in some minor performance improvements. Also, removed feature gate so that it can be compiled in stable. +* Fixes a string parsing bug when given highly significant numbers that require rounding. + +## 0.10.1 + +* Bumped dependencies to remove some legacy serialization requirements. + +## 0.10.0 + +Special thanks to @xilec, @snd and @AndrewSpeed for their help with this release. + +* New rounding strategies introduced via `round_dp_with_strategy`. Previously default rounding support used bankers rounding by default whereas now you can choose to round the half way point either up or down. +* PostgreSQL write performance improved so that it is at least 3 times faster than the previous implementation. +* `Debug` trait now outputs the actual decimal number by default to make it more useful within consuming libraries (e.g. `criterion.rs`). To get something similar to the previous functionality you can use the `unpack` argument - this is likely for core `rust-decimal` library maintainers. +* Various other performance improvements for common operations such as `rescale`, `sub` and `div`. + +## 0.9.1 + +* Performance optimization for `add`. + +## 0.9.0 + +* Introduces the `Neg` trait to support the ability to use `-decimal_variable`. +* Fixes bug with underflow on addition. + +## 0.8.1 + +This release updates the published documentation only and is a no-op for functionality. + +## 0.8.0 + +* Introduces `from_scientific` allowing parsing of scientific notation into the Decimal type. +* Fixes a bug when formatting a number with a leading zero's. + +## 0.7.2 + +* Fixes bug in `rescale` whereby scaling which invoked rounding incorrectly set the new scale for the left/right sides. + +## 0.7.1 + +* Fixes bug in `cmp` whereby two negatives would return an incorrect result. +* Further documentation examples +* Small improvements in division logic +* New `abs`, `floor` and `ceil` functions. + +## 0.7.0 + +This is a minor version bump as we slowly build our way towards 1.0. Thank you for everyone's support and help as we get there! This has a few notable changes - also introducing a few new interfaces which is the reason for the version bump: + +* `from_parts` function to allow effective creation of `Decimal`'s without requiring binary serialization. An example of this benefit is with the lazy static group initializers for Postgres. +* `normalize` function to allow stripping trailing zero's easily. +* `trunc` function allows truncation of a number without any rounding. This effectively "truncates" the fractional part of the number. +* `fract` function returns the fractional part of the number without the integral. +* Minor improvements in some iterator logic, utilizing the compiler for further optimizations. +* Fixes issue in string parsing logic whereby `_` would cause numbers to be incorrectly identified. +* Many improvements to `mul`. Numbers utilizing the `lo` portion of the decimal only will now be shortcut and bigger numbers will now correctly overflow. True overflows will still panic, however large underflows will now be rounded as necessary as opposed to panicing. +* `Hash` was implemented by convention in `0.6.5` however is reimplemented explicitly in `0.7.0` for effectiveness. +* PostgreSQL read performance improved by pre-caching groups and leveraging `normalize` (i.e. avoiding strings). Further optimizations can be made in write however require some `div` optimizations first. +* Added short circuit write improvement for zero in PostgreSQL writes. +* Benchmarks are now recorded per build so we can start tracking where slow downs have occurred. This does mean there is a performance hit on Travis builds however hopefully the pay off will make it worthwhile. + +## 0.6.5 + +Fixes issue with rescale sometimes causing a silent overflow which led to incorrect results during addition, subtraction and compare. Consequently Decimal now rounds the most significant number so that these operations work successfully. + +In addition, Decimal now derive's the `Hash` trait so that it can be used for indexing. + +## 0.6.4 + +Fixes silent overflow errors when parsing highly significant strings. `from_str` will now round in these scenario's, similar to oleaut32 behavior. + +## 0.6.3 + +Fixes a regression in ordering where by different scales would be rescaled towards losing precision instead of increasing precision. Have added numerous test suites to help cover more issues like this in the future. +Also fixes an issue in parsing invalid strings whereby the precision exceeded our maximum precision. Previously, this would work with unintended results however this now returns an Error returned from `FromStr`. + +## 0.6.2 + +Fixes an issue with division of rational numbers allowing results greater than `MAX_PRECISION`. This would ultimately cause issues for future operations on this number. +In addition, in some cases transitive operations would not be equal due to overflow being lost. + +## 0.6.1 + +This minor release is purely to expose `rust_decimal_macros` for use on the nightly channel. Documentation has been updated accordingly. + +## 0.6.0 + +This release has a few major changes to the internal workings of the `Decimal` implementation and consequently comes with a number of performance improvements. + +* Floats can now be parsed into a `Decimal` type using `from_f32` and `from_f64`. +* `add`, `sub`, `mul` run roughly 1500% faster than before. +* `div` run's roughly 1000% faster than before with room for future improvement. +* Also get significant speed improvements with `cmp`, `rescale`, `round_dp` and some string manipulations. +* Implemented `*Assign` traits for simpler usage. +* Removed `BigInt` and `BigUint` as being intermediary data types. + +## 0.5.2 + +Minor bug fix to prevent a `panic` from overflow during comparison of high significant digit decimals. + +## 0.5.1 + +Minor bux fix to prevent `panic` upon parsing an empty string. + +## 0.5.0 + +* Removes postgres from default feature set. +* `bincode` support for serde +* Better support for format strings +* Benchmarks added to tests + +## 0.4.2 + +Fixes bug in `cmp` whereby negative's were not being compared correctly. + +## 0.4.1 + +Minor bug fix to support creating negative numbers using the default constructor. + +## 0.4.0 + +This release is a stylistic cleanup however does include some minor changes that may break existing builds. + +### Changed +* Serde is now optional. You can enable Serde support within `features` using the keyword `serde`. +* Serde now returns errors on invalid input as opposed to `0`. +* `f64` conversion support has been added. +* Update Postgres dependency to use v0.15. + +## 0.3.1 + +This is a documentation release that should help with discoverability and usage. + +## 0.3.0 + +### Changed +* Removed trait `ToDecimal` and replaced with builtin [`From`](https://doc.rust-lang.org/std/convert/trait.From.html) trait ([`#12`](https://github.com/paupino/rust-decimal/pull/12)) diff --git a/third_party/rust/rust_decimal/benches/lib_benches.rs b/third_party/rust/rust_decimal/benches/lib_benches.rs new file mode 100644 index 000000000000..89e7e70b9478 --- /dev/null +++ b/third_party/rust/rust_decimal/benches/lib_benches.rs @@ -0,0 +1,194 @@ +#![feature(test)] + +extern crate test; + +use rust_decimal::Decimal; +use std::str::FromStr; + +macro_rules! bench_decimal_op { + ($name:ident, $op:tt, $y:expr) => { + #[bench] + fn $name(b: &mut ::test::Bencher) { + let x = Decimal::from_str("2.01").unwrap(); + let y = Decimal::from_str($y).unwrap(); + b.iter(|| { + let result = x $op y; + ::test::black_box(result); + }); + } + } +} + +macro_rules! bench_fold_op { + ($name:ident, $op:tt, $init:expr, $count:expr) => { + #[bench] + fn $name(b: &mut ::test::Bencher) { + fn fold(values: &[Decimal]) -> Decimal { + let mut acc: Decimal = $init.into(); + for value in values { + acc = acc $op value; + } + acc + } + + let values: Vec = test::black_box((1..$count).map(|i| i.into()).collect()); + b.iter(|| { + let result = fold(&values); + ::test::black_box(result); + }); + } + } +} + +/* Add */ +bench_decimal_op!(add_one, +, "1"); +bench_decimal_op!(add_two, +, "2"); +bench_decimal_op!(add_one_hundred, +, "100"); +bench_decimal_op!(add_point_zero_one, +, "0.01"); +bench_decimal_op!(add_negative_point_five, +, "-0.5"); +bench_decimal_op!(add_pi, +, "3.1415926535897932384626433832"); +bench_decimal_op!(add_negative_pi, +, "-3.1415926535897932384626433832"); + +bench_fold_op!(add_10k, +, 0, 10_000); + +/* Sub */ +bench_decimal_op!(sub_one, -, "1"); +bench_decimal_op!(sub_two, -, "2"); +bench_decimal_op!(sub_one_hundred, -, "100"); +bench_decimal_op!(sub_point_zero_one, -, "0.01"); +bench_decimal_op!(sub_negative_point_five, -, "-0.5"); +bench_decimal_op!(sub_pi, -, "3.1415926535897932384626433832"); +bench_decimal_op!(sub_negative_pi, -, "-3.1415926535897932384626433832"); + +bench_fold_op!(sub_10k, -, 5_000_000, 10_000); + +/* Mul */ +bench_decimal_op!(mul_one, *, "1"); +bench_decimal_op!(mul_two, *, "2"); +bench_decimal_op!(mul_one_hundred, *, "100"); +bench_decimal_op!(mul_point_zero_one, *, "0.01"); +bench_decimal_op!(mul_negative_point_five, *, "-0.5"); +bench_decimal_op!(mul_pi, *, "3.1415926535897932384626433832"); +bench_decimal_op!(mul_negative_pi, *, "-3.1415926535897932384626433832"); + +/* Div */ +bench_decimal_op!(div_one, /, "1"); +bench_decimal_op!(div_two, /, "2"); +bench_decimal_op!(div_one_hundred, /, "100"); +bench_decimal_op!(div_point_zero_one, /, "0.01"); +bench_decimal_op!(div_negative_point_five, /, "-0.5"); +bench_decimal_op!(div_pi, /, "3.1415926535897932384626433832"); +bench_decimal_op!(div_negative_pi, /, "-3.1415926535897932384626433832"); + +bench_fold_op!(div_10k, /, Decimal::max_value(), 10_000); + +/* Iteration */ +struct DecimalIterator { + count: usize, +} + +impl DecimalIterator { + fn new() -> DecimalIterator { + DecimalIterator { count: 0 } + } +} + +impl Iterator for DecimalIterator { + type Item = Decimal; + + fn next(&mut self) -> Option { + self.count += 1; + if self.count < 6 { + Some(Decimal::new(314, 2)) + } else { + None + } + } +} + +#[bench] +fn iterator_individual(b: &mut ::test::Bencher) { + b.iter(|| { + let mut result = Decimal::new(0, 0); + let iterator = DecimalIterator::new(); + for i in iterator { + result += i; + } + ::test::black_box(result); + }); +} + +#[bench] +fn iterator_sum(b: &mut ::test::Bencher) { + b.iter(|| { + let result: Decimal = DecimalIterator::new().sum(); + ::test::black_box(result); + }); +} + +#[bench] +fn decimal_from_str(b: &mut test::Bencher) { + let samples_strs = &[ + "3950.123456", + "3950", + "0.1", + "0.01", + "0.001", + "0.0001", + "0.00001", + "0.000001", + "1", + "-100", + "-123.456", + "119996.25", + "1000000", + "9999999.99999", + "12340.56789", + ]; + + b.iter(|| { + for s in samples_strs { + let result = Decimal::from_str(s).unwrap(); + test::black_box(result); + } + }) +} + +#[cfg(feature = "postgres")] +#[bench] +fn to_from_sql(b: &mut ::test::Bencher) { + use postgres::types::{FromSql, Kind, ToSql, Type}; + + let samples_strs = &[ + "3950.123456", + "3950", + "0.1", + "0.01", + "0.001", + "0.0001", + "0.00001", + "0.000001", + "1", + "-100", + "-123.456", + "119996.25", + "1000000", + "9999999.99999", + "12340.56789", + ]; + + let samples: Vec = test::black_box(samples_strs.iter().map(|x| Decimal::from_str(x).unwrap()).collect()); + let t = Type::_new("".into(), 0, Kind::Simple, "".into()); + let mut vec = Vec::::with_capacity(100); + + b.iter(|| { + for _ in 0..100 { + for sample in &samples { + vec.clear(); + sample.to_sql(&t, &mut vec).unwrap(); + let result = Decimal::from_sql(&t, &vec).unwrap(); + ::test::black_box(result); + } + } + }); +} diff --git a/third_party/rust/rust_decimal/rustfmt.toml b/third_party/rust/rust_decimal/rustfmt.toml new file mode 100644 index 000000000000..866c75610557 --- /dev/null +++ b/third_party/rust/rust_decimal/rustfmt.toml @@ -0,0 +1 @@ +max_width = 120 \ No newline at end of file diff --git a/third_party/rust/rust_decimal/src/decimal.rs b/third_party/rust/rust_decimal/src/decimal.rs new file mode 100644 index 000000000000..34ab50598c62 --- /dev/null +++ b/third_party/rust/rust_decimal/src/decimal.rs @@ -0,0 +1,3161 @@ +use crate::Error; + +use num_traits::{FromPrimitive, Num, One, Signed, ToPrimitive, Zero}; + +#[cfg(feature = "diesel")] +use diesel::sql_types::Numeric; + +use std::{ + cmp::{Ordering::Equal, *}, + fmt, + hash::{Hash, Hasher}, + iter::{repeat, Sum}, + ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign}, + str::FromStr, +}; + +// Sign mask for the flags field. A value of zero in this bit indicates a +// positive Decimal value, and a value of one in this bit indicates a +// negative Decimal value. +const SIGN_MASK: u32 = 0x8000_0000; +const UNSIGN_MASK: u32 = 0x4FFF_FFFF; + +// Scale mask for the flags field. This byte in the flags field contains +// the power of 10 to divide the Decimal value by. The scale byte must +// contain a value between 0 and 28 inclusive. +const SCALE_MASK: u32 = 0x00FF_0000; +const U8_MASK: u32 = 0x0000_00FF; +const U32_MASK: u64 = 0xFFFF_FFFF; + +// Number of bits scale is shifted by. +const SCALE_SHIFT: u32 = 16; +// Number of bits sign is shifted by. +const SIGN_SHIFT: u32 = 31; + +// The maximum supported precision +pub(crate) const MAX_PRECISION: u32 = 28; +// 79,228,162,514,264,337,593,543,950,335 +const MAX_I128_REPR: i128 = 0x0000_0000_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF; + +static ONE_INTERNAL_REPR: [u32; 3] = [1, 0, 0]; + +const MIN: Decimal = Decimal { + flags: 2_147_483_648, + lo: 4_294_967_295, + mid: 4_294_967_295, + hi: 4_294_967_295, +}; + +const MAX: Decimal = Decimal { + flags: 0, + lo: 4_294_967_295, + mid: 4_294_967_295, + hi: 4_294_967_295, +}; + +// Fast access for 10^n where n is 0-9 +static POWERS_10: [u32; 10] = [ + 1, + 10, + 100, + 1_000, + 10_000, + 100_000, + 1_000_000, + 10_000_000, + 100_000_000, + 1_000_000_000, +]; +// Fast access for 10^n where n is 10-19 +#[allow(dead_code)] +static BIG_POWERS_10: [u64; 10] = [ + 10_000_000_000, + 100_000_000_000, + 1_000_000_000_000, + 10_000_000_000_000, + 100_000_000_000_000, + 1_000_000_000_000_000, + 10_000_000_000_000_000, + 100_000_000_000_000_000, + 1_000_000_000_000_000_000, + 10_000_000_000_000_000_000, +]; + +/// `UnpackedDecimal` contains unpacked representation of `Decimal` where each component +/// of decimal-format stored in it's own field +#[derive(Clone, Copy, Debug)] +pub struct UnpackedDecimal { + pub is_negative: bool, + pub scale: u32, + pub hi: u32, + pub mid: u32, + pub lo: u32, +} + +/// `Decimal` represents a 128 bit representation of a fixed-precision decimal number. +/// The finite set of values of type `Decimal` are of the form m / 10e, +/// where m is an integer such that -296 < m < 296, and e is an integer +/// between 0 and 28 inclusive. +#[derive(Clone, Copy)] +#[cfg_attr(feature = "diesel", derive(FromSqlRow, AsExpression), sql_type = "Numeric")] +pub struct Decimal { + // Bits 0-15: unused + // Bits 16-23: Contains "e", a value between 0-28 that indicates the scale + // Bits 24-30: unused + // Bit 31: the sign of the Decimal value, 0 meaning positive and 1 meaning negative. + flags: u32, + // The lo, mid, hi, and flags fields contain the representation of the + // Decimal value as a 96-bit integer. + hi: u32, + lo: u32, + mid: u32, +} + +/// `RoundingStrategy` represents the different strategies that can be used by +/// `round_dp_with_strategy`. +/// +/// `RoundingStrategy::BankersRounding` - Rounds toward the nearest even number, e.g. 6.5 -> 6, 7.5 -> 8 +/// `RoundingStrategy::RoundHalfUp` - Rounds up if the value >= 5, otherwise rounds down, e.g. 6.5 -> 7, +/// `RoundingStrategy::RoundHalfDown` - Rounds down if the value =< 5, otherwise rounds up, e.g. +/// 6.5 -> 6, 6.51 -> 7 +/// 1.4999999 -> 1 +/// `RoundingStrategy::RoundDown` - Always round down. +/// `RoundingStrategy::RoundUp` - Always round up. +pub enum RoundingStrategy { + BankersRounding, + RoundHalfUp, + RoundHalfDown, + RoundDown, + RoundUp, +} + +#[allow(dead_code)] +impl Decimal { + /// Returns a `Decimal` with a 64 bit `m` representation and corresponding `e` scale. + /// + /// # Arguments + /// + /// * `num` - An i64 that represents the `m` portion of the decimal number + /// * `scale` - A u32 representing the `e` portion of the decimal number. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let pi = Decimal::new(3141, 3); + /// assert_eq!(pi.to_string(), "3.141"); + /// ``` + pub fn new(num: i64, scale: u32) -> Decimal { + if scale > MAX_PRECISION { + panic!( + "Scale exceeds the maximum precision allowed: {} > {}", + scale, MAX_PRECISION + ); + } + let flags: u32 = scale << SCALE_SHIFT; + if num < 0 { + let pos_num = num.wrapping_neg() as u64; + return Decimal { + flags: flags | SIGN_MASK, + hi: 0, + lo: (pos_num & U32_MASK) as u32, + mid: ((pos_num >> 32) & U32_MASK) as u32, + }; + } + Decimal { + flags, + hi: 0, + lo: (num as u64 & U32_MASK) as u32, + mid: ((num as u64 >> 32) & U32_MASK) as u32, + } + } + + /// Creates a `Decimal` using a 128 bit signed `m` representation and corresponding `e` scale. + /// + /// # Arguments + /// + /// * `num` - An i128 that represents the `m` portion of the decimal number + /// * `scale` - A u32 representing the `e` portion of the decimal number. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let pi = Decimal::from_i128_with_scale(3141i128, 3); + /// assert_eq!(pi.to_string(), "3.141"); + /// ``` + pub fn from_i128_with_scale(num: i128, scale: u32) -> Decimal { + if scale > MAX_PRECISION { + panic!( + "Scale exceeds the maximum precision allowed: {} > {}", + scale, MAX_PRECISION + ); + } + let mut neg = false; + let mut wrapped = num; + if num > MAX_I128_REPR { + panic!("Number exceeds maximum value that can be represented"); + } else if num < -MAX_I128_REPR { + panic!("Number less than minimum value that can be represented"); + } else if num < 0 { + neg = true; + wrapped = -num; + } + let flags: u32 = flags(neg, scale); + Decimal { + flags, + lo: (wrapped as u64 & U32_MASK) as u32, + mid: ((wrapped as u64 >> 32) & U32_MASK) as u32, + hi: ((wrapped as u128 >> 64) as u64 & U32_MASK) as u32, + } + } + + /// Returns a `Decimal` using the instances constituent parts. + /// + /// # Arguments + /// + /// * `lo` - The low 32 bits of a 96-bit integer. + /// * `mid` - The middle 32 bits of a 96-bit integer. + /// * `hi` - The high 32 bits of a 96-bit integer. + /// * `negative` - `true` to indicate a negative number. + /// * `scale` - A power of 10 ranging from 0 to 28. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let pi = Decimal::from_parts(1102470952, 185874565, 1703060790, false, 28); + /// assert_eq!(pi.to_string(), "3.1415926535897932384626433832"); + /// ``` + pub const fn from_parts(lo: u32, mid: u32, hi: u32, negative: bool, scale: u32) -> Decimal { + Decimal { + lo, + mid, + hi, + flags: flags(negative, scale), + } + } + + /// Returns a `Result` which if successful contains the `Decimal` constitution of + /// the scientific notation provided by `value`. + /// + /// # Arguments + /// + /// * `value` - The scientific notation of the `Decimal`. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let value = Decimal::from_scientific("9.7e-7").unwrap(); + /// assert_eq!(value.to_string(), "0.00000097"); + /// ``` + pub fn from_scientific(value: &str) -> Result { + let err = Error::new("Failed to parse"); + let mut split = value.splitn(2, |c| c == 'e' || c == 'E'); + + let base = split.next().ok_or_else(|| err.clone())?; + let exp = split.next().ok_or_else(|| err.clone())?; + + let mut ret = Decimal::from_str(base)?; + let current_scale = ret.scale(); + + if exp.starts_with('-') { + let exp: u32 = exp[1..].parse().map_err(move |_| err)?; + ret.set_scale(current_scale + exp)?; + } else { + let exp: u32 = exp.parse().map_err(move |_| err)?; + if exp <= current_scale { + ret.set_scale(current_scale - exp)?; + } else { + ret *= Decimal::from_i64(10_i64.pow(exp)).unwrap(); + ret = ret.normalize(); + } + } + Ok(ret) + } + + /// Returns the scale of the decimal number, otherwise known as `e`. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let num = Decimal::new(1234, 3); + /// assert_eq!(num.scale(), 3u32); + /// ``` + #[inline] + pub const fn scale(&self) -> u32 { + ((self.flags & SCALE_MASK) >> SCALE_SHIFT) as u32 + } + + /// An optimized method for changing the sign of a decimal number. + /// + /// # Arguments + /// + /// * `positive`: true if the resulting decimal should be positive. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let mut one = Decimal::new(1, 0); + /// one.set_sign(false); + /// assert_eq!(one.to_string(), "-1"); + /// ``` + #[deprecated(since = "1.4.0", note = "please use `set_sign_positive` instead")] + pub fn set_sign(&mut self, positive: bool) { + self.set_sign_positive(positive); + } + + /// An optimized method for changing the sign of a decimal number. + /// + /// # Arguments + /// + /// * `positive`: true if the resulting decimal should be positive. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let mut one = Decimal::new(1, 0); + /// one.set_sign_positive(false); + /// assert_eq!(one.to_string(), "-1"); + /// ``` + #[inline(always)] + pub fn set_sign_positive(&mut self, positive: bool) { + if positive { + self.flags &= UNSIGN_MASK; + } else { + self.flags |= SIGN_MASK; + } + } + + /// An optimized method for changing the sign of a decimal number. + /// + /// # Arguments + /// + /// * `negative`: true if the resulting decimal should be negative. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let mut one = Decimal::new(1, 0); + /// one.set_sign_negative(true); + /// assert_eq!(one.to_string(), "-1"); + /// ``` + #[inline(always)] + pub fn set_sign_negative(&mut self, negative: bool) { + self.set_sign_positive(!negative); + } + + /// An optimized method for changing the scale of a decimal number. + /// + /// # Arguments + /// + /// * `scale`: the new scale of the number + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let mut one = Decimal::new(1, 0); + /// one.set_scale(5); + /// assert_eq!(one.to_string(), "0.00001"); + /// ``` + pub fn set_scale(&mut self, scale: u32) -> Result<(), Error> { + if scale > MAX_PRECISION { + return Err(Error::new("Scale exceeds maximum precision")); + } + self.flags = (scale << SCALE_SHIFT) | (self.flags & SIGN_MASK); + Ok(()) + } + + /// Modifies the `Decimal` to the given scale, attempting to do so without changing the + /// underlying number itself. + /// + /// Note that setting the scale to something less then the current `Decimal`s scale will + /// cause the newly created `Decimal` to have some rounding. + /// Scales greater than the maximum precision supported by `Decimal` will be automatically + /// rounded to `Decimal::MAX_PRECISION`. + /// Rounding leverages the half up strategy. + /// + /// # Arguments + /// * `scale`: The scale to use for the new `Decimal` number. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let mut number = Decimal::new(1_123, 3); + /// number.rescale(6); + /// assert_eq!(number, Decimal::new(1_123_000, 6)); + /// let mut round = Decimal::new(145, 2); + /// round.rescale(1); + /// assert_eq!(round, Decimal::new(15, 1)); + /// ``` + pub fn rescale(&mut self, scale: u32) { + let mut array = [self.lo, self.mid, self.hi]; + let mut value_scale = self.scale(); + rescale_internal(&mut array, &mut value_scale, scale); + self.lo = array[0]; + self.mid = array[1]; + self.hi = array[2]; + self.flags = flags(self.is_sign_negative(), value_scale); + } + + /// Returns a serialized version of the decimal number. + /// The resulting byte array will have the following representation: + /// + /// * Bytes 1-4: flags + /// * Bytes 5-8: lo portion of `m` + /// * Bytes 9-12: mid portion of `m` + /// * Bytes 13-16: high portion of `m` + pub const fn serialize(&self) -> [u8; 16] { + [ + (self.flags & U8_MASK) as u8, + ((self.flags >> 8) & U8_MASK) as u8, + ((self.flags >> 16) & U8_MASK) as u8, + ((self.flags >> 24) & U8_MASK) as u8, + (self.lo & U8_MASK) as u8, + ((self.lo >> 8) & U8_MASK) as u8, + ((self.lo >> 16) & U8_MASK) as u8, + ((self.lo >> 24) & U8_MASK) as u8, + (self.mid & U8_MASK) as u8, + ((self.mid >> 8) & U8_MASK) as u8, + ((self.mid >> 16) & U8_MASK) as u8, + ((self.mid >> 24) & U8_MASK) as u8, + (self.hi & U8_MASK) as u8, + ((self.hi >> 8) & U8_MASK) as u8, + ((self.hi >> 16) & U8_MASK) as u8, + ((self.hi >> 24) & U8_MASK) as u8, + ] + } + + /// Deserializes the given bytes into a decimal number. + /// The deserialized byte representation must be 16 bytes and adhere to the followign convention: + /// + /// * Bytes 1-4: flags + /// * Bytes 5-8: lo portion of `m` + /// * Bytes 9-12: mid portion of `m` + /// * Bytes 13-16: high portion of `m` + pub const fn deserialize(bytes: [u8; 16]) -> Decimal { + Decimal { + flags: (bytes[0] as u32) | (bytes[1] as u32) << 8 | (bytes[2] as u32) << 16 | (bytes[3] as u32) << 24, + lo: (bytes[4] as u32) | (bytes[5] as u32) << 8 | (bytes[6] as u32) << 16 | (bytes[7] as u32) << 24, + mid: (bytes[8] as u32) | (bytes[9] as u32) << 8 | (bytes[10] as u32) << 16 | (bytes[11] as u32) << 24, + hi: (bytes[12] as u32) | (bytes[13] as u32) << 8 | (bytes[14] as u32) << 16 | (bytes[15] as u32) << 24, + } + } + + /// Returns `true` if the decimal is negative. + #[deprecated(since = "0.6.3", note = "please use `is_sign_negative` instead")] + pub fn is_negative(&self) -> bool { + self.is_sign_negative() + } + + /// Returns `true` if the decimal is positive. + #[deprecated(since = "0.6.3", note = "please use `is_sign_positive` instead")] + pub fn is_positive(&self) -> bool { + self.is_sign_positive() + } + + /// Returns `true` if the sign bit of the decimal is negative. + #[inline(always)] + pub const fn is_sign_negative(&self) -> bool { + self.flags & SIGN_MASK > 0 + } + + /// Returns `true` if the sign bit of the decimal is positive. + #[inline(always)] + pub const fn is_sign_positive(&self) -> bool { + self.flags & SIGN_MASK == 0 + } + + /// Returns the minimum possible number that `Decimal` can represent. + pub const fn min_value() -> Decimal { + MIN + } + + /// Returns the maximum possible number that `Decimal` can represent. + pub const fn max_value() -> Decimal { + MAX + } + + /// Returns a new `Decimal` integral with no fractional portion. + /// This is a true truncation whereby no rounding is performed. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let pi = Decimal::new(3141, 3); + /// let trunc = Decimal::new(3, 0); + /// // note that it returns a decimal + /// assert_eq!(pi.trunc(), trunc); + /// ``` + pub fn trunc(&self) -> Decimal { + let mut scale = self.scale(); + if scale == 0 { + // Nothing to do + return *self; + } + let mut working = [self.lo, self.mid, self.hi]; + while scale > 0 { + // We're removing precision, so we don't care about overflow + if scale < 10 { + div_by_u32(&mut working, POWERS_10[scale as usize]); + break; + } else { + div_by_u32(&mut working, POWERS_10[9]); + // Only 9 as this array starts with 1 + scale -= 9; + } + } + Decimal { + lo: working[0], + mid: working[1], + hi: working[2], + flags: flags(self.is_sign_negative(), 0), + } + } + + /// Returns a new `Decimal` representing the fractional portion of the number. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let pi = Decimal::new(3141, 3); + /// let fract = Decimal::new(141, 3); + /// // note that it returns a decimal + /// assert_eq!(pi.fract(), fract); + /// ``` + pub fn fract(&self) -> Decimal { + // This is essentially the original number minus the integral. + // Could possibly be optimized in the future + *self - self.trunc() + } + + /// Computes the absolute value of `self`. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let num = Decimal::new(-3141, 3); + /// assert_eq!(num.abs().to_string(), "3.141"); + /// ``` + pub fn abs(&self) -> Decimal { + let mut me = *self; + me.set_sign_positive(true); + me + } + + /// Returns the largest integer less than or equal to a number. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let num = Decimal::new(3641, 3); + /// assert_eq!(num.floor().to_string(), "3"); + /// ``` + pub fn floor(&self) -> Decimal { + let scale = self.scale(); + if scale == 0 { + // Nothing to do + return *self; + } + + // Opportunity for optimization here + let floored = self.trunc(); + if self.is_sign_negative() && !self.fract().is_zero() { + floored - Decimal::one() + } else { + floored + } + } + + /// Returns the smallest integer greater than or equal to a number. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let num = Decimal::new(3141, 3); + /// assert_eq!(num.ceil().to_string(), "4"); + /// let num = Decimal::new(3, 0); + /// assert_eq!(num.ceil().to_string(), "3"); + /// ``` + pub fn ceil(&self) -> Decimal { + let scale = self.scale(); + if scale == 0 { + // Nothing to do + return *self; + } + + // Opportunity for optimization here + if self.is_sign_positive() && !self.fract().is_zero() { + self.trunc() + Decimal::one() + } else { + self.trunc() + } + } + + /// Returns the maximum of the two numbers. + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let x = Decimal::new(1, 0); + /// let y = Decimal::new(2, 0); + /// assert_eq!(y, x.max(y)); + /// ``` + pub fn max(self, other: Decimal) -> Decimal { + if self < other { + return other; + } else { + self + } + } + + /// Returns the minimum of the two numbers. + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let x = Decimal::new(1, 0); + /// let y = Decimal::new(2, 0); + /// assert_eq!(x, x.min(y)); + /// ``` + pub fn min(self, other: Decimal) -> Decimal { + if self > other { + return other; + } else { + self + } + } + + /// Strips any trailing zero's from a `Decimal` and converts -0 to 0. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// let number = Decimal::new(3100, 3); + /// // note that it returns a decimal, without the extra scale + /// assert_eq!(number.normalize().to_string(), "3.1"); + /// ``` + pub fn normalize(&self) -> Decimal { + if self.is_zero() { + // Convert -0, -0.0*, or 0.0* to 0. + return Decimal::zero(); + } + + let mut scale = self.scale(); + if scale == 0 { + // Nothing to do + return *self; + } + + let mut result = [self.lo, self.mid, self.hi]; + let mut working = [self.lo, self.mid, self.hi]; + while scale > 0 { + if div_by_u32(&mut working, 10) > 0 { + break; + } + scale -= 1; + result.copy_from_slice(&working); + } + Decimal { + lo: result[0], + mid: result[1], + hi: result[2], + flags: flags(self.is_sign_negative(), scale), + } + } + + /// Returns a new `Decimal` number with no fractional portion (i.e. an integer). + /// Rounding currently follows "Bankers Rounding" rules. e.g. 6.5 -> 6, 7.5 -> 8 + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// + /// // Demonstrating bankers rounding... + /// let number_down = Decimal::new(65, 1); + /// let number_up = Decimal::new(75, 1); + /// assert_eq!(number_down.round().to_string(), "6"); + /// assert_eq!(number_up.round().to_string(), "8"); + /// ``` + pub fn round(&self) -> Decimal { + self.round_dp(0) + } + + /// Returns a new `Decimal` number with the specified number of decimal points for fractional + /// portion. + /// Rounding is performed using the provided [`RoundingStrategy`] + /// + /// # Arguments + /// * `dp`: the number of decimal points to round to. + /// * `strategy`: the [`RoundingStrategy`] to use. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::{Decimal, RoundingStrategy}; + /// use std::str::FromStr; + /// + /// let tax = Decimal::from_str("3.4395").unwrap(); + /// assert_eq!(tax.round_dp_with_strategy(2, RoundingStrategy::RoundHalfUp).to_string(), "3.44"); + /// ``` + pub fn round_dp_with_strategy(&self, dp: u32, strategy: RoundingStrategy) -> Decimal { + // Short circuit for zero + if self.is_zero() { + return Decimal { + lo: 0, + mid: 0, + hi: 0, + flags: flags(self.is_sign_negative(), dp), + }; + } + + let old_scale = self.scale(); + + // return early if decimal has a smaller number of fractional places than dp + // e.g. 2.51 rounded to 3 decimal places is 2.51 + if old_scale <= dp { + return *self; + } + + let mut value = [self.lo, self.mid, self.hi]; + let mut value_scale = self.scale(); + let negative = self.is_sign_negative(); + + value_scale -= dp; + + // Rescale to zero so it's easier to work with + while value_scale > 0 { + if value_scale < 10 { + div_by_u32(&mut value, POWERS_10[value_scale as usize]); + value_scale = 0; + } else { + div_by_u32(&mut value, POWERS_10[9]); + value_scale -= 9; + } + } + + // Do some midpoint rounding checks + // We're actually doing two things here. + // 1. Figuring out midpoint rounding when we're right on the boundary. e.g. 2.50000 + // 2. Figuring out whether to add one or not e.g. 2.51 + // For this, we need to figure out the fractional portion that is additional to + // the rounded number. e.g. for 0.12345 rounding to 2dp we'd want 345. + // We're doing the equivalent of losing precision (e.g. to get 0.12) + // then increasing the precision back up to 0.12000 + let mut offset = [self.lo, self.mid, self.hi]; + let mut diff = old_scale - dp; + + while diff > 0 { + if diff < 10 { + div_by_u32(&mut offset, POWERS_10[diff as usize]); + break; + } else { + div_by_u32(&mut offset, POWERS_10[9]); + // Only 9 as this array starts with 1 + diff -= 9; + } + } + + let mut diff = old_scale - dp; + + while diff > 0 { + if diff < 10 { + mul_by_u32(&mut offset, POWERS_10[diff as usize]); + break; + } else { + mul_by_u32(&mut offset, POWERS_10[9]); + // Only 9 as this array starts with 1 + diff -= 9; + } + } + + let mut decimal_portion = [self.lo, self.mid, self.hi]; + sub_internal(&mut decimal_portion, &offset); + + // If the decimal_portion is zero then we round based on the other data + let mut cap = [5, 0, 0]; + for _ in 0..(old_scale - dp - 1) { + mul_by_u32(&mut cap, 10); + } + let order = cmp_internal(&decimal_portion, &cap); + + match strategy { + RoundingStrategy::BankersRounding => { + match order { + Ordering::Equal => { + if (value[0] & 1) == 1 { + add_internal(&mut value, &ONE_INTERNAL_REPR); + } + } + Ordering::Greater => { + // Doesn't matter about the decimal portion + add_internal(&mut value, &ONE_INTERNAL_REPR); + } + _ => {} + } + } + RoundingStrategy::RoundHalfDown => { + if let Ordering::Greater = order { + add_internal(&mut value, &ONE_INTERNAL_REPR); + } + } + RoundingStrategy::RoundHalfUp => { + // when Ordering::Equal, decimal_portion is 0.5 exactly + // when Ordering::Greater, decimal_portion is > 0.5 + match order { + Ordering::Equal => { + add_internal(&mut value, &ONE_INTERNAL_REPR); + } + Ordering::Greater => { + // Doesn't matter about the decimal portion + add_internal(&mut value, &ONE_INTERNAL_REPR); + } + _ => {} + } + } + RoundingStrategy::RoundUp => { + if !is_all_zero(&decimal_portion) { + add_internal(&mut value, &ONE_INTERNAL_REPR); + } + } + RoundingStrategy::RoundDown => (), + } + + Decimal { + lo: value[0], + mid: value[1], + hi: value[2], + flags: flags(negative, dp), + } + } + + /// Returns a new `Decimal` number with the specified number of decimal points for fractional portion. + /// Rounding currently follows "Bankers Rounding" rules. e.g. 6.5 -> 6, 7.5 -> 8 + /// + /// # Arguments + /// * `dp`: the number of decimal points to round to. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// use std::str::FromStr; + /// + /// let pi = Decimal::from_str("3.1415926535897932384626433832").unwrap(); + /// assert_eq!(pi.round_dp(2).to_string(), "3.14"); + /// ``` + pub fn round_dp(&self, dp: u32) -> Decimal { + self.round_dp_with_strategy(dp, RoundingStrategy::BankersRounding) + } + + /// Convert `Decimal` to an internal representation of the underlying struct. This is useful + /// for debugging the internal state of the object. + /// + /// # Important Disclaimer + /// This is primarily intended for library maintainers. The internal representation of a + /// `Decimal` is considered "unstable" for public use. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// use std::str::FromStr; + /// + /// let pi = Decimal::from_str("3.1415926535897932384626433832").unwrap(); + /// assert_eq!(format!("{:?}", pi), "3.1415926535897932384626433832"); + /// assert_eq!(format!("{:?}", pi.unpack()), "UnpackedDecimal { \ + /// is_negative: false, scale: 28, hi: 1703060790, mid: 185874565, lo: 1102470952 \ + /// }"); + /// ``` + pub const fn unpack(&self) -> UnpackedDecimal { + UnpackedDecimal { + is_negative: self.is_sign_negative(), + scale: self.scale(), + hi: self.hi, + lo: self.lo, + mid: self.mid, + } + } + + /// Convert `Decimal` to an internal representation of the underlying struct. This is useful + /// for debugging the internal state of the object. + /// + /// # Important Disclaimer + /// This is primarily intended for library maintainers. The internal representation of a + /// `Decimal` is considered "unstable" for public use. + /// + /// # Example + /// + /// ``` + /// use rust_decimal::Decimal; + /// use std::str::FromStr; + /// + /// let pi = Decimal::from_str("3.1415926535897932384626433832").unwrap(); + /// assert_eq!(format!("{:?}", pi), "3.1415926535897932384626433832"); + /// assert_eq!(format!("{:?}", pi.unpack()), "UnpackedDecimal { \ + /// is_negative: false, scale: 28, hi: 1703060790, mid: 185874565, lo: 1102470952 \ + /// }"); + /// ``` + + #[inline(always)] + pub(crate) fn mantissa_array3(&self) -> [u32; 3] { + [self.lo, self.mid, self.hi] + } + + #[inline(always)] + pub(crate) fn mantissa_array4(&self) -> [u32; 4] { + [self.lo, self.mid, self.hi, 0] + } + + fn base2_to_decimal(bits: &mut [u32; 3], exponent2: i32, positive: bool, is64: bool) -> Option { + // 2^exponent2 = (10^exponent2)/(5^exponent2) + // = (5^-exponent2)*(10^exponent2) + let mut exponent5 = -exponent2; + let mut exponent10 = exponent2; // Ultimately, we want this for the scale + + while exponent5 > 0 { + // Check to see if the mantissa is divisible by 2 + if bits[0] & 0x1 == 0 { + exponent10 += 1; + exponent5 -= 1; + + // We can divide by 2 without losing precision + let hi_carry = bits[2] & 0x1 == 1; + bits[2] >>= 1; + let mid_carry = bits[1] & 0x1 == 1; + bits[1] = (bits[1] >> 1) | if hi_carry { SIGN_MASK } else { 0 }; + bits[0] = (bits[0] >> 1) | if mid_carry { SIGN_MASK } else { 0 }; + } else { + // The mantissa is NOT divisible by 2. Therefore the mantissa should + // be multiplied by 5, unless the multiplication overflows. + exponent5 -= 1; + + let mut temp = [bits[0], bits[1], bits[2]]; + if mul_by_u32(&mut temp, 5) == 0 { + // Multiplication succeeded without overflow, so copy result back + bits[0] = temp[0]; + bits[1] = temp[1]; + bits[2] = temp[2]; + } else { + // Multiplication by 5 overflows. The mantissa should be divided + // by 2, and therefore will lose significant digits. + exponent10 += 1; + + // Shift right + let hi_carry = bits[2] & 0x1 == 1; + bits[2] >>= 1; + let mid_carry = bits[1] & 0x1 == 1; + bits[1] = (bits[1] >> 1) | if hi_carry { SIGN_MASK } else { 0 }; + bits[0] = (bits[0] >> 1) | if mid_carry { SIGN_MASK } else { 0 }; + } + } + } + + // In order to divide the value by 5, it is best to multiply by 2/10. + // Therefore, exponent10 is decremented, and the mantissa should be multiplied by 2 + while exponent5 < 0 { + if bits[2] & SIGN_MASK == 0 { + // No far left bit, the mantissa can withstand a shift-left without overflowing + exponent10 -= 1; + exponent5 += 1; + shl_internal(bits, 1, 0); + } else { + // The mantissa would overflow if shifted. Therefore it should be + // directly divided by 5. This will lose significant digits, unless + // by chance the mantissa happens to be divisible by 5. + exponent5 += 1; + div_by_u32(bits, 5); + } + } + + // At this point, the mantissa has assimilated the exponent5, but + // exponent10 might not be suitable for assignment. exponent10 must be + // in the range [-MAX_PRECISION..0], so the mantissa must be scaled up or + // down appropriately. + while exponent10 > 0 { + // In order to bring exponent10 down to 0, the mantissa should be + // multiplied by 10 to compensate. If the exponent10 is too big, this + // will cause the mantissa to overflow. + if mul_by_u32(bits, 10) == 0 { + exponent10 -= 1; + } else { + // Overflowed - return? + return None; + } + } + + // In order to bring exponent up to -MAX_PRECISION, the mantissa should + // be divided by 10 to compensate. If the exponent10 is too small, this + // will cause the mantissa to underflow and become 0. + while exponent10 < -(MAX_PRECISION as i32) { + let rem10 = div_by_u32(bits, 10); + exponent10 += 1; + if is_all_zero(bits) { + // Underflow, unable to keep dividing + exponent10 = 0; + } else if rem10 >= 5 { + add_internal(bits, &ONE_INTERNAL_REPR); + } + } + + // This step is required in order to remove excess bits of precision from the + // end of the bit representation, down to the precision guaranteed by the + // floating point number + if is64 { + // Guaranteed to about 16 dp + while exponent10 < 0 && (bits[2] != 0 || (bits[1] & 0xFFF0_0000) != 0) { + let rem10 = div_by_u32(bits, 10); + exponent10 += 1; + if rem10 >= 5 { + add_internal(bits, &ONE_INTERNAL_REPR); + } + } + } else { + // Guaranteed to about 7 dp + while exponent10 < 0 + && (bits[2] != 0 || bits[1] != 0 || (bits[2] == 0 && bits[1] == 0 && (bits[0] & 0xFF00_0000) != 0)) + { + let rem10 = div_by_u32(bits, 10); + exponent10 += 1; + if rem10 >= 5 { + add_internal(bits, &ONE_INTERNAL_REPR); + } + } + } + + // Remove multiples of 10 from the representation + while exponent10 < 0 { + let mut temp = [bits[0], bits[1], bits[2]]; + let remainder = div_by_u32(&mut temp, 10); + if remainder == 0 { + exponent10 += 1; + bits[0] = temp[0]; + bits[1] = temp[1]; + bits[2] = temp[2]; + } else { + break; + } + } + + Some(Decimal { + lo: bits[0], + mid: bits[1], + hi: bits[2], + flags: flags(!positive, -exponent10 as u32), + }) + } + + /// Checked addition. Computes `self + other`, returning `None` if overflow occurred. + #[inline(always)] + pub fn checked_add(self, other: Decimal) -> Option { + // Convert to the same scale + let mut my = [self.lo, self.mid, self.hi]; + let mut my_scale = self.scale(); + let mut ot = [other.lo, other.mid, other.hi]; + let mut other_scale = other.scale(); + rescale_to_maximum_scale(&mut my, &mut my_scale, &mut ot, &mut other_scale); + let mut final_scale = my_scale.max(other_scale); + + // Add the items together + let my_negative = self.is_sign_negative(); + let other_negative = other.is_sign_negative(); + let mut negative = false; + let carry; + if !(my_negative ^ other_negative) { + negative = my_negative; + carry = add3_internal(&mut my, &ot); + } else { + let cmp = cmp_internal(&my, &ot); + // -x + y + // if x > y then it's negative (i.e. -2 + 1) + match cmp { + Ordering::Less => { + negative = other_negative; + sub3_internal(&mut ot, &my); + my[0] = ot[0]; + my[1] = ot[1]; + my[2] = ot[2]; + } + Ordering::Greater => { + negative = my_negative; + sub3_internal(&mut my, &ot); + } + Ordering::Equal => { + // -2 + 2 + my[0] = 0; + my[1] = 0; + my[2] = 0; + } + } + carry = 0; + } + + // If we have a carry we underflowed. + // We need to lose some significant digits (if possible) + if carry > 0 { + if final_scale == 0 { + return None; + } + + // Copy it over to a temp array for modification + let mut temp = [my[0], my[1], my[2], carry]; + while final_scale > 0 && temp[3] != 0 { + div_by_u32(&mut temp, 10); + final_scale -= 1; + } + + // If we still have a carry bit then we overflowed + if temp[3] > 0 { + return None; + } + + // Copy it back - we're done + my[0] = temp[0]; + my[1] = temp[1]; + my[2] = temp[2]; + } + Some(Decimal { + lo: my[0], + mid: my[1], + hi: my[2], + flags: flags(negative, final_scale), + }) + } + + /// Checked subtraction. Computes `self - other`, returning `None` if overflow occurred. + #[inline(always)] + pub fn checked_sub(self, other: Decimal) -> Option { + let negated_other = Decimal { + lo: other.lo, + mid: other.mid, + hi: other.hi, + flags: other.flags ^ SIGN_MASK, + }; + self.checked_add(negated_other) + } + + /// Checked multiplication. Computes `self * other`, returning `None` if overflow occurred. + #[inline] + pub fn checked_mul(self, other: Decimal) -> Option { + // Early exit if either is zero + if self.is_zero() || other.is_zero() { + return Some(Decimal::zero()); + } + + // We are only resulting in a negative if we have mismatched signs + let negative = self.is_sign_negative() ^ other.is_sign_negative(); + + // We get the scale of the result by adding the operands. This may be too big, however + // we'll correct later + let mut final_scale = self.scale() + other.scale(); + + // First of all, if ONLY the lo parts of both numbers is filled + // then we can simply do a standard 64 bit calculation. It's a minor + // optimization however prevents the need for long form multiplication + if self.mid == 0 && self.hi == 0 && other.mid == 0 && other.hi == 0 { + // Simply multiplication + let mut u64_result = u64_to_array(u64::from(self.lo) * u64::from(other.lo)); + + // If we're above max precision then this is a very small number + if final_scale > MAX_PRECISION { + final_scale -= MAX_PRECISION; + + // If the number is above 19 then this will equate to zero. + // This is because the max value in 64 bits is 1.84E19 + if final_scale > 19 { + return Some(Decimal::zero()); + } + + let mut rem_lo = 0; + let mut power; + if final_scale > 9 { + // Since 10^10 doesn't fit into u32, we divide by 10^10/4 + // and multiply the next divisor by 4. + rem_lo = div_by_u32(&mut u64_result, 2_500_000_000); + power = POWERS_10[final_scale as usize - 10] << 2; + } else { + power = POWERS_10[final_scale as usize]; + } + + // Divide fits in 32 bits + let rem_hi = div_by_u32(&mut u64_result, power); + + // Round the result. Since the divisor is a power of 10 + // we check to see if the remainder is >= 1/2 divisor + power >>= 1; + if rem_hi >= power && (rem_hi > power || (rem_lo | (u64_result[0] & 0x1)) != 0) { + u64_result[0] += 1; + } + + final_scale = MAX_PRECISION; + } + return Some(Decimal { + lo: u64_result[0], + mid: u64_result[1], + hi: 0, + flags: flags(negative, final_scale), + }); + } + + // We're using some of the high bits, so we essentially perform + // long form multiplication. We compute the 9 partial products + // into a 192 bit result array. + // + // [my-h][my-m][my-l] + // x [ot-h][ot-m][ot-l] + // -------------------------------------- + // 1. [r-hi][r-lo] my-l * ot-l [0, 0] + // 2. [r-hi][r-lo] my-l * ot-m [0, 1] + // 3. [r-hi][r-lo] my-m * ot-l [1, 0] + // 4. [r-hi][r-lo] my-m * ot-m [1, 1] + // 5. [r-hi][r-lo] my-l * ot-h [0, 2] + // 6. [r-hi][r-lo] my-h * ot-l [2, 0] + // 7. [r-hi][r-lo] my-m * ot-h [1, 2] + // 8. [r-hi][r-lo] my-h * ot-m [2, 1] + // 9.[r-hi][r-lo] my-h * ot-h [2, 2] + let my = [self.lo, self.mid, self.hi]; + let ot = [other.lo, other.mid, other.hi]; + let mut product = [0u32, 0u32, 0u32, 0u32, 0u32, 0u32]; + + // We can perform a minor short circuit here. If the + // high portions are both 0 then we can skip portions 5-9 + let to = if my[2] == 0 && ot[2] == 0 { 2 } else { 3 }; + + for my_index in 0..to { + for ot_index in 0..to { + let (mut rlo, mut rhi) = mul_part(my[my_index], ot[ot_index], 0); + + // Get the index for the lo portion of the product + for prod in product.iter_mut().skip(my_index + ot_index) { + let (res, overflow) = add_part(rlo, *prod); + *prod = res; + + // If we have something in rhi from before then promote that + if rhi > 0 { + // If we overflowed in the last add, add that with rhi + if overflow > 0 { + let (nlo, nhi) = add_part(rhi, overflow); + rlo = nlo; + rhi = nhi; + } else { + rlo = rhi; + rhi = 0; + } + } else if overflow > 0 { + rlo = overflow; + rhi = 0; + } else { + break; + } + + // If nothing to do next round then break out + if rlo == 0 { + break; + } + } + } + } + + // If our result has used up the high portion of the product + // then we either have an overflow or an underflow situation + // Overflow will occur if we can't scale it back, whereas underflow + // with kick in rounding + let mut remainder = 0; + while final_scale > 0 && (product[3] != 0 || product[4] != 0 || product[5] != 0) { + remainder = div_by_u32(&mut product, 10u32); + final_scale -= 1; + } + + // Round up the carry if we need to + if remainder >= 5 { + for part in product.iter_mut() { + if remainder == 0 { + break; + } + let digit: u64 = u64::from(*part) + 1; + remainder = if digit > 0xFFFF_FFFF { 1 } else { 0 }; + *part = (digit & 0xFFFF_FFFF) as u32; + } + } + + // If we're still above max precision then we'll try again to + // reduce precision - we may be dealing with a limit of "0" + if final_scale > MAX_PRECISION { + // We're in an underflow situation + // The easiest way to remove precision is to divide off the result + while final_scale > MAX_PRECISION && !is_all_zero(&product) { + div_by_u32(&mut product, 10); + final_scale -= 1; + } + // If we're still at limit then we can't represent any + // siginificant decimal digits and will return an integer only + // Can also be invoked while representing 0. + if final_scale > MAX_PRECISION { + final_scale = 0; + } + } else if !(product[3] == 0 && product[4] == 0 && product[5] == 0) { + // We're in an overflow situation - we're within our precision bounds + // but still have bits in overflow + return None; + } + + Some(Decimal { + lo: product[0], + mid: product[1], + hi: product[2], + flags: flags(negative, final_scale), + }) + } + + /// Checked division. Computes `self / other`, returning `None` if `other == 0.0` or the + /// division results in overflow. + pub fn checked_div(self, other: Decimal) -> Option { + match self.div_impl(other) { + DivResult::Ok(quot) => Some(quot), + DivResult::Overflow => None, + DivResult::DivByZero => None, + } + } + + fn div_impl(self, other: Decimal) -> DivResult { + if other.is_zero() { + return DivResult::DivByZero; + } + if self.is_zero() { + return DivResult::Ok(Decimal::zero()); + } + + let dividend = [self.lo, self.mid, self.hi]; + let divisor = [other.lo, other.mid, other.hi]; + let mut quotient = [0u32, 0u32, 0u32]; + let mut quotient_scale: i32 = self.scale() as i32 - other.scale() as i32; + + // We supply an extra overflow word for each of the dividend and the remainder + let mut working_quotient = [dividend[0], dividend[1], dividend[2], 0u32]; + let mut working_remainder = [0u32, 0u32, 0u32, 0u32]; + let mut working_scale = quotient_scale; + let mut remainder_scale = quotient_scale; + let mut underflow; + + loop { + div_internal(&mut working_quotient, &mut working_remainder, &divisor); + underflow = add_with_scale_internal( + &mut quotient, + &mut quotient_scale, + &mut working_quotient, + &mut working_scale, + ); + + // Multiply the remainder by 10 + let mut overflow = 0; + for part in working_remainder.iter_mut() { + let (lo, hi) = mul_part(*part, 10, overflow); + *part = lo; + overflow = hi; + } + // Copy temp remainder into the temp quotient section + working_quotient.copy_from_slice(&working_remainder); + + remainder_scale += 1; + working_scale = remainder_scale; + + if underflow || is_all_zero(&working_remainder) { + break; + } + } + + // If we have a really big number try to adjust the scale to 0 + while quotient_scale < 0 { + copy_array_diff_lengths(&mut working_quotient, "ient); + working_quotient[3] = 0; + working_remainder.iter_mut().for_each(|x| *x = 0); + + // Mul 10 + let mut overflow = 0; + for part in &mut working_quotient { + let (lo, hi) = mul_part(*part, 10, overflow); + *part = lo; + overflow = hi; + } + for part in &mut working_remainder { + let (lo, hi) = mul_part(*part, 10, overflow); + *part = lo; + overflow = hi; + } + if working_quotient[3] == 0 && is_all_zero(&working_remainder) { + quotient_scale += 1; + quotient[0] = working_quotient[0]; + quotient[1] = working_quotient[1]; + quotient[2] = working_quotient[2]; + } else { + // Overflow + return DivResult::Overflow; + } + } + + if quotient_scale > 255 { + quotient[0] = 0; + quotient[1] = 0; + quotient[2] = 0; + quotient_scale = 0; + } + + let mut quotient_negative = self.is_sign_negative() ^ other.is_sign_negative(); + + // Check for underflow + let mut final_scale: u32 = quotient_scale as u32; + if final_scale > MAX_PRECISION { + let mut remainder = 0; + + // Division underflowed. We must remove some significant digits over using + // an invalid scale. + while final_scale > MAX_PRECISION && !is_all_zero("ient) { + remainder = div_by_u32(&mut quotient, 10); + final_scale -= 1; + } + if final_scale > MAX_PRECISION { + // Result underflowed so set to zero + final_scale = 0; + quotient_negative = false; + } else if remainder >= 5 { + for part in &mut quotient { + if remainder == 0 { + break; + } + let digit: u64 = u64::from(*part) + 1; + remainder = if digit > 0xFFFF_FFFF { 1 } else { 0 }; + *part = (digit & 0xFFFF_FFFF) as u32; + } + } + } + + DivResult::Ok(Decimal { + lo: quotient[0], + mid: quotient[1], + hi: quotient[2], + flags: flags(quotient_negative, final_scale), + }) + } + + /// Checked remainder. Computes `self % other`, returning `None` if `other == 0.0`. + pub fn checked_rem(self, other: Decimal) -> Option { + if other.is_zero() { + return None; + } + if self.is_zero() { + return Some(Decimal::zero()); + } + + // Rescale so comparable + let initial_scale = self.scale(); + let mut quotient = [self.lo, self.mid, self.hi]; + let mut quotient_scale = initial_scale; + let mut divisor = [other.lo, other.mid, other.hi]; + let mut divisor_scale = other.scale(); + rescale_to_maximum_scale(&mut quotient, &mut quotient_scale, &mut divisor, &mut divisor_scale); + + // Working is the remainder + the quotient + // We use an aligned array since we'll be using it a lot. + let mut working_quotient = [quotient[0], quotient[1], quotient[2], 0u32]; + let mut working_remainder = [0u32, 0u32, 0u32, 0u32]; + div_internal(&mut working_quotient, &mut working_remainder, &divisor); + + // Round if necessary. This is for semantic correctness, but could feasibly be removed for + // performance improvements. + if quotient_scale > initial_scale { + let mut working = [ + working_remainder[0], + working_remainder[1], + working_remainder[2], + working_remainder[3], + ]; + while quotient_scale > initial_scale { + if div_by_u32(&mut working, 10) > 0 { + break; + } + quotient_scale -= 1; + working_remainder.copy_from_slice(&working); + } + } + + Some(Decimal { + lo: working_remainder[0], + mid: working_remainder[1], + hi: working_remainder[2], + flags: flags(self.is_sign_negative(), quotient_scale), + }) + } +} + +impl Default for Decimal { + fn default() -> Self { + Self::zero() + } +} + +enum DivResult { + Ok(Decimal), + Overflow, + DivByZero, +} + +#[inline] +const fn flags(neg: bool, scale: u32) -> u32 { + (scale << SCALE_SHIFT) | ((neg as u32) << SIGN_SHIFT) +} + +/// Rescales the given decimals to equivalent scales. +/// It will firstly try to scale both the left and the right side to +/// the maximum scale of left/right. If it is unable to do that it +/// will try to reduce the accuracy of the other argument. +/// e.g. with 1.23 and 2.345 it'll rescale the first arg to 1.230 +#[inline(always)] +fn rescale_to_maximum_scale(left: &mut [u32; 3], left_scale: &mut u32, right: &mut [u32; 3], right_scale: &mut u32) { + if left_scale == right_scale { + // Nothing to do + return; + } + + if is_all_zero(left) { + *left_scale = *right_scale; + return; + } else if is_all_zero(right) { + *right_scale = *left_scale; + return; + } + + if left_scale > right_scale { + rescale_internal(right, right_scale, *left_scale); + if right_scale != left_scale { + rescale_internal(left, left_scale, *right_scale); + } + } else { + rescale_internal(left, left_scale, *right_scale); + if right_scale != left_scale { + rescale_internal(right, right_scale, *left_scale); + } + } +} + +/// Rescales the given decimal to new scale. +/// e.g. with 1.23 and new scale 3 rescale the value to 1.230 +#[inline(always)] +fn rescale_internal(value: &mut [u32; 3], value_scale: &mut u32, new_scale: u32) { + if *value_scale == new_scale { + // Nothing to do + return; + } + + if is_all_zero(value) { + *value_scale = new_scale; + return; + } + + if *value_scale > new_scale { + let mut diff = *value_scale - new_scale; + // Scaling further isn't possible since we got an overflow + // In this case we need to reduce the accuracy of the "side to keep" + + // Now do the necessary rounding + let mut remainder = 0; + while diff > 0 { + if is_all_zero(value) { + *value_scale = new_scale; + return; + } + + diff -= 1; + + // Any remainder is discarded if diff > 0 still (i.e. lost precision) + remainder = div_by_10(value); + } + if remainder >= 5 { + for part in value.iter_mut() { + let digit = u64::from(*part) + 1u64; + remainder = if digit > 0xFFFF_FFFF { 1 } else { 0 }; + *part = (digit & 0xFFFF_FFFF) as u32; + if remainder == 0 { + break; + } + } + } + *value_scale = new_scale; + } else { + let mut diff = new_scale - *value_scale; + let mut working = [value[0], value[1], value[2]]; + while diff > 0 && mul_by_10(&mut working) == 0 { + value.copy_from_slice(&working); + diff -= 1; + } + *value_scale = new_scale - diff; + } +} + +// This method should only be used where copy from slice cannot be +#[inline] +fn copy_array_diff_lengths(into: &mut [u32], from: &[u32]) { + for i in 0..into.len() { + if i >= from.len() { + break; + } + into[i] = from[i]; + } +} + +#[inline] +fn u64_to_array(value: u64) -> [u32; 2] { + [(value & U32_MASK) as u32, (value >> 32 & U32_MASK) as u32] +} + +fn add_internal(value: &mut [u32], by: &[u32]) -> u32 { + let mut carry: u64 = 0; + let vl = value.len(); + let bl = by.len(); + if vl >= bl { + let mut sum: u64; + for i in 0..bl { + sum = u64::from(value[i]) + u64::from(by[i]) + carry; + value[i] = (sum & U32_MASK) as u32; + carry = sum >> 32; + } + if vl > bl && carry > 0 { + for i in value.iter_mut().skip(bl) { + sum = u64::from(*i) + carry; + *i = (sum & U32_MASK) as u32; + carry = sum >> 32; + if carry == 0 { + break; + } + } + } + } else if vl + 1 == bl { + // Overflow, by default, is anything in the high portion of by + let mut sum: u64; + for i in 0..vl { + sum = u64::from(value[i]) + u64::from(by[i]) + carry; + value[i] = (sum & U32_MASK) as u32; + carry = sum >> 32; + } + if by[vl] > 0 { + carry += u64::from(by[vl]); + } + } else { + panic!("Internal error: add using incompatible length arrays. {} <- {}", vl, bl); + } + carry as u32 +} + +#[inline] +fn add3_internal(value: &mut [u32; 3], by: &[u32; 3]) -> u32 { + let mut carry: u32 = 0; + let bl = by.len(); + for i in 0..bl { + let res1 = value[i].overflowing_add(by[i]); + let res2 = res1.0.overflowing_add(carry); + value[i] = res2.0; + carry = (res1.1 | res2.1) as u32; + } + carry +} + +fn add_with_scale_internal( + quotient: &mut [u32; 3], + quotient_scale: &mut i32, + working_quotient: &mut [u32; 4], + working_scale: &mut i32, +) -> bool { + // Add quotient and the working (i.e. quotient = quotient + working) + if is_all_zero(quotient) { + // Quotient is zero so we can just copy the working quotient in directly + // First, make sure they are both 96 bit. + while working_quotient[3] != 0 { + div_by_u32(working_quotient, 10); + *working_scale -= 1; + } + copy_array_diff_lengths(quotient, working_quotient); + *quotient_scale = *working_scale; + return false; + } + + if is_all_zero(working_quotient) { + return false; + } + + // We have ensured that our working is not zero so we should do the addition + + // If our two quotients are different then + // try to scale down the one with the bigger scale + let mut temp3 = [0u32, 0u32, 0u32]; + let mut temp4 = [0u32, 0u32, 0u32, 0u32]; + if *quotient_scale != *working_scale { + // TODO: Remove necessity for temp (without performance impact) + fn div_by_10(target: &mut [u32], temp: &mut [u32], scale: &mut i32, target_scale: i32) { + // Copy to the temp array + temp.copy_from_slice(target); + // divide by 10 until target scale is reached + while *scale > target_scale { + let remainder = div_by_u32(temp, 10); + if remainder == 0 { + *scale -= 1; + target.copy_from_slice(&temp); + } else { + break; + } + } + } + + if *quotient_scale < *working_scale { + div_by_10(working_quotient, &mut temp4, working_scale, *quotient_scale); + } else { + div_by_10(quotient, &mut temp3, quotient_scale, *working_scale); + } + } + + // If our two quotients are still different then + // try to scale up the smaller scale + if *quotient_scale != *working_scale { + // TODO: Remove necessity for temp (without performance impact) + fn mul_by_10(target: &mut [u32], temp: &mut [u32], scale: &mut i32, target_scale: i32) { + temp.copy_from_slice(target); + let mut overflow = 0; + // Multiply by 10 until target scale reached or overflow + while *scale < target_scale && overflow == 0 { + overflow = mul_by_u32(temp, 10); + if overflow == 0 { + // Still no overflow + *scale += 1; + target.copy_from_slice(&temp); + } + } + } + + if *quotient_scale > *working_scale { + mul_by_10(working_quotient, &mut temp4, working_scale, *quotient_scale); + } else { + mul_by_10(quotient, &mut temp3, quotient_scale, *working_scale); + } + } + + // If our two quotients are still different then + // try to scale down the one with the bigger scale + // (ultimately losing significant digits) + if *quotient_scale != *working_scale { + // TODO: Remove necessity for temp (without performance impact) + fn div_by_10_lossy(target: &mut [u32], temp: &mut [u32], scale: &mut i32, target_scale: i32) { + temp.copy_from_slice(target); + // divide by 10 until target scale is reached + while *scale > target_scale { + div_by_u32(temp, 10); + *scale -= 1; + target.copy_from_slice(&temp); + } + } + if *quotient_scale < *working_scale { + div_by_10_lossy(working_quotient, &mut temp4, working_scale, *quotient_scale); + } else { + div_by_10_lossy(quotient, &mut temp3, quotient_scale, *working_scale); + } + } + + // If quotient or working are zero we have an underflow condition + if is_all_zero(quotient) || is_all_zero(working_quotient) { + // Underflow + return true; + } else { + // Both numbers have the same scale and can be added. + // We just need to know whether we can fit them in + let mut underflow = false; + let mut temp = [0u32, 0u32, 0u32]; + while !underflow { + temp.copy_from_slice(quotient); + + // Add the working quotient + let overflow = add_internal(&mut temp, working_quotient); + if overflow == 0 { + // addition was successful + quotient.copy_from_slice(&temp); + break; + } else { + // addition overflowed - remove significant digits and try again + div_by_u32(quotient, 10); + *quotient_scale -= 1; + div_by_u32(working_quotient, 10); + *working_scale -= 1; + // Check for underflow + underflow = is_all_zero(quotient) || is_all_zero(working_quotient); + } + } + if underflow { + return true; + } + } + false +} + +#[inline] +fn add_part(left: u32, right: u32) -> (u32, u32) { + let added = u64::from(left) + u64::from(right); + ((added & U32_MASK) as u32, (added >> 32 & U32_MASK) as u32) +} + +#[inline(always)] +fn sub3_internal(value: &mut [u32; 3], by: &[u32; 3]) { + let mut overflow = 0; + let vl = value.len(); + for i in 0..vl { + let part = (0x1_0000_0000u64 + u64::from(value[i])) - (u64::from(by[i]) + overflow); + value[i] = part as u32; + overflow = 1 - (part >> 32); + } +} + +fn sub_internal(value: &mut [u32], by: &[u32]) -> u32 { + // The way this works is similar to long subtraction + // Let's assume we're working with bytes for simpliciy in an example: + // 257 - 8 = 249 + // 0000_0001 0000_0001 - 0000_0000 0000_1000 = 0000_0000 1111_1001 + // We start by doing the first byte... + // Overflow = 0 + // Left = 0000_0001 (1) + // Right = 0000_1000 (8) + // Firstly, we make sure the left and right are scaled up to twice the size + // Left = 0000_0000 0000_0001 + // Right = 0000_0000 0000_1000 + // We then subtract right from left + // Result = Left - Right = 1111_1111 1111_1001 + // We subtract the overflow, which in this case is 0. + // Because left < right (1 < 8) we invert the high part. + // Lo = 1111_1001 + // Hi = 1111_1111 -> 0000_0001 + // Lo is the field, hi is the overflow. + // We do the same for the second byte... + // Overflow = 1 + // Left = 0000_0001 + // Right = 0000_0000 + // Result = Left - Right = 0000_0000 0000_0001 + // We subtract the overflow... + // Result = 0000_0000 0000_0001 - 1 = 0 + // And we invert the high, just because (invert 0 = 0). + // So our result is: + // 0000_0000 1111_1001 + let mut overflow = 0; + let vl = value.len(); + let bl = by.len(); + for i in 0..vl { + if i >= bl { + break; + } + let (lo, hi) = sub_part(value[i], by[i], overflow); + value[i] = lo; + overflow = hi; + } + overflow +} + +fn sub_part(left: u32, right: u32, overflow: u32) -> (u32, u32) { + let part = 0x1_0000_0000u64 + u64::from(left) - (u64::from(right) + u64::from(overflow)); + let lo = part as u32; + let hi = 1 - ((part >> 32) as u32); + (lo, hi) +} + +// Returns overflow +#[inline] +fn mul_by_10(bits: &mut [u32; 3]) -> u32 { + let mut overflow = 0u64; + for b in bits.iter_mut() { + let result = u64::from(*b) * 10u64 + overflow; + let hi = (result >> 32) & U32_MASK; + let lo = (result & U32_MASK) as u32; + *b = lo; + overflow = hi; + } + + overflow as u32 +} + +// Returns overflow +pub(crate) fn mul_by_u32(bits: &mut [u32], m: u32) -> u32 { + let mut overflow = 0; + for b in bits.iter_mut() { + let (lo, hi) = mul_part(*b, m, overflow); + *b = lo; + overflow = hi; + } + overflow +} + +fn mul_part(left: u32, right: u32, high: u32) -> (u32, u32) { + let result = u64::from(left) * u64::from(right) + u64::from(high); + let hi = ((result >> 32) & U32_MASK) as u32; + let lo = (result & U32_MASK) as u32; + (lo, hi) +} + +fn div_internal(quotient: &mut [u32; 4], remainder: &mut [u32; 4], divisor: &[u32; 3]) { + // There are a couple of ways to do division on binary numbers: + // 1. Using long division + // 2. Using the complement method + // ref: http://paulmason.me/dividing-binary-numbers-part-2/ + // The complement method basically keeps trying to subtract the + // divisor until it can't anymore and placing the rest in remainder. + let mut complement = [ + divisor[0] ^ 0xFFFF_FFFF, + divisor[1] ^ 0xFFFF_FFFF, + divisor[2] ^ 0xFFFF_FFFF, + 0xFFFF_FFFF, + ]; + + // Add one onto the complement + add_internal(&mut complement, &[1u32]); + + // Make sure the remainder is 0 + remainder.iter_mut().for_each(|x| *x = 0); + + // If we have nothing in our hi+ block then shift over till we do + let mut blocks_to_process = 0; + while blocks_to_process < 4 && quotient[3] == 0 { + // Shift whole blocks to the "left" + shl_internal(quotient, 32, 0); + + // Incremember the counter + blocks_to_process += 1; + } + + // Let's try and do the addition... + let mut block = blocks_to_process << 5; + let mut working = [0u32, 0u32, 0u32, 0u32]; + while block < 128 { + // << 1 for quotient AND remainder + let carry = shl_internal(quotient, 1, 0); + shl_internal(remainder, 1, carry); + + // Copy the remainder of working into sub + working.copy_from_slice(remainder); + + // Add the remainder with the complement + add_internal(&mut working, &complement); + + // Check for the significant bit - move over to the quotient + // as necessary + if (working[3] & 0x8000_0000) == 0 { + remainder.copy_from_slice(&working); + quotient[0] |= 1; + } + + // Increment our pointer + block += 1; + } +} + +// Returns remainder +pub(crate) fn div_by_u32(bits: &mut [u32], divisor: u32) -> u32 { + if divisor == 0 { + // Divide by zero + panic!("Internal error: divide by zero"); + } else if divisor == 1 { + // dividend remains unchanged + 0 + } else { + let mut remainder = 0u32; + let divisor = u64::from(divisor); + for part in bits.iter_mut().rev() { + let temp = (u64::from(remainder) << 32) + u64::from(*part); + remainder = (temp % divisor) as u32; + *part = (temp / divisor) as u32; + } + + remainder + } +} + +fn div_by_10(bits: &mut [u32; 3]) -> u32 { + let mut remainder = 0u32; + let divisor = 10u64; + for part in bits.iter_mut().rev() { + let temp = (u64::from(remainder) << 32) + u64::from(*part); + remainder = (temp % divisor) as u32; + *part = (temp / divisor) as u32; + } + + remainder +} + +#[inline] +fn shl_internal(bits: &mut [u32], shift: u32, carry: u32) -> u32 { + let mut shift = shift; + + // Whole blocks first + while shift >= 32 { + // memcpy would be useful here + for i in (1..bits.len()).rev() { + bits[i] = bits[i - 1]; + } + bits[0] = 0; + shift -= 32; + } + + // Continue with the rest + if shift > 0 { + let mut carry = carry; + for part in bits.iter_mut() { + let b = *part >> (32 - shift); + *part = (*part << shift) | carry; + carry = b; + } + carry + } else { + 0 + } +} + +#[inline] +fn cmp_internal(left: &[u32; 3], right: &[u32; 3]) -> Ordering { + let left_hi: u32 = left[2]; + let right_hi: u32 = right[2]; + let left_lo: u64 = u64::from(left[1]) << 32 | u64::from(left[0]); + let right_lo: u64 = u64::from(right[1]) << 32 | u64::from(right[0]); + if left_hi < right_hi || (left_hi <= right_hi && left_lo < right_lo) { + Ordering::Less + } else if left_hi == right_hi && left_lo == right_lo { + Ordering::Equal + } else { + Ordering::Greater + } +} + +#[inline] +pub(crate) fn is_all_zero(bits: &[u32]) -> bool { + bits.iter().all(|b| *b == 0) +} + +macro_rules! impl_from { + ($T:ty, $from_ty:path) => { + impl From<$T> for Decimal { + #[inline] + fn from(t: $T) -> Decimal { + $from_ty(t).unwrap() + } + } + }; +} + +impl_from!(isize, FromPrimitive::from_isize); +impl_from!(i8, FromPrimitive::from_i8); +impl_from!(i16, FromPrimitive::from_i16); +impl_from!(i32, FromPrimitive::from_i32); +impl_from!(i64, FromPrimitive::from_i64); +impl_from!(usize, FromPrimitive::from_usize); +impl_from!(u8, FromPrimitive::from_u8); +impl_from!(u16, FromPrimitive::from_u16); +impl_from!(u32, FromPrimitive::from_u32); +impl_from!(u64, FromPrimitive::from_u64); + +macro_rules! forward_val_val_binop { + (impl $imp:ident for $res:ty, $method:ident) => { + impl $imp<$res> for $res { + type Output = $res; + + #[inline] + fn $method(self, other: $res) -> $res { + (&self).$method(&other) + } + } + }; +} + +macro_rules! forward_ref_val_binop { + (impl $imp:ident for $res:ty, $method:ident) => { + impl<'a> $imp<$res> for &'a $res { + type Output = $res; + + #[inline] + fn $method(self, other: $res) -> $res { + self.$method(&other) + } + } + }; +} + +macro_rules! forward_val_ref_binop { + (impl $imp:ident for $res:ty, $method:ident) => { + impl<'a> $imp<&'a $res> for $res { + type Output = $res; + + #[inline] + fn $method(self, other: &$res) -> $res { + (&self).$method(other) + } + } + }; +} + +macro_rules! forward_all_binop { + (impl $imp:ident for $res:ty, $method:ident) => { + forward_val_val_binop!(impl $imp for $res, $method); + forward_ref_val_binop!(impl $imp for $res, $method); + forward_val_ref_binop!(impl $imp for $res, $method); + }; +} + +impl Zero for Decimal { + fn zero() -> Decimal { + Decimal { + flags: 0, + hi: 0, + lo: 0, + mid: 0, + } + } + + fn is_zero(&self) -> bool { + self.lo.is_zero() && self.mid.is_zero() && self.hi.is_zero() + } +} + +impl One for Decimal { + fn one() -> Decimal { + Decimal { + flags: 0, + hi: 0, + lo: 1, + mid: 0, + } + } +} + +impl Signed for Decimal { + fn abs(&self) -> Self { + self.abs() + } + + fn abs_sub(&self, other: &Self) -> Self { + if self <= other { + Decimal::zero() + } else { + self.abs() + } + } + + fn signum(&self) -> Self { + if self.is_zero() { + Decimal::zero() + } else { + let mut value = Decimal::one(); + if self.is_sign_negative() { + value.set_sign_negative(true); + } + value + } + } + + fn is_positive(&self) -> bool { + self.is_sign_positive() + } + + fn is_negative(&self) -> bool { + self.is_sign_negative() + } +} + +impl Num for Decimal { + type FromStrRadixErr = Error; + + fn from_str_radix(str: &str, radix: u32) -> Result { + if str.is_empty() { + return Err(Error::new("Invalid decimal: empty")); + } + if radix < 2 { + return Err(Error::new("Unsupported radix < 2")); + } + if radix > 36 { + // As per trait documentation + return Err(Error::new("Unsupported radix > 36")); + } + + let mut offset = 0; + let mut len = str.len(); + let bytes: Vec = str.bytes().collect(); + let mut negative = false; // assume positive + + // handle the sign + if bytes[offset] == b'-' { + negative = true; // leading minus means negative + offset += 1; + len -= 1; + } else if bytes[offset] == b'+' { + // leading + allowed + offset += 1; + len -= 1; + } + + // should now be at numeric part of the significand + let mut digits_before_dot: i32 = -1; // digits before '.', -1 if no '.' + let mut coeff = Vec::new(); // integer significand array + + // Supporting different radix + let (max_n, max_alpha_lower, max_alpha_upper) = if radix <= 10 { + (b'0' + (radix - 1) as u8, 0, 0) + } else { + let adj = (radix - 11) as u8; + (b'9', adj + b'a', adj + b'A') + }; + + // Estimate the max precision. All in all, it needs to fit into 96 bits. + // Rather than try to estimate, I've included the constants directly in here. We could, + // perhaps, replace this with a formula if it's faster - though it does appear to be log2. + let estimated_max_precision = match radix { + 2 => 96, + 3 => 61, + 4 => 48, + 5 => 42, + 6 => 38, + 7 => 35, + 8 => 32, + 9 => 31, + 10 => 29, + 11 => 28, + 12 => 27, + 13 => 26, + 14 => 26, + 15 => 25, + 16 => 24, + 17 => 24, + 18 => 24, + 19 => 23, + 20 => 23, + 21 => 22, + 22 => 22, + 23 => 22, + 24 => 21, + 25 => 21, + 26 => 21, + 27 => 21, + 28 => 20, + 29 => 20, + 30 => 20, + 31 => 20, + 32 => 20, + 33 => 20, + 34 => 19, + 35 => 19, + 36 => 19, + _ => return Err(Error::new("Unsupported radix")), + }; + + let mut maybe_round = false; + while len > 0 { + let b = bytes[offset]; + match b { + b'0'..=b'9' => { + if b > max_n { + return Err(Error::new("Invalid decimal: invalid character")); + } + coeff.push(u32::from(b - b'0')); + offset += 1; + len -= 1; + + // If the coefficient is longer than the max, exit early + if coeff.len() as u32 > estimated_max_precision { + maybe_round = true; + break; + } + } + b'a'..=b'z' => { + if b > max_alpha_lower { + return Err(Error::new("Invalid decimal: invalid character")); + } + coeff.push(u32::from(b - b'a') + 10); + offset += 1; + len -= 1; + + if coeff.len() as u32 > estimated_max_precision { + maybe_round = true; + break; + } + } + b'A'..=b'Z' => { + if b > max_alpha_upper { + return Err(Error::new("Invalid decimal: invalid character")); + } + coeff.push(u32::from(b - b'A') + 10); + offset += 1; + len -= 1; + + if coeff.len() as u32 > estimated_max_precision { + maybe_round = true; + break; + } + } + b'.' => { + if digits_before_dot >= 0 { + return Err(Error::new("Invalid decimal: two decimal points")); + } + digits_before_dot = coeff.len() as i32; + offset += 1; + len -= 1; + } + b'_' => { + // Must start with a number... + if coeff.is_empty() { + return Err(Error::new("Invalid decimal: must start lead with a number")); + } + offset += 1; + len -= 1; + } + _ => return Err(Error::new("Invalid decimal: unknown character")), + } + } + + // If we exited before the end of the string then do some rounding if necessary + if maybe_round && offset < bytes.len() { + let next_byte = bytes[offset]; + let digit = match next_byte { + b'0'..=b'9' => { + if next_byte > max_n { + return Err(Error::new("Invalid decimal: invalid character")); + } + u32::from(next_byte - b'0') + } + b'a'..=b'z' => { + if next_byte > max_alpha_lower { + return Err(Error::new("Invalid decimal: invalid character")); + } + u32::from(next_byte - b'a') + 10 + } + b'A'..=b'Z' => { + if next_byte > max_alpha_upper { + return Err(Error::new("Invalid decimal: invalid character")); + } + u32::from(next_byte - b'A') + 10 + } + b'_' => 0, + b'.' => { + // Still an error if we have a second dp + if digits_before_dot >= 0 { + return Err(Error::new("Invalid decimal: two decimal points")); + } + 0 + } + _ => return Err(Error::new("Invalid decimal: unknown character")), + }; + + // Round at midpoint + let midpoint = if radix & 0x1 == 1 { radix / 2 } else { radix + 1 / 2 }; + if digit >= midpoint { + let mut index = coeff.len() - 1; + loop { + let new_digit = coeff[index] + 1; + if new_digit <= 9 { + coeff[index] = new_digit; + break; + } else { + coeff[index] = 0; + if index == 0 { + coeff.insert(0, 1u32); + digits_before_dot += 1; + coeff.pop(); + break; + } + } + index -= 1; + } + } + } + + // here when no characters left + if coeff.is_empty() { + return Err(Error::new("Invalid decimal: no digits found")); + } + + let mut scale = if digits_before_dot >= 0 { + // we had a decimal place so set the scale + (coeff.len() as u32) - (digits_before_dot as u32) + } else { + 0 + }; + + // Parse this using specified radix + let mut data = [0u32, 0u32, 0u32]; + let mut tmp = [0u32, 0u32, 0u32]; + let len = coeff.len(); + for (i, digit) in coeff.iter().enumerate() { + // If the data is going to overflow then we should go into recovery mode + tmp[0] = data[0]; + tmp[1] = data[1]; + tmp[2] = data[2]; + let overflow = mul_by_u32(&mut tmp, radix); + if overflow > 0 { + // This means that we have more data to process, that we're not sure what to do with. + // This may or may not be an issue - depending on whether we're past a decimal point + // or not. + if (i as i32) < digits_before_dot && i + 1 < len { + return Err(Error::new("Invalid decimal: overflow from too many digits")); + } + + if *digit >= 5 { + let carry = add_internal(&mut data, &ONE_INTERNAL_REPR); + if carry > 0 { + // Highly unlikely scenario which is more indicative of a bug + return Err(Error::new("Invalid decimal: overflow when rounding")); + } + } + // We're also one less digit so reduce the scale + let diff = (len - i) as u32; + if diff > scale { + return Err(Error::new("Invalid decimal: overflow from scale mismatch")); + } + scale -= diff; + break; + } else { + data[0] = tmp[0]; + data[1] = tmp[1]; + data[2] = tmp[2]; + let carry = add_internal(&mut data, &[*digit]); + if carry > 0 { + // Highly unlikely scenario which is more indicative of a bug + return Err(Error::new("Invalid decimal: overflow from carry")); + } + } + } + + Ok(Decimal { + lo: data[0], + mid: data[1], + hi: data[2], + flags: flags(negative, scale), + }) + } +} + +impl FromStr for Decimal { + type Err = Error; + + fn from_str(value: &str) -> Result { + Decimal::from_str_radix(value, 10) + } +} + +impl FromPrimitive for Decimal { + fn from_i32(n: i32) -> Option { + let flags: u32; + let value_copy: i64; + if n >= 0 { + flags = 0; + value_copy = n as i64; + } else { + flags = SIGN_MASK; + value_copy = -(n as i64); + } + Some(Decimal { + flags, + lo: value_copy as u32, + mid: 0, + hi: 0, + }) + } + + fn from_i64(n: i64) -> Option { + let flags: u32; + let value_copy: i128; + if n >= 0 { + flags = 0; + value_copy = n as i128; + } else { + flags = SIGN_MASK; + value_copy = -(n as i128); + } + Some(Decimal { + flags, + lo: value_copy as u32, + mid: (value_copy >> 32) as u32, + hi: 0, + }) + } + + fn from_u32(n: u32) -> Option { + Some(Decimal { + flags: 0, + lo: n, + mid: 0, + hi: 0, + }) + } + + fn from_u64(n: u64) -> Option { + Some(Decimal { + flags: 0, + lo: n as u32, + mid: (n >> 32) as u32, + hi: 0, + }) + } + + fn from_f32(n: f32) -> Option { + // Handle the case if it is NaN, Infinity or -Infinity + if !n.is_finite() { + return None; + } + + // It's a shame we can't use a union for this due to it being broken up by bits + // i.e. 1/8/23 (sign, exponent, mantissa) + // See https://en.wikipedia.org/wiki/IEEE_754-1985 + // n = (sign*-1) * 2^exp * mantissa + // Decimal of course stores this differently... 10^-exp * significand + let raw = n.to_bits(); + let positive = (raw >> 31) == 0; + let biased_exponent = ((raw >> 23) & 0xFF) as i32; + let mantissa = raw & 0x007F_FFFF; + + // Handle the special zero case + if biased_exponent == 0 && mantissa == 0 { + let mut zero = Decimal::zero(); + if !positive { + zero.set_sign_negative(true); + } + return Some(zero); + } + + // Get the bits and exponent2 + let mut exponent2 = biased_exponent - 127; + let mut bits = [mantissa, 0u32, 0u32]; + if biased_exponent == 0 { + // Denormalized number - correct the exponent + exponent2 += 1; + } else { + // Add extra hidden bit to mantissa + bits[0] |= 0x0080_0000; + } + + // The act of copying a mantissa as integer bits is equivalent to shifting + // left the mantissa 23 bits. The exponent is reduced to compensate. + exponent2 -= 23; + + // Convert to decimal + Decimal::base2_to_decimal(&mut bits, exponent2, positive, false) + } + + fn from_f64(n: f64) -> Option { + // Handle the case if it is NaN, Infinity or -Infinity + if !n.is_finite() { + return None; + } + + // It's a shame we can't use a union for this due to it being broken up by bits + // i.e. 1/11/52 (sign, exponent, mantissa) + // See https://en.wikipedia.org/wiki/IEEE_754-1985 + // n = (sign*-1) * 2^exp * mantissa + // Decimal of course stores this differently... 10^-exp * significand + let raw = n.to_bits(); + let positive = (raw >> 63) == 0; + let biased_exponent = ((raw >> 52) & 0x7FF) as i32; + let mantissa = raw & 0x000F_FFFF_FFFF_FFFF; + + // Handle the special zero case + if biased_exponent == 0 && mantissa == 0 { + let mut zero = Decimal::zero(); + if !positive { + zero.set_sign_negative(true); + } + return Some(zero); + } + + // Get the bits and exponent2 + let mut exponent2 = biased_exponent - 1023; + let mut bits = [ + (mantissa & 0xFFFF_FFFF) as u32, + ((mantissa >> 32) & 0xFFFF_FFFF) as u32, + 0u32, + ]; + if biased_exponent == 0 { + // Denormalized number - correct the exponent + exponent2 += 1; + } else { + // Add extra hidden bit to mantissa + bits[1] |= 0x0010_0000; + } + + // The act of copying a mantissa as integer bits is equivalent to shifting + // left the mantissa 52 bits. The exponent is reduced to compensate. + exponent2 -= 52; + + // Convert to decimal + Decimal::base2_to_decimal(&mut bits, exponent2, positive, true) + } +} + +impl ToPrimitive for Decimal { + fn to_i64(&self) -> Option { + let d = self.trunc(); + // Quick overflow check + if d.hi != 0 || (d.mid & 0x8000_0000) > 0 { + // Overflow + return None; + } + + let raw: i64 = (i64::from(d.mid) << 32) | i64::from(d.lo); + if self.is_sign_negative() { + Some(-raw) + } else { + Some(raw) + } + } + + fn to_u64(&self) -> Option { + if self.is_sign_negative() { + return None; + } + + let d = self.trunc(); + if d.hi != 0 { + // Overflow + return None; + } + + Some((u64::from(d.mid) << 32) | u64::from(d.lo)) + } + + fn to_f64(&self) -> Option { + if self.scale() == 0 { + let integer = self.to_i64(); + match integer { + Some(i) => Some(i as f64), + None => None, + } + } else { + let sign: f64 = if self.is_sign_negative() { -1.0 } else { 1.0 }; + let mut mantissa: u128 = self.lo.into(); + mantissa |= (self.mid as u128) << 32; + mantissa |= (self.hi as u128) << 64; + // scale is at most 28, so this fits comfortably into a u128. + let scale = self.scale(); + let precision: u128 = 10_u128.pow(scale); + let integral_part = mantissa / precision; + let frac_part = mantissa % precision; + let frac_f64 = (frac_part as f64) / (precision as f64); + Some(sign * ((integral_part as f64) + frac_f64)) + } + } +} + +impl fmt::Display for Decimal { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + // Get the scale - where we need to put the decimal point + let mut scale = self.scale() as usize; + + // Convert to a string and manipulate that (neg at front, inject decimal) + let mut chars = Vec::new(); + let mut working = [self.lo, self.mid, self.hi]; + while !is_all_zero(&working) { + let remainder = div_by_u32(&mut working, 10u32); + chars.push(char::from(b'0' + remainder as u8)); + } + while scale > chars.len() { + chars.push('0'); + } + + let mut rep = chars.iter().rev().collect::(); + let len = rep.len(); + + if let Some(n_dp) = f.precision() { + if n_dp < scale { + rep.truncate(len - scale + n_dp) + } else { + let zeros = repeat("0").take(n_dp - scale).collect::(); + rep.push_str(&zeros[..]); + } + scale = n_dp; + } + let len = rep.len(); + + // Inject the decimal point + if scale > 0 { + // Must be a low fractional + // TODO: Remove this condition as it's no longer possible for `scale > len` + if scale > len { + let mut new_rep = String::new(); + let zeros = repeat("0").take(scale as usize - len).collect::(); + new_rep.push_str("0."); + new_rep.push_str(&zeros[..]); + new_rep.push_str(&rep[..]); + rep = new_rep; + } else if scale == len { + rep.insert(0, '.'); + rep.insert(0, '0'); + } else { + rep.insert(len - scale as usize, '.'); + } + } else if rep.is_empty() { + // corner case for when we truncated everything in a low fractional + rep.insert(0, '0'); + } + + f.pad_integral(self.is_sign_positive(), "", &rep) + } +} + +impl fmt::Debug for Decimal { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + fmt::Display::fmt(self, f) + } +} + +impl Neg for Decimal { + type Output = Decimal; + + fn neg(self) -> Decimal { + -&self + } +} + +impl<'a> Neg for &'a Decimal { + type Output = Decimal; + + fn neg(self) -> Decimal { + Decimal { + flags: flags(!self.is_sign_negative(), self.scale()), + hi: self.hi, + lo: self.lo, + mid: self.mid, + } + } +} + +forward_all_binop!(impl Add for Decimal, add); + +impl<'a, 'b> Add<&'b Decimal> for &'a Decimal { + type Output = Decimal; + + #[inline(always)] + fn add(self, other: &Decimal) -> Decimal { + match self.checked_add(*other) { + Some(sum) => sum, + None => panic!("Addition overflowed"), + } + } +} + +impl AddAssign for Decimal { + fn add_assign(&mut self, other: Decimal) { + let result = self.add(other); + self.lo = result.lo; + self.mid = result.mid; + self.hi = result.hi; + self.flags = result.flags; + } +} + +impl<'a> AddAssign<&'a Decimal> for Decimal { + fn add_assign(&mut self, other: &'a Decimal) { + Decimal::add_assign(self, *other) + } +} + +impl<'a> AddAssign for &'a mut Decimal { + fn add_assign(&mut self, other: Decimal) { + Decimal::add_assign(*self, other) + } +} + +impl<'a> AddAssign<&'a Decimal> for &'a mut Decimal { + fn add_assign(&mut self, other: &'a Decimal) { + Decimal::add_assign(*self, *other) + } +} + +forward_all_binop!(impl Sub for Decimal, sub); + +impl<'a, 'b> Sub<&'b Decimal> for &'a Decimal { + type Output = Decimal; + + #[inline(always)] + fn sub(self, other: &Decimal) -> Decimal { + match self.checked_sub(*other) { + Some(diff) => diff, + None => panic!("Subtraction overflowed"), + } + } +} + +impl SubAssign for Decimal { + fn sub_assign(&mut self, other: Decimal) { + let result = self.sub(other); + self.lo = result.lo; + self.mid = result.mid; + self.hi = result.hi; + self.flags = result.flags; + } +} + +impl<'a> SubAssign<&'a Decimal> for Decimal { + fn sub_assign(&mut self, other: &'a Decimal) { + Decimal::sub_assign(self, *other) + } +} + +impl<'a> SubAssign for &'a mut Decimal { + fn sub_assign(&mut self, other: Decimal) { + Decimal::sub_assign(*self, other) + } +} + +impl<'a> SubAssign<&'a Decimal> for &'a mut Decimal { + fn sub_assign(&mut self, other: &'a Decimal) { + Decimal::sub_assign(*self, *other) + } +} + +forward_all_binop!(impl Mul for Decimal, mul); + +impl<'a, 'b> Mul<&'b Decimal> for &'a Decimal { + type Output = Decimal; + + #[inline] + fn mul(self, other: &Decimal) -> Decimal { + match self.checked_mul(*other) { + Some(prod) => prod, + None => panic!("Multiplication overflowed"), + } + } +} + +impl MulAssign for Decimal { + fn mul_assign(&mut self, other: Decimal) { + let result = self.mul(other); + self.lo = result.lo; + self.mid = result.mid; + self.hi = result.hi; + self.flags = result.flags; + } +} + +impl<'a> MulAssign<&'a Decimal> for Decimal { + fn mul_assign(&mut self, other: &'a Decimal) { + Decimal::mul_assign(self, *other) + } +} + +impl<'a> MulAssign for &'a mut Decimal { + fn mul_assign(&mut self, other: Decimal) { + Decimal::mul_assign(*self, other) + } +} + +impl<'a> MulAssign<&'a Decimal> for &'a mut Decimal { + fn mul_assign(&mut self, other: &'a Decimal) { + Decimal::mul_assign(*self, *other) + } +} + +forward_all_binop!(impl Div for Decimal, div); + +impl<'a, 'b> Div<&'b Decimal> for &'a Decimal { + type Output = Decimal; + + fn div(self, other: &Decimal) -> Decimal { + match self.div_impl(*other) { + DivResult::Ok(quot) => quot, + DivResult::Overflow => panic!("Division overflowed"), + DivResult::DivByZero => panic!("Division by zero"), + } + } +} + +impl DivAssign for Decimal { + fn div_assign(&mut self, other: Decimal) { + let result = self.div(other); + self.lo = result.lo; + self.mid = result.mid; + self.hi = result.hi; + self.flags = result.flags; + } +} + +impl<'a> DivAssign<&'a Decimal> for Decimal { + fn div_assign(&mut self, other: &'a Decimal) { + Decimal::div_assign(self, *other) + } +} + +impl<'a> DivAssign for &'a mut Decimal { + fn div_assign(&mut self, other: Decimal) { + Decimal::div_assign(*self, other) + } +} + +impl<'a> DivAssign<&'a Decimal> for &'a mut Decimal { + fn div_assign(&mut self, other: &'a Decimal) { + Decimal::div_assign(*self, *other) + } +} + +forward_all_binop!(impl Rem for Decimal, rem); + +impl<'a, 'b> Rem<&'b Decimal> for &'a Decimal { + type Output = Decimal; + + #[inline] + fn rem(self, other: &Decimal) -> Decimal { + match self.checked_rem(*other) { + Some(rem) => rem, + None => panic!("Division by zero"), + } + } +} + +impl RemAssign for Decimal { + fn rem_assign(&mut self, other: Decimal) { + let result = self.rem(other); + self.lo = result.lo; + self.mid = result.mid; + self.hi = result.hi; + self.flags = result.flags; + } +} + +impl<'a> RemAssign<&'a Decimal> for Decimal { + fn rem_assign(&mut self, other: &'a Decimal) { + Decimal::rem_assign(self, *other) + } +} + +impl<'a> RemAssign for &'a mut Decimal { + fn rem_assign(&mut self, other: Decimal) { + Decimal::rem_assign(*self, other) + } +} + +impl<'a> RemAssign<&'a Decimal> for &'a mut Decimal { + fn rem_assign(&mut self, other: &'a Decimal) { + Decimal::rem_assign(*self, *other) + } +} + +impl PartialEq for Decimal { + #[inline] + fn eq(&self, other: &Decimal) -> bool { + self.cmp(other) == Equal + } +} + +impl Eq for Decimal {} + +impl Hash for Decimal { + fn hash(&self, state: &mut H) { + let n = self.normalize(); + n.lo.hash(state); + n.mid.hash(state); + n.hi.hash(state); + n.flags.hash(state); + } +} + +impl PartialOrd for Decimal { + #[inline] + fn partial_cmp(&self, other: &Decimal) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for Decimal { + fn cmp(&self, other: &Decimal) -> Ordering { + // Quick exit if major differences + let self_negative = self.is_sign_negative(); + let other_negative = other.is_sign_negative(); + if self_negative && !other_negative { + if self.is_zero() && other.is_zero() { + return Ordering::Equal; + } + return Ordering::Less; + } else if !self_negative && other_negative { + if self.is_zero() && other.is_zero() { + return Ordering::Equal; + } + return Ordering::Greater; + } + + // If we have 1.23 and 1.2345 then we have + // 123 scale 2 and 12345 scale 4 + // We need to convert the first to + // 12300 scale 4 so we can compare equally + let left: &Decimal; + let right: &Decimal; + if self_negative && other_negative { + // Both are negative, so reverse cmp + left = other; + right = self; + } else { + left = self; + right = other; + } + let mut left_scale = left.scale(); + let mut right_scale = right.scale(); + + if left_scale == right_scale { + // Fast path for same scale + if left.hi != right.hi { + return left.hi.cmp(&right.hi); + } + if left.mid != right.mid { + return left.mid.cmp(&right.mid); + } + return left.lo.cmp(&right.lo); + } + + // Rescale and compare + let mut left_raw = [left.lo, left.mid, left.hi]; + let mut right_raw = [right.lo, right.mid, right.hi]; + rescale_to_maximum_scale(&mut left_raw, &mut left_scale, &mut right_raw, &mut right_scale); + cmp_internal(&left_raw, &right_raw) + } +} + +impl Sum for Decimal { + fn sum>(iter: I) -> Self { + let mut sum = Decimal::zero(); + for i in iter { + sum += i; + } + sum + } +} + +#[cfg(test)] +mod test { + // Tests on private methods. + // + // All public tests should go under `tests/`. + + use super::*; + + #[test] + fn it_can_rescale_to_maximum_scale() { + fn extract(value: &str) -> ([u32; 3], u32) { + let v = Decimal::from_str(value).unwrap(); + ([v.lo, v.mid, v.hi], v.scale()) + } + + let tests = &[ + ("1", "1", "1", "1"), + ("1", "1.0", "1.0", "1.0"), + ("1", "1.00000", "1.00000", "1.00000"), + ("1", "1.0000000000", "1.0000000000", "1.0000000000"), + ( + "1", + "1.00000000000000000000", + "1.00000000000000000000", + "1.00000000000000000000", + ), + ("1.1", "1.1", "1.1", "1.1"), + ("1.1", "1.10000", "1.10000", "1.10000"), + ("1.1", "1.1000000000", "1.1000000000", "1.1000000000"), + ( + "1.1", + "1.10000000000000000000", + "1.10000000000000000000", + "1.10000000000000000000", + ), + ( + "0.6386554621848739495798319328", + "11.815126050420168067226890757", + "0.638655462184873949579831933", + "11.815126050420168067226890757", + ), + ( + "0.0872727272727272727272727272", // Scale 28 + "843.65000000", // Scale 8 + "0.0872727272727272727272727", // 25 + "843.6500000000000000000000000", // 25 + ), + ]; + + for &(left_raw, right_raw, expected_left, expected_right) in tests { + // Left = the value to rescale + // Right = the new scale we're scaling to + // Expected = the expected left value after rescale + let (expected_left, expected_lscale) = extract(expected_left); + let (expected_right, expected_rscale) = extract(expected_right); + + let (mut left, mut left_scale) = extract(left_raw); + let (mut right, mut right_scale) = extract(right_raw); + rescale_to_maximum_scale(&mut left, &mut left_scale, &mut right, &mut right_scale); + assert_eq!(left, expected_left); + assert_eq!(left_scale, expected_lscale); + assert_eq!(right, expected_right); + assert_eq!(right_scale, expected_rscale); + + // Also test the transitive case + let (mut left, mut left_scale) = extract(left_raw); + let (mut right, mut right_scale) = extract(right_raw); + rescale_to_maximum_scale(&mut right, &mut right_scale, &mut left, &mut left_scale); + assert_eq!(left, expected_left); + assert_eq!(left_scale, expected_lscale); + assert_eq!(right, expected_right); + assert_eq!(right_scale, expected_rscale); + } + } + + #[test] + fn it_can_rescale_internal() { + fn extract(value: &str) -> ([u32; 3], u32) { + let v = Decimal::from_str(value).unwrap(); + ([v.lo, v.mid, v.hi], v.scale()) + } + + let tests = &[ + ("1", 0, "1"), + ("1", 1, "1.0"), + ("1", 5, "1.00000"), + ("1", 10, "1.0000000000"), + ("1", 20, "1.00000000000000000000"), + ("0.6386554621848739495798319328", 27, "0.638655462184873949579831933"), + ( + "843.65000000", // Scale 8 + 25, // 25 + "843.6500000000000000000000000", // 25 + ), + ( + "843.65000000", // Scale 8 + 30, // 30 + "843.6500000000000000000000000000", // 28 + ), + ]; + + for &(value_raw, new_scale, expected_value) in tests { + let (expected_value, _) = extract(expected_value); + let (mut value, mut value_scale) = extract(value_raw); + rescale_internal(&mut value, &mut value_scale, new_scale); + assert_eq!(value, expected_value); + } + } +} diff --git a/third_party/rust/rust_decimal/src/error.rs b/third_party/rust/rust_decimal/src/error.rs new file mode 100644 index 000000000000..82bd7f0489c2 --- /dev/null +++ b/third_party/rust/rust_decimal/src/error.rs @@ -0,0 +1,31 @@ +use std::{error, fmt}; + +/// Error type for the library. +#[derive(Clone, Debug)] +pub struct Error { + message: String, +} + +impl Error { + /// Instantiate an error with the specified error message. + /// + /// This function is only available within the crate as there should never + /// be a need to create this error outside of the library. + pub(crate) fn new>(message: S) -> Error { + Error { + message: message.into(), + } + } +} + +impl error::Error for Error { + fn description(&self) -> &str { + &self.message + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + f.pad(&self.message) + } +} diff --git a/third_party/rust/rust_decimal/src/lib.rs b/third_party/rust/rust_decimal/src/lib.rs new file mode 100644 index 000000000000..cbcd931cd5cd --- /dev/null +++ b/third_party/rust/rust_decimal/src/lib.rs @@ -0,0 +1,56 @@ +//! +//! A Decimal implementation written in pure Rust suitable +//! for financial calculations that require significant integral +//! and fractional digits with no round-off errors. +//! +//! The binary representation consists of a 96 bit integer number, +//! a scaling factor used to specify the decimal fraction and a 1 +//! bit sign. Because of this representation, trailing zeros are +//! preserved and may be exposed when in string form. These can be +//! truncated using the `normalize` or `round_dp` functions. +//! +//! ## Usage +//! +//! Decimal numbers can be created in a few distinct ways, depending +//! on the rust compiler version you're targeting. +//! +//! The stable version of rust requires you to create a Decimal number +//! using one of it's convenience methods. +//! +//! ```rust +//! use rust_decimal::prelude::*; +//! +//! // Using an integer followed by the decimal points +//! let scaled = Decimal::new(202, 2); // 2.02 +//! +//! // From a string representation +//! let from_string = Decimal::from_str("2.02").unwrap(); // 2.02 +//! +//! // Using the `Into` trait +//! let my_int : Decimal = 3i32.into(); +//! +//! // Using the raw decimal representation +//! // 3.1415926535897932384626433832 +//! let pi = Decimal::from_parts(1102470952, 185874565, 1703060790, false, 28); +//! ``` +//! +mod decimal; +mod error; + +#[cfg(any(feature = "postgres", feature = "diesel"))] +mod postgres; +#[cfg(feature = "serde")] +mod serde_types; + +pub use decimal::{Decimal, RoundingStrategy}; +pub use error::Error; + +pub mod prelude { + pub use crate::{Decimal, RoundingStrategy}; + pub use num_traits::{FromPrimitive, One, ToPrimitive, Zero}; + pub use std::str::FromStr; +} + +#[cfg(feature = "diesel")] +#[macro_use] +extern crate diesel; diff --git a/third_party/rust/rust_decimal/src/postgres.rs b/third_party/rust/rust_decimal/src/postgres.rs new file mode 100644 index 000000000000..c34ed5df627b --- /dev/null +++ b/third_party/rust/rust_decimal/src/postgres.rs @@ -0,0 +1,856 @@ +use num_traits::Zero; + +use crate::Decimal; + +use std::{convert::TryInto, error, fmt, result::*}; + +use crate::decimal::{div_by_u32, is_all_zero, mul_by_u32, MAX_PRECISION}; + +#[derive(Debug, Clone)] +pub struct InvalidDecimal { + inner: Option, +} + +impl fmt::Display for InvalidDecimal { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + if let Some(ref msg) = self.inner { + fmt.write_fmt(format_args!("Invalid Decimal: {}", msg)) + } else { + fmt.write_str("Invalid Decimal") + } + } +} + +impl error::Error for InvalidDecimal {} + +struct PostgresDecimal { + neg: bool, + weight: i16, + scale: u16, + digits: D, +} + +impl Decimal { + fn from_postgres>( + PostgresDecimal { + neg, + scale, + digits, + weight, + }: PostgresDecimal, + ) -> Result { + let mut digits = digits.into_iter().collect::>(); + + let fractionals_part_count = digits.len() as i32 + (-weight as i32) - 1; + let integers_part_count = weight as i32 + 1; + + let mut result = Decimal::zero(); + // adding integer part + if integers_part_count > 0 { + let (start_integers, last) = if integers_part_count > digits.len() as i32 { + (integers_part_count - digits.len() as i32, digits.len() as i32) + } else { + (0, integers_part_count) + }; + let integers: Vec<_> = digits.drain(..last as usize).collect(); + for digit in integers { + result *= Decimal::from_i128_with_scale(10i128.pow(4), 0); + result += Decimal::new(digit as i64, 0); + } + result *= Decimal::from_i128_with_scale(10i128.pow(4 * start_integers as u32), 0); + } + // adding fractional part + if fractionals_part_count > 0 { + let dec: Vec<_> = digits.into_iter().collect(); + let start_fractionals = if weight < 0 { (-weight as u32) - 1 } else { 0 }; + for (i, digit) in dec.into_iter().enumerate() { + let fract_pow = 4 * (i as u32 + 1 + start_fractionals); + if fract_pow <= MAX_PRECISION { + result += Decimal::new(digit as i64, 0) / Decimal::from_i128_with_scale(10i128.pow(fract_pow), 0); + } else if fract_pow == MAX_PRECISION + 4 { + // rounding last digit + if digit >= 5000 { + result += + Decimal::new(1 as i64, 0) / Decimal::from_i128_with_scale(10i128.pow(MAX_PRECISION), 0); + } + } + } + } + + result.set_sign_negative(neg); + // Rescale to the postgres value, automatically rounding as needed. + result.rescale(scale as u32); + + Ok(result) + } + + fn to_postgres(self) -> PostgresDecimal> { + if self.is_zero() { + return PostgresDecimal { + neg: false, + weight: 0, + scale: 0, + digits: vec![0], + }; + } + let scale = self.scale() as u16; + + let groups_diff = scale & 0x3; // groups_diff = scale % 4 + + let mut mantissa = self.mantissa_array4(); + + if groups_diff > 0 { + let remainder = 4 - groups_diff; + let power = 10u32.pow(u32::from(remainder)); + mul_by_u32(&mut mantissa, power); + } + + // array to store max mantissa of Decimal in Postgres decimal format + const MAX_GROUP_COUNT: usize = 8; + let mut digits = Vec::with_capacity(MAX_GROUP_COUNT); + + while !is_all_zero(&mantissa) { + let digit = div_by_u32(&mut mantissa, 10000) as u16; + digits.push(digit.try_into().unwrap()); + } + digits.reverse(); + let digits_after_decimal = (scale + 3) as u16 / 4; + let weight = digits.len() as i16 - digits_after_decimal as i16 - 1; + + let unnecessary_zeroes = if weight >= 0 { + let index_of_decimal = (weight + 1) as usize; + digits + .get(index_of_decimal..) + .expect("enough digits exist") + .iter() + .rev() + .take_while(|i| **i == 0) + .count() + } else { + 0 + }; + let relevant_digits = digits.len() - unnecessary_zeroes; + digits.truncate(relevant_digits); + + PostgresDecimal { + neg: self.is_sign_negative(), + digits, + scale, + weight, + } + } +} + +#[cfg(feature = "diesel")] +mod diesel { + use super::*; + + use ::diesel::{ + deserialize::{self, FromSql}, + pg::data_types::PgNumeric, + pg::Pg, + serialize::{self, Output, ToSql}, + sql_types::Numeric, + }; + use ::std::{ + convert::{TryFrom, TryInto}, + io::Write, + }; + + impl<'a> TryFrom<&'a PgNumeric> for Decimal { + type Error = Box; + + fn try_from(numeric: &'a PgNumeric) -> deserialize::Result { + let (neg, weight, scale, digits) = match *numeric { + PgNumeric::Positive { + weight, + scale, + ref digits, + } => (false, weight, scale, digits), + PgNumeric::Negative { + weight, + scale, + ref digits, + } => (true, weight, scale, digits), + PgNumeric::NaN => return Err(Box::from("NaN is not supported in Decimal")), + }; + + Ok(Self::from_postgres(PostgresDecimal { + neg, + weight, + scale, + digits: digits.iter().copied().map(|v| v.try_into().unwrap()), + }) + .map_err(Box::new)?) + } + } + + impl TryFrom for Decimal { + type Error = Box; + + fn try_from(numeric: PgNumeric) -> deserialize::Result { + (&numeric).try_into() + } + } + + impl<'a> From<&'a Decimal> for PgNumeric { + // NOTE(clippy): Clippy suggests to replace the `.take_while(|i| i.is_zero())` + // with `.take_while(Zero::is_zero)`, but that's a false positive. + // The closure gets an `&&i16` due to autoderef `::is_zero(&self) -> bool` + // is called. There is no impl for `&i16` that would work with this closure. + #[allow(clippy::assign_op_pattern, clippy::redundant_closure)] + fn from(decimal: &'a Decimal) -> Self { + let PostgresDecimal { + neg, + weight, + scale, + digits, + } = decimal.to_postgres(); + + let digits = digits.into_iter().map(|v| v.try_into().unwrap()).collect(); + + if neg { + PgNumeric::Negative { digits, scale, weight } + } else { + PgNumeric::Positive { digits, scale, weight } + } + } + } + + impl From for PgNumeric { + fn from(bigdecimal: Decimal) -> Self { + (&bigdecimal).into() + } + } + + impl ToSql for Decimal { + fn to_sql(&self, out: &mut Output) -> serialize::Result { + let numeric = PgNumeric::from(self); + ToSql::::to_sql(&numeric, out) + } + } + + impl FromSql for Decimal { + fn from_sql(numeric: Option<&[u8]>) -> deserialize::Result { + PgNumeric::from_sql(numeric)?.try_into() + } + } + + #[cfg(test)] + mod pg_tests { + use super::*; + use std::str::FromStr; + + #[test] + fn test_unnecessary_zeroes() { + fn extract(value: &str) -> Decimal { + Decimal::from_str(value).unwrap() + } + + let tests = &[ + ("0.000001660"), + ("41.120255926293000"), + ("0.5538973300"), + ("08883.55986854293100"), + ("0.0000_0000_0016_6000_00"), + ("0.00000166650000"), + ("1666500000000"), + ("1666500000000.0000054500"), + ("8944.000000000000"), + ]; + + for &value in tests { + let value = extract(value); + let pg = PgNumeric::from(value); + let dec = Decimal::try_from(pg).unwrap(); + assert_eq!(dec, value); + } + } + + #[test] + fn decimal_to_pgnumeric_converts_digits_to_base_10000() { + let decimal = Decimal::from_str("1").unwrap(); + let expected = PgNumeric::Positive { + weight: 0, + scale: 0, + digits: vec![1], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("10").unwrap(); + let expected = PgNumeric::Positive { + weight: 0, + scale: 0, + digits: vec![10], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("10000").unwrap(); + let expected = PgNumeric::Positive { + weight: 1, + scale: 0, + digits: vec![1, 0], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("10001").unwrap(); + let expected = PgNumeric::Positive { + weight: 1, + scale: 0, + digits: vec![1, 1], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("100000000").unwrap(); + let expected = PgNumeric::Positive { + weight: 2, + scale: 0, + digits: vec![1, 0, 0], + }; + assert_eq!(expected, decimal.into()); + } + + #[test] + fn decimal_to_pg_numeric_properly_adjusts_scale() { + let decimal = Decimal::from_str("1").unwrap(); + let expected = PgNumeric::Positive { + weight: 0, + scale: 0, + digits: vec![1], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("1.0").unwrap(); + let expected = PgNumeric::Positive { + weight: 0, + scale: 1, + digits: vec![1], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("1.1").unwrap(); + let expected = PgNumeric::Positive { + weight: 0, + scale: 1, + digits: vec![1, 1000], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("1.10").unwrap(); + let expected = PgNumeric::Positive { + weight: 0, + scale: 2, + digits: vec![1, 1000], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("100000000.0001").unwrap(); + let expected = PgNumeric::Positive { + weight: 2, + scale: 4, + digits: vec![1, 0, 0, 1], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("0.1").unwrap(); + let expected = PgNumeric::Positive { + weight: -1, + scale: 1, + digits: vec![1000], + }; + assert_eq!(expected, decimal.into()); + } + + #[test] + #[cfg(feature = "unstable")] + fn decimal_to_pg_numeric_retains_sign() { + let decimal = Decimal::from_str("123.456").unwrap(); + let expected = PgNumeric::Positive { + weight: 0, + scale: 3, + digits: vec![123, 4560], + }; + assert_eq!(expected, decimal.into()); + + let decimal = Decimal::from_str("-123.456").unwrap(); + let expected = PgNumeric::Negative { + weight: 0, + scale: 3, + digits: vec![123, 4560], + }; + assert_eq!(expected, decimal.into()); + } + + #[test] + fn pg_numeric_to_decimal_works() { + let expected = Decimal::from_str("50").unwrap(); + let pg_numeric = PgNumeric::Positive { + weight: 0, + scale: 0, + digits: vec![50], + }; + let res: Decimal = pg_numeric.try_into().unwrap(); + assert_eq!(res, expected); + let expected = Decimal::from_str("123.456").unwrap(); + let pg_numeric = PgNumeric::Positive { + weight: 0, + scale: 3, + digits: vec![123, 4560], + }; + let res: Decimal = pg_numeric.try_into().unwrap(); + assert_eq!(res, expected); + + let expected = Decimal::from_str("-56.78").unwrap(); + let pg_numeric = PgNumeric::Negative { + weight: 0, + scale: 2, + digits: vec![56, 7800], + }; + let res: Decimal = pg_numeric.try_into().unwrap(); + assert_eq!(res, expected); + + // Verify no trailing zeroes are lost. + + let expected = Decimal::from_str("1.100").unwrap(); + let pg_numeric = PgNumeric::Positive { + weight: 0, + scale: 3, + digits: vec![1, 1000], + }; + let res: Decimal = pg_numeric.try_into().unwrap(); + assert_eq!(res.to_string(), expected.to_string()); + + // To represent 5.00, Postgres can return either [5, 0] as the list of digits. + let expected = Decimal::from_str("5.00").unwrap(); + let pg_numeric = PgNumeric::Positive { + weight: 0, + scale: 2, + + digits: vec![5, 0], + }; + let res: Decimal = pg_numeric.try_into().unwrap(); + assert_eq!(res.to_string(), expected.to_string()); + + // To represent 5.00, Postgres can return [5] as the list of digits. + let expected = Decimal::from_str("5.00").unwrap(); + let pg_numeric = PgNumeric::Positive { + weight: 0, + scale: 2, + digits: vec![5], + }; + let res: Decimal = pg_numeric.try_into().unwrap(); + assert_eq!(res.to_string(), expected.to_string()); + + let expected = Decimal::from_str("3.1415926535897932384626433833").unwrap(); + let pg_numeric = PgNumeric::Positive { + weight: 0, + scale: 30, + digits: vec![3, 1415, 9265, 3589, 7932, 3846, 2643, 3832, 7950, 2800], + }; + let res: Decimal = pg_numeric.try_into().unwrap(); + assert_eq!(res.to_string(), expected.to_string()); + + let expected = Decimal::from_str("3.1415926535897932384626433833").unwrap(); + let pg_numeric = PgNumeric::Positive { + weight: 0, + scale: 34, + digits: vec![3, 1415, 9265, 3589, 7932, 3846, 2643, 3832, 7950, 2800], + }; + + let res: Decimal = pg_numeric.try_into().unwrap(); + assert_eq!(res.to_string(), expected.to_string()); + + let expected = Decimal::from_str("1.2345678901234567890123456790").unwrap(); + let pg_numeric = PgNumeric::Positive { + weight: 0, + scale: 34, + digits: vec![1, 2345, 6789, 0123, 4567, 8901, 2345, 6789, 5000, 0], + }; + + let res: Decimal = pg_numeric.try_into().unwrap(); + assert_eq!(res.to_string(), expected.to_string()); + } + } +} + +#[cfg(feature = "postgres")] +mod postgres { + use super::*; + + use ::byteorder::{BigEndian, ReadBytesExt}; + use ::bytes::{BufMut, BytesMut}; + use ::postgres::types::*; + use ::std::io::Cursor; + + impl<'a> FromSql<'a> for Decimal { + // Decimals are represented as follows: + // Header: + // u16 numGroups + // i16 weightFirstGroup (10000^weight) + // u16 sign (0x0000 = positive, 0x4000 = negative, 0xC000 = NaN) + // i16 dscale. Number of digits (in base 10) to print after decimal separator + // + // Psuedo code : + // const Decimals [ + // 0.0000000000000000000000000001, + // 0.000000000000000000000001, + // 0.00000000000000000001, + // 0.0000000000000001, + // 0.000000000001, + // 0.00000001, + // 0.0001, + // 1, + // 10000, + // 100000000, + // 1000000000000, + // 10000000000000000, + // 100000000000000000000, + // 1000000000000000000000000, + // 10000000000000000000000000000 + // ] + // overflow = false + // result = 0 + // for i = 0, weight = weightFirstGroup + 7; i < numGroups; i++, weight-- + // group = read.u16 + // if weight < 0 or weight > MaxNum + // overflow = true + // else + // result += Decimals[weight] * group + // sign == 0x4000 ? -result : result + + // So if we were to take the number: 3950.123456 + // + // Stored on Disk: + // 00 03 00 00 00 00 00 06 0F 6E 04 D2 15 E0 + // + // Number of groups: 00 03 + // Weight of first group: 00 00 + // Sign: 00 00 + // DScale: 00 06 + // + // 0F 6E = 3950 + // result = result + 3950 * 1; + // 04 D2 = 1234 + // result = result + 1234 * 0.0001; + // 15 E0 = 5600 + // result = result + 5600 * 0.00000001; + // + + fn from_sql(_: &Type, raw: &[u8]) -> Result> { + let mut raw = Cursor::new(raw); + let num_groups = raw.read_u16::()?; + let weight = raw.read_i16::()?; // 10000^weight + // Sign: 0x0000 = positive, 0x4000 = negative, 0xC000 = NaN + let sign = raw.read_u16::()?; + // Number of digits (in base 10) to print after decimal separator + let scale = raw.read_u16::()?; + + // Read all of the groups + let mut groups = Vec::new(); + for _ in 0..num_groups as usize { + groups.push(raw.read_u16::()?); + } + + Ok(Self::from_postgres(PostgresDecimal { + neg: sign == 0x4000, + weight, + scale, + digits: groups.into_iter(), + }) + .map_err(Box::new)?) + } + + fn accepts(ty: &Type) -> bool { + match ty { + &Type::NUMERIC => true, + _ => false, + } + } + } + + impl ToSql for Decimal { + fn to_sql( + &self, + _: &Type, + out: &mut BytesMut, + ) -> Result> { + let PostgresDecimal { + neg, + weight, + scale, + digits, + } = self.to_postgres(); + + let num_digits = digits.len(); + + // Reserve bytes + out.reserve(8 + num_digits * 2); + + // Number of groups + out.put_u16(num_digits.try_into().unwrap()); + // Weight of first group + out.put_i16(weight); + // Sign + out.put_u16(if neg { 0x4000 } else { 0x0000 }); + // DScale + out.put_u16(scale); + // Now process the number + for digit in digits[0..num_digits].iter() { + out.put_i16(*digit); + } + + Ok(IsNull::No) + } + + fn accepts(ty: &Type) -> bool { + match ty { + &Type::NUMERIC => true, + _ => false, + } + } + + to_sql_checked!(); + } + + #[cfg(test)] + mod test { + use super::*; + + use ::postgres::{Client, NoTls}; + + use std::str::FromStr; + + /// Gets the URL for connecting to PostgreSQL for testing. Set the POSTGRES_URL + /// environment variable to change from the default of "postgres://postgres@localhost". + fn get_postgres_url() -> String { + if let Ok(url) = std::env::var("POSTGRES_URL") { + return url; + } + "postgres://postgres@localhost".to_string() + } + + pub static TEST_DECIMALS: &[(u32, u32, &str, &str)] = &[ + // precision, scale, sent, expected + (35, 6, "3950.123456", "3950.123456"), + (35, 2, "3950.123456", "3950.12"), + (35, 2, "3950.1256", "3950.13"), + (10, 2, "3950.123456", "3950.12"), + (35, 6, "3950", "3950.000000"), + (4, 0, "3950", "3950"), + (35, 6, "0.1", "0.100000"), + (35, 6, "0.01", "0.010000"), + (35, 6, "0.001", "0.001000"), + (35, 6, "0.0001", "0.000100"), + (35, 6, "0.00001", "0.000010"), + (35, 6, "0.000001", "0.000001"), + (35, 6, "1", "1.000000"), + (35, 6, "-100", "-100.000000"), + (35, 6, "-123.456", "-123.456000"), + (35, 6, "119996.25", "119996.250000"), + (35, 6, "1000000", "1000000.000000"), + (35, 6, "9999999.99999", "9999999.999990"), + (35, 6, "12340.56789", "12340.567890"), + // Scale is only 28 since that is the maximum we can represent. + (65, 30, "1.2", "1.2000000000000000000000000000"), + // Pi - rounded at scale 28 + ( + 65, + 30, + "3.141592653589793238462643383279", + "3.1415926535897932384626433833", + ), + ( + 65, + 34, + "3.1415926535897932384626433832795028", + "3.1415926535897932384626433833", + ), + // Unrounded number + ( + 65, + 34, + "1.234567890123456789012345678950000", + "1.2345678901234567890123456790", + ), + ( + 65, + 34, // No rounding due to 49999 after significant digits + "1.234567890123456789012345678949999", + "1.2345678901234567890123456789", + ), + // 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF (96 bit) + (35, 0, "79228162514264337593543950335", "79228162514264337593543950335"), + // 0x0FFF_FFFF_FFFF_FFFF_FFFF_FFFF (95 bit) + (35, 1, "4951760157141521099596496895", "4951760157141521099596496895.0"), + // 0x1000_0000_0000_0000_0000_0000 + (35, 1, "4951760157141521099596496896", "4951760157141521099596496896.0"), + (35, 6, "18446744073709551615", "18446744073709551615.000000"), + (35, 6, "-18446744073709551615", "-18446744073709551615.000000"), + (35, 6, "0.10001", "0.100010"), + (35, 6, "0.12345", "0.123450"), + ]; + + #[test] + fn test_null() { + let mut client = match Client::connect(&get_postgres_url(), NoTls) { + Ok(x) => x, + Err(err) => panic!("{:#?}", err), + }; + + // Test NULL + let result: Option = match client.query("SELECT NULL::numeric", &[]) { + Ok(x) => x.iter().next().unwrap().get(0), + Err(err) => panic!("{:#?}", err), + }; + assert_eq!(None, result); + } + + #[tokio::test] + #[cfg(feature = "tokio-pg")] + async fn async_test_null() { + use ::futures::future::FutureExt; + use ::tokio_postgres::connect; + + let (client, connection) = connect(&get_postgres_url(), NoTls).await.unwrap(); + let connection = connection.map(|e| e.unwrap()); + tokio::spawn(connection); + + let statement = client.prepare(&"SELECT NULL::numeric").await.unwrap(); + let rows = client.query(&statement, &[]).await.unwrap(); + let result: Option = rows.iter().next().unwrap().get(0); + + assert_eq!(None, result); + } + + #[test] + fn read_numeric_type() { + let mut client = match Client::connect(&get_postgres_url(), NoTls) { + Ok(x) => x, + Err(err) => panic!("{:#?}", err), + }; + for &(precision, scale, sent, expected) in TEST_DECIMALS.iter() { + let result: Decimal = + match client.query(&*format!("SELECT {}::NUMERIC({}, {})", sent, precision, scale), &[]) { + Ok(x) => x.iter().next().unwrap().get(0), + Err(err) => panic!("SELECT {}::NUMERIC({}, {}), error - {:#?}", sent, precision, scale, err), + }; + assert_eq!( + expected, + result.to_string(), + "NUMERIC({}, {}) sent: {}", + precision, + scale, + sent + ); + } + } + + #[tokio::test] + #[cfg(feature = "tokio-pg")] + async fn async_read_numeric_type() { + use ::futures::future::FutureExt; + use ::tokio_postgres::connect; + + let (client, connection) = connect(&get_postgres_url(), NoTls).await.unwrap(); + let connection = connection.map(|e| e.unwrap()); + tokio::spawn(connection); + for &(precision, scale, sent, expected) in TEST_DECIMALS.iter() { + let statement = client + .prepare(&*format!("SELECT {}::NUMERIC({}, {})", sent, precision, scale)) + .await + .unwrap(); + let rows = client.query(&statement, &[]).await.unwrap(); + let result: Decimal = rows.iter().next().unwrap().get(0); + + assert_eq!(expected, result.to_string(), "NUMERIC({}, {})", precision, scale); + } + } + + #[test] + fn write_numeric_type() { + let mut client = match Client::connect(&get_postgres_url(), NoTls) { + Ok(x) => x, + Err(err) => panic!("{:#?}", err), + }; + for &(precision, scale, sent, expected) in TEST_DECIMALS.iter() { + let number = Decimal::from_str(sent).unwrap(); + let result: Decimal = + match client.query(&*format!("SELECT $1::NUMERIC({}, {})", precision, scale), &[&number]) { + Ok(x) => x.iter().next().unwrap().get(0), + Err(err) => panic!("{:#?}", err), + }; + assert_eq!(expected, result.to_string(), "NUMERIC({}, {})", precision, scale); + } + } + + #[tokio::test] + #[cfg(feature = "tokio-pg")] + async fn async_write_numeric_type() { + use ::futures::future::FutureExt; + use ::tokio_postgres::connect; + + let (client, connection) = connect(&get_postgres_url(), NoTls).await.unwrap(); + let connection = connection.map(|e| e.unwrap()); + tokio::spawn(connection); + + for &(precision, scale, sent, expected) in TEST_DECIMALS.iter() { + let statement = client + .prepare(&*format!("SELECT $1::NUMERIC({}, {})", precision, scale)) + .await + .unwrap(); + let number = Decimal::from_str(sent).unwrap(); + let rows = client.query(&statement, &[&number]).await.unwrap(); + let result: Decimal = rows.iter().next().unwrap().get(0); + + assert_eq!(expected, result.to_string(), "NUMERIC({}, {})", precision, scale); + } + } + + #[test] + fn numeric_overflow() { + let tests = [(4, 4, "3950.1234")]; + let mut client = match Client::connect(&get_postgres_url(), NoTls) { + Ok(x) => x, + Err(err) => panic!("{:#?}", err), + }; + for &(precision, scale, sent) in tests.iter() { + match client.query(&*format!("SELECT {}::NUMERIC({}, {})", sent, precision, scale), &[]) { + Ok(_) => panic!( + "Expected numeric overflow for {}::NUMERIC({}, {})", + sent, precision, scale + ), + Err(err) => { + assert_eq!("22003", err.code().unwrap().code(), "Unexpected error code"); + } + }; + } + } + + #[tokio::test] + #[cfg(feature = "tokio-pg")] + async fn async_numeric_overflow() { + use ::futures::future::FutureExt; + use ::tokio_postgres::connect; + + let tests = [(4, 4, "3950.1234")]; + let (client, connection) = connect(&get_postgres_url(), NoTls).await.unwrap(); + let connection = connection.map(|e| e.unwrap()); + tokio::spawn(connection); + + for &(precision, scale, sent) in tests.iter() { + let statement = client + .prepare(&*format!("SELECT {}::NUMERIC({}, {})", sent, precision, scale)) + .await + .unwrap(); + + match client.query(&statement, &[]).await { + Ok(_) => panic!( + "Expected numeric overflow for {}::NUMERIC({}, {})", + sent, precision, scale + ), + Err(err) => assert_eq!("22003", err.code().unwrap().code(), "Unexpected error code"), + } + } + } + } +} diff --git a/third_party/rust/rust_decimal/src/serde_types.rs b/third_party/rust/rust_decimal/src/serde_types.rs new file mode 100644 index 000000000000..a19174973fb1 --- /dev/null +++ b/third_party/rust/rust_decimal/src/serde_types.rs @@ -0,0 +1,218 @@ +use crate::Decimal; + +use num_traits::FromPrimitive; + +use serde::{self, de::Unexpected}; + +use std::{fmt, str::FromStr}; + +#[cfg(not(feature = "serde-bincode"))] +impl<'de> serde::Deserialize<'de> for Decimal { + fn deserialize(deserializer: D) -> Result + where + D: serde::de::Deserializer<'de>, + { + deserializer.deserialize_any(DecimalVisitor) + } +} + +#[cfg(all(feature = "serde-bincode", not(feature = "serde-float")))] +impl<'de> serde::Deserialize<'de> for Decimal { + fn deserialize(deserializer: D) -> Result + where + D: serde::de::Deserializer<'de>, + { + deserializer.deserialize_str(DecimalVisitor) + } +} + +#[cfg(all(feature = "serde-bincode", feature = "serde-float"))] +impl<'de> serde::Deserialize<'de> for Decimal { + fn deserialize(deserializer: D) -> Result + where + D: serde::de::Deserializer<'de>, + { + deserializer.deserialize_f64(DecimalVisitor) + } +} + +struct DecimalVisitor; + +impl<'de> serde::de::Visitor<'de> for DecimalVisitor { + type Value = Decimal; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + write!(formatter, "a Decimal type representing a fixed-point number") + } + + fn visit_i64(self, value: i64) -> Result + where + E: serde::de::Error, + { + match Decimal::from_i64(value) { + Some(s) => Ok(s), + None => Err(E::invalid_value(Unexpected::Signed(value), &self)), + } + } + + fn visit_u64(self, value: u64) -> Result + where + E: serde::de::Error, + { + match Decimal::from_u64(value) { + Some(s) => Ok(s), + None => Err(E::invalid_value(Unexpected::Unsigned(value), &self)), + } + } + + fn visit_f64(self, value: f64) -> Result + where + E: serde::de::Error, + { + Decimal::from_str(&value.to_string()).map_err(|_| E::invalid_value(Unexpected::Float(value), &self)) + } + + fn visit_str(self, value: &str) -> Result + where + E: serde::de::Error, + { + Decimal::from_str(value) + .or_else(|_| Decimal::from_scientific(value)) + .map_err(|_| E::invalid_value(Unexpected::Str(value), &self)) + } +} + +#[cfg(not(feature = "serde-float"))] +impl serde::Serialize for Decimal { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} + +#[cfg(feature = "serde-float")] +impl serde::Serialize for Decimal { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + use num_traits::ToPrimitive; + serializer.serialize_f64(self.to_f64().unwrap()) + } +} + +#[cfg(test)] +mod test { + + use super::*; + + use serde_derive::{Deserialize, Serialize}; + + #[derive(Serialize, Deserialize, Debug)] + struct Record { + amount: Decimal, + } + + #[test] + #[cfg(not(feature = "serde-bincode"))] + fn deserialize_valid_decimal() { + let data = [ + ("{\"amount\":\"1.234\"}", "1.234"), + ("{\"amount\":1234}", "1234"), + ("{\"amount\":1234.56}", "1234.56"), + ("{\"amount\":\"1.23456e3\"}", "1234.56"), + ]; + for &(serialized, value) in data.iter() { + let result = serde_json::from_str(serialized); + assert_eq!( + true, + result.is_ok(), + "expected successful deseralization for {}. Error: {:?}", + serialized, + result.err().unwrap() + ); + let record: Record = result.unwrap(); + assert_eq!( + value, + record.amount.to_string(), + "expected: {}, actual: {}", + value, + record.amount.to_string() + ); + } + } + + #[test] + #[should_panic] + fn deserialize_invalid_decimal() { + let serialized = "{\"amount\":\"foo\"}"; + let _: Record = serde_json::from_str(serialized).unwrap(); + } + + #[test] + #[cfg(not(feature = "serde-float"))] + fn serialize_decimal() { + let record = Record { + amount: Decimal::new(1234, 3), + }; + let serialized = serde_json::to_string(&record).unwrap(); + assert_eq!("{\"amount\":\"1.234\"}", serialized); + } + + #[test] + #[cfg(feature = "serde-float")] + fn serialize_decimal() { + let record = Record { + amount: Decimal::new(1234, 3), + }; + let serialized = serde_json::to_string(&record).unwrap(); + assert_eq!("{\"amount\":1.234}", serialized); + } + + #[test] + #[cfg(all(feature = "serde-bincode", not(feature = "serde-float")))] + fn bincode_serialization() { + use bincode::{deserialize, serialize}; + + let data = [ + "0", + "0.00", + "3.14159", + "-3.14159", + "1234567890123.4567890", + "-1234567890123.4567890", + ]; + for &raw in data.iter() { + let value = Decimal::from_str(raw).unwrap(); + let encoded = serialize(&value).unwrap(); + let decoded: Decimal = deserialize(&encoded[..]).unwrap(); + assert_eq!(value, decoded); + assert_eq!(8usize + raw.len(), encoded.len()); + } + } + + #[test] + #[cfg(all(feature = "serde-bincode", feature = "serde-float"))] + fn bincode_serialization() { + use bincode::{deserialize, serialize}; + + let data = [ + ("0", "0"), + ("0.00", "0.00"), + ("3.14159", "3.14159"), + ("-3.14159", "-3.14159"), + ("1234567890123.4567890", "1234567890123.4568"), + ("-1234567890123.4567890", "-1234567890123.4568"), + ]; + for &(value, expected) in data.iter() { + let value = Decimal::from_str(value).unwrap(); + let expected = Decimal::from_str(expected).unwrap(); + let encoded = serialize(&value).unwrap(); + let decoded: Decimal = deserialize(&encoded[..]).unwrap(); + assert_eq!(expected, decoded); + assert_eq!(8usize, encoded.len()); + } + } +} diff --git a/third_party/rust/rust_decimal/tests/decimal_tests.rs b/third_party/rust/rust_decimal/tests/decimal_tests.rs new file mode 100644 index 000000000000..4517254e3547 --- /dev/null +++ b/third_party/rust/rust_decimal/tests/decimal_tests.rs @@ -0,0 +1,1633 @@ +use num_traits::{Signed, ToPrimitive, Zero}; + +use rust_decimal::{Decimal, RoundingStrategy}; + +use std::{ + cmp::{Ordering, Ordering::*}, + str::FromStr, +}; + +// Parsing + +#[test] +fn it_creates_a_new_negative_decimal() { + let a = Decimal::new(-100, 2); + assert_eq!(a.is_sign_negative(), true); + assert_eq!(a.scale(), 2); + assert_eq!("-1.00", a.to_string()); +} + +#[test] +fn it_creates_a_new_decimal_using_numeric_boundaries() { + let a = Decimal::new(i64::max_value(), 2); + assert_eq!(a.is_sign_negative(), false); + assert_eq!(a.scale(), 2); + assert_eq!("92233720368547758.07", a.to_string()); + + let b = Decimal::new(i64::min_value(), 2); + assert_eq!(b.is_sign_negative(), true); + assert_eq!(b.scale(), 2); + assert_eq!("-92233720368547758.08", b.to_string()); +} + +#[test] +fn it_parses_empty_string() { + assert!(Decimal::from_str("").is_err()); + assert!(Decimal::from_str(" ").is_err()); +} + +#[test] +fn it_parses_positive_int_string() { + let a = Decimal::from_str("233").unwrap(); + assert_eq!(a.is_sign_negative(), false); + assert_eq!(a.scale(), 0); + assert_eq!("233", a.to_string()); +} + +#[test] +fn it_parses_negative_int_string() { + let a = Decimal::from_str("-233").unwrap(); + assert_eq!(a.is_sign_negative(), true); + assert_eq!(a.scale(), 0); + println!("to_string"); + assert_eq!("-233", a.to_string()); +} + +#[test] +fn it_parses_positive_float_string() { + let a = Decimal::from_str("233.323223").unwrap(); + assert_eq!(a.is_sign_negative(), false); + assert_eq!(a.scale(), 6); + assert_eq!("233.323223", a.to_string()); +} + +#[test] +fn it_parses_negative_float_string() { + let a = Decimal::from_str("-233.43343").unwrap(); + assert_eq!(a.is_sign_negative(), true); + assert_eq!(a.scale(), 5); + assert_eq!("-233.43343", a.to_string()); +} + +#[test] +fn it_parses_positive_tiny_float_string() { + let a = Decimal::from_str(".000001").unwrap(); + assert_eq!(a.is_sign_negative(), false); + assert_eq!(a.scale(), 6); + assert_eq!("0.000001", a.to_string()); +} + +#[test] +fn it_parses_negative_tiny_float_string() { + let a = Decimal::from_str("-0.000001").unwrap(); + assert_eq!(a.is_sign_negative(), true); + assert_eq!(a.scale(), 6); + assert_eq!("-0.000001", a.to_string()); +} + +#[test] +fn it_parses_big_integer_string() { + let a = Decimal::from_str("79228162514264337593543950330").unwrap(); + assert_eq!("79228162514264337593543950330", a.to_string()); +} + +#[test] +fn it_parses_big_float_string() { + let a = Decimal::from_str("79.228162514264337593543950330").unwrap(); + assert_eq!("79.228162514264337593543950330", a.to_string()); +} + +#[test] +fn it_can_serialize_deserialize() { + let a = Decimal::from_str("12.3456789").unwrap(); + let bytes = a.serialize(); + let b = Decimal::deserialize(bytes); + assert_eq!("12.3456789", b.to_string()); +} + +// Formatting + +#[test] +fn it_formats() { + let a = Decimal::from_str("233.323223").unwrap(); + assert_eq!(format!("{}", a), "233.323223"); + assert_eq!(format!("{:.9}", a), "233.323223000"); + assert_eq!(format!("{:.0}", a), "233"); + assert_eq!(format!("{:.2}", a), "233.32"); + assert_eq!(format!("{:010.2}", a), "0000233.32"); + assert_eq!(format!("{:0<10.2}", a), "233.320000"); +} +#[test] +fn it_formats_neg() { + let a = Decimal::from_str("-233.323223").unwrap(); + assert_eq!(format!("{}", a), "-233.323223"); + assert_eq!(format!("{:.9}", a), "-233.323223000"); + assert_eq!(format!("{:.0}", a), "-233"); + assert_eq!(format!("{:.2}", a), "-233.32"); + assert_eq!(format!("{:010.2}", a), "-000233.32"); + assert_eq!(format!("{:0<10.2}", a), "-233.32000"); +} +#[test] +fn it_formats_small() { + let a = Decimal::from_str("0.2223").unwrap(); + assert_eq!(format!("{}", a), "0.2223"); + assert_eq!(format!("{:.9}", a), "0.222300000"); + assert_eq!(format!("{:.0}", a), "0"); + assert_eq!(format!("{:.2}", a), "0.22"); + assert_eq!(format!("{:010.2}", a), "0000000.22"); + assert_eq!(format!("{:0<10.2}", a), "0.22000000"); +} +#[test] +fn it_formats_small_leading_zeros() { + let a = Decimal::from_str("0.0023554701772169").unwrap(); + assert_eq!(format!("{}", a), "0.0023554701772169"); + assert_eq!(format!("{:.9}", a), "0.002355470"); + assert_eq!(format!("{:.0}", a), "0"); + assert_eq!(format!("{:.2}", a), "0.00"); + assert_eq!(format!("{:010.2}", a), "0000000.00"); + assert_eq!(format!("{:0<10.2}", a), "0.00000000"); +} +#[test] +fn it_formats_small_neg() { + let a = Decimal::from_str("-0.2223").unwrap(); + assert_eq!(format!("{}", a), "-0.2223"); + assert_eq!(format!("{:.9}", a), "-0.222300000"); + assert_eq!(format!("{:.0}", a), "-0"); + assert_eq!(format!("{:.2}", a), "-0.22"); + assert_eq!(format!("{:010.2}", a), "-000000.22"); + assert_eq!(format!("{:0<10.2}", a), "-0.2200000"); +} +#[test] +fn it_formats_zero() { + let a = Decimal::from_str("0").unwrap(); + assert_eq!(format!("{}", a), "0"); + assert_eq!(format!("{:.9}", a), "0.000000000"); + assert_eq!(format!("{:.0}", a), "0"); + assert_eq!(format!("{:.2}", a), "0.00"); + assert_eq!(format!("{:010.2}", a), "0000000.00"); + assert_eq!(format!("{:0<10.2}", a), "0.00000000"); +} +#[test] +fn it_formats_int() { + let a = Decimal::from_str("5").unwrap(); + assert_eq!(format!("{}", a), "5"); + assert_eq!(format!("{:.9}", a), "5.000000000"); + assert_eq!(format!("{:.0}", a), "5"); + assert_eq!(format!("{:.2}", a), "5.00"); + assert_eq!(format!("{:010.2}", a), "0000005.00"); + assert_eq!(format!("{:0<10.2}", a), "5.00000000"); +} + +// Negation +#[test] +fn it_negates_decimals() { + fn neg(a: &str, b: &str) { + let a = Decimal::from_str(a).unwrap(); + let result = -a; + assert_eq!(b, result.to_string(), "- {}", a.to_string()); + } + + let tests = &[ + ("1", "-1"), + ("2", "-2"), + ("2454495034", "-2454495034"), + (".1", "-0.1"), + ("11.815126050420168067226890757", "-11.815126050420168067226890757"), + ]; + + for &(a, b) in tests { + neg(a, b); + } +} + +// Addition + +#[test] +fn it_adds_decimals() { + fn add(a: &str, b: &str, c: &str) { + let a = Decimal::from_str(a).unwrap(); + let b = Decimal::from_str(b).unwrap(); + let result = a + b; + assert_eq!(c, result.to_string(), "{} + {}", a.to_string(), b.to_string()); + let result = b + a; + assert_eq!(c, result.to_string(), "{} + {}", b.to_string(), a.to_string()); + } + + let tests = &[ + ("2", "3", "5"), + ("2454495034", "3451204593", "5905699627"), + ("24544.95034", ".3451204593", "24545.2954604593"), + (".1", ".1", "0.2"), + (".10", ".1", "0.20"), + (".1", "-.1", "0.0"), + ("0", "1.001", "1.001"), + ("2", "-3", "-1"), + ("-2", "3", "1"), + ("-2", "-3", "-5"), + ("3", "-2", "1"), + ("-3", "2", "-1"), + ("1.234", "2.4567", "3.6907"), + ( + "11.815126050420168067226890757", + "0.6386554621848739495798319328", + "12.453781512605042016806722690", + ), + ( + "-11.815126050420168067226890757", + "0.6386554621848739495798319328", + "-11.176470588235294117647058824", + ), + ( + "11.815126050420168067226890757", + "-0.6386554621848739495798319328", + "11.176470588235294117647058824", + ), + ( + "-11.815126050420168067226890757", + "-0.6386554621848739495798319328", + "-12.453781512605042016806722690", + ), + ( + "11815126050420168067226890757", + "0.4386554621848739495798319328", + "11815126050420168067226890757", + ), + ( + "-11815126050420168067226890757", + "0.4386554621848739495798319328", + "-11815126050420168067226890757", + ), + ( + "11815126050420168067226890757", + "-0.4386554621848739495798319328", + "11815126050420168067226890757", + ), + ( + "-11815126050420168067226890757", + "-0.4386554621848739495798319328", + "-11815126050420168067226890757", + ), + ( + "0.0872727272727272727272727272", + "843.65000000", + "843.7372727272727272727272727", + ), + ( + "7314.6229858868828353570724702", + "1000", + // Overflow causes this to round + "8314.622985886882835357072470", + ), + ( + "108053.27500000000000000000000", + "0.00000000000000000000000", + "108053.27500000000000000000000", + ), + ( + "108053.27500000000000000000000", + // This zero value has too high precision and will be trimmed + "0.000000000000000000000000", + "108053.27500000000000000000000", + ), + ( + "108053.27500000000000000000000", + // This value has too high precision and will be rounded + "0.000000000000000000000001", + "108053.27500000000000000000000", + ), + ( + "108053.27500000000000000000000", + // This value has too high precision and will be rounded + "0.000000000000000000000005", + "108053.27500000000000000000001", + ), + ( + "8097370036018690744.2590371109596744091", + "3807285637671831400.15346897797550749555", + "11904655673690522144.412506089", + ), + ]; + for &(a, b, c) in tests { + add(a, b, c); + } +} + +#[test] +fn it_can_addassign() { + let mut a = Decimal::from_str("1.01").unwrap(); + let b = Decimal::from_str("0.99").unwrap(); + a += b; + assert_eq!("2.00", a.to_string()); + + a += &b; + assert_eq!("2.99", a.to_string()); + + let mut c = &mut a; + c += b; + assert_eq!("3.98", a.to_string()); + + let mut c = &mut a; + c += &b; + assert_eq!("4.97", a.to_string()); +} + +// Subtraction + +#[test] +fn it_subtracts_decimals() { + fn sub(a: &str, b: &str, c: &str) { + let a = Decimal::from_str(a).unwrap(); + let b = Decimal::from_str(b).unwrap(); + let result = a - b; + assert_eq!(c, result.to_string(), "{} - {}", a.to_string(), b.to_string()); + } + + let tests = &[ + ("2", "3", "-1"), + ("3451204593", "2323322332", "1127882261"), + ("24544.95034", ".3451204593", "24544.6052195407"), + (".1", ".1", "0.0"), + (".1", "-.1", "0.2"), + ("1.001", "0", "1.001"), + ("2", "-3", "5"), + ("-2", "3", "-5"), + ("-2", "-3", "1"), + ("3", "-2", "5"), + ("-3", "2", "-5"), + ("1.234", "2.4567", "-1.2227"), + ("844.13000000", "843.65000000", "0.48000000"), + ("79228162514264337593543950335", "79228162514264337593543950335", "0"), // 0xFFFF_FFFF_FFFF_FFFF_FFF_FFFF - 0xFFFF_FFFF_FFFF_FFFF_FFF_FFFF + ("79228162514264337593543950335", "0", "79228162514264337593543950335"), + ("79228162514264337593543950335", "79228162514264337593543950333", "2"), + ("4951760157141521099596496896", "1", "4951760157141521099596496895"), // 0x1000_0000_0000_0000_0000_0000 - 1 = 0x0FFF_FFFF_FFFF_FFFF_FFF_FFFF + ("79228162514264337593543950334", "79228162514264337593543950335", "-1"), + ("1", "4951760157141521099596496895", "-4951760157141521099596496894"), + ("18446744073709551615", "-18446744073709551615", "36893488147419103230"), // 0xFFFF_FFFF_FFFF_FFFF - -0xFFFF_FFFF_FFFF_FFFF + ]; + for &(a, b, c) in tests { + sub(a, b, c); + } +} + +#[test] +fn it_can_subassign() { + let mut a = Decimal::from_str("1.01").unwrap(); + let b = Decimal::from_str("0.51").unwrap(); + a -= b; + assert_eq!("0.50", a.to_string()); + + a -= &b; + assert_eq!("-0.01", a.to_string()); + + let mut c = &mut a; + c -= b; + assert_eq!("-0.52", a.to_string()); + + let mut c = &mut a; + c -= &b; + assert_eq!("-1.03", a.to_string()); +} + +// Multiplication + +#[test] +fn it_multiplies_decimals() { + fn mul(a: &str, b: &str, c: &str) { + let a = Decimal::from_str(a).unwrap(); + let b = Decimal::from_str(b).unwrap(); + let result = a * b; + assert_eq!(c, result.to_string(), "{} * {}", a.to_string(), b.to_string()); + let result = b * a; + assert_eq!(c, result.to_string(), "{} * {}", b.to_string(), a.to_string()); + } + + let tests = &[ + ("2", "3", "6"), + ("2454495034", "3451204593", "8470964534836491162"), + ("24544.95034", ".3451204593", "8470.964534836491162"), + (".1", ".1", "0.01"), + ("0", "1.001", "0"), + ("2", "-3", "-6"), + ("-2", "3", "-6"), + ("-2", "-3", "6"), + ("1", "2.01", "2.01"), + ("1.0", "2.01", "2.010"), // Scale is always additive + ( + "0.00000000000000001", + "0.00000000000000001", + "0.0000000000000000000000000000", + ), + ("0.0000000000000000000000000001", "0.0000000000000000000000000001", "0"), + ( + "0.6386554621848739495798319328", + "11.815126050420168067226890757", + "7.5457947885036367488171739292", + ), + ( + "2123456789012345678901234567.8", + "11.815126050420168067226890757", + "25088909624801327937270048761", + ), + ( + "2123456789012345678901234567.8", + "-11.815126050420168067226890757", + "-25088909624801327937270048761", + ), + ( + "2.1234567890123456789012345678", + "2.1234567890123456789012345678", + "4.5090687348026215523554336227", + ), + ( + "0.48000000", + "0.1818181818181818181818181818", + "0.0872727272727272727272727272", + ), + ]; + for &(a, b, c) in tests { + mul(a, b, c); + } +} + +#[test] +#[should_panic] +fn it_panics_when_multiply_with_overflow() { + let a = Decimal::from_str("2000000000000000000001").unwrap(); + let b = Decimal::from_str("3000000000000000000001").unwrap(); + let _ = a * b; +} + +#[test] +fn it_can_mulassign() { + let mut a = Decimal::from_str("1.25").unwrap(); + let b = Decimal::from_str("0.01").unwrap(); + + a *= b; + assert_eq!("0.0125", a.to_string()); + + a *= &b; + assert_eq!("0.000125", a.to_string()); + + let mut c = &mut a; + c *= b; + assert_eq!("0.00000125", a.to_string()); + + let mut c = &mut a; + c *= &b; + assert_eq!("0.0000000125", a.to_string()); +} + +// Division + +#[test] +fn it_divides_decimals() { + fn div(a: &str, b: &str, c: &str) { + let a = Decimal::from_str(a).unwrap(); + let b = Decimal::from_str(b).unwrap(); + let result = a / b; + assert_eq!(c, result.to_string(), "{} / {}", a.to_string(), b.to_string()); + } + + let tests = &[ + ("6", "3", "2"), + ("10", "2", "5"), + ("2.2", "1.1", "2"), + ("-2.2", "-1.1", "2"), + ("12.88", "5.6", "2.3"), + ("1023427554493", "43432632", "23563.562864276795382789603908"), + ("10000", "3", "3333.3333333333333333333333333"), + ("2", "3", "0.6666666666666666666666666667"), + ("1", "3", "0.3333333333333333333333333333"), + ("-2", "3", "-0.6666666666666666666666666667"), + ("2", "-3", "-0.6666666666666666666666666667"), + ("-2", "-3", "0.6666666666666666666666666667"), + ("1234.567890123456789012345678", "1.234567890123456789012345678", "1000"), + ]; + for &(a, b, c) in tests { + div(a, b, c); + } +} + +#[test] +#[should_panic] +fn it_can_divide_by_zero() { + let a = Decimal::from_str("2").unwrap(); + let _ = a / Decimal::zero(); +} + +#[test] +fn it_can_divassign() { + let mut a = Decimal::from_str("1.25").unwrap(); + let b = Decimal::from_str("0.01").unwrap(); + + a /= b; + assert_eq!("125", a.to_string()); + + a /= &b; + assert_eq!("12500", a.to_string()); + + let mut c = &mut a; + c /= b; + assert_eq!("1250000", a.to_string()); + + let mut c = &mut a; + c /= &b; + assert_eq!("125000000", a.to_string()); +} + +// Modulus and Remainder are not the same thing! +// https://math.stackexchange.com/q/801962/82277 + +#[test] +fn it_rems_decimals() { + fn rem(a: &str, b: &str, c: &str) { + let a = Decimal::from_str(a).unwrap(); + let b = Decimal::from_str(b).unwrap(); + // a = qb + r + let result = a % b; + assert_eq!(c, result.to_string(), "{} % {}", a.to_string(), b.to_string()); + } + + let tests = &[ + ("2", "3", "2"), + ("-2", "3", "-2"), + ("2", "-3", "2"), + ("-2", "-3", "-2"), + ("6", "3", "0"), + ("42.2", "11.9", "6.5"), + ("2.1", "3", "2.1"), + ("2", "3.1", "2"), + ("2.0", "3.1", "2.0"), + ("4", "3.1", "0.9"), + ]; + for &(a, b, c) in tests { + rem(a, b, c); + } +} + +#[test] +fn it_can_remassign() { + let mut a = Decimal::from_str("5").unwrap(); + let b = Decimal::from_str("2").unwrap(); + + a %= b; + assert_eq!("1", a.to_string()); + + a %= &b; + assert_eq!("1", a.to_string()); + + let mut c = &mut a; + c %= b; + assert_eq!("1", a.to_string()); + + let mut c = &mut a; + c %= &b; + assert_eq!("1", a.to_string()); +} + +#[test] +fn it_eqs_decimals() { + fn eq(a: &str, b: &str, c: bool) { + let a = Decimal::from_str(a).unwrap(); + let b = Decimal::from_str(b).unwrap(); + assert_eq!(c, a.eq(&b), "{} == {}", a.to_string(), b.to_string()); + assert_eq!(c, b.eq(&a), "{} == {}", b.to_string(), a.to_string()); + } + + let tests = &[ + ("1", "1", true), + ("1", "-1", false), + ("1", "1.00", true), + ("1.2345000000000", "1.2345", true), + ("1.0000000000000000000000000000", "1.0000000000000000000000000000", true), + ( + "1.0000000000000000000000000001", + "1.0000000000000000000000000000", + false, + ), + ]; + for &(a, b, c) in tests { + eq(a, b, c); + } +} + +#[test] +fn it_cmps_decimals() { + fn cmp(a: &str, b: &str, c: Ordering) { + let a = Decimal::from_str(a).unwrap(); + let b = Decimal::from_str(b).unwrap(); + assert_eq!(c, a.cmp(&b), "{} {:?} {}", a.to_string(), c, b.to_string()); + } + + let tests = &[ + ("1", "1", Equal), + ("1", "-1", Greater), + ("1", "1.00", Equal), + ("1.2345000000000", "1.2345", Equal), + ( + "1.0000000000000000000000000001", + "1.0000000000000000000000000000", + Greater, + ), + ("1.0000000000000000000000000000", "1.0000000000000000000000000001", Less), + ("-1", "100", Less), + ("-100", "1", Less), + ("0", "0.5", Less), + ("0.5", "0", Greater), + ("100", "0.0098", Greater), + ("1000000000000000", "999000000000000.0001", Greater), + ("2.0001", "2.0001", Equal), + ( + "11.815126050420168067226890757", + "0.6386554621848739495798319328", + Greater, + ), + ("0.6386554621848739495798319328", "11.815126050420168067226890757", Less), + ("-0.5", "-0.01", Less), + ("-0.5", "-0.1", Less), + ("-0.01", "-0.5", Greater), + ("-0.1", "-0.5", Greater), + ]; + for &(a, b, c) in tests { + cmp(a, b, c); + } +} + +#[test] +fn it_floors_decimals() { + let tests = &[ + ("1", "1"), + ("1.00", "1"), + ("1.2345", "1"), + ("-1", "-1"), + ("-1.00", "-1"), + ("-1.2345", "-2"), + ]; + for &(a, expected) in tests { + let a = Decimal::from_str(a).unwrap(); + assert_eq!(expected, a.floor().to_string(), "Failed flooring {}", a); + } +} + +#[test] +fn it_ceils_decimals() { + let tests = &[ + ("1", "1"), + ("1.00", "1"), + ("1.2345", "2"), + ("-1", "-1"), + ("-1.00", "-1"), + ("-1.2345", "-1"), + ]; + for &(a, expected) in tests { + let a = Decimal::from_str(a).unwrap(); + assert_eq!(expected, a.ceil().to_string(), "Failed ceiling {}", a); + } +} + +#[test] +fn it_finds_max_of_two() { + let tests = &[("1", "1", "1"), ("2", "1", "2"), ("1", "2", "2")]; + for &(a, b, expected) in tests { + let a = Decimal::from_str(a).unwrap(); + let b = Decimal::from_str(b).unwrap(); + assert_eq!(expected, a.max(b).to_string()); + } +} + +#[test] +fn it_finds_min_of_two() { + let tests = &[("1", "1", "1"), ("2", "1", "1"), ("1", "2", "1")]; + for &(a, b, expected) in tests { + let a = Decimal::from_str(a).unwrap(); + let b = Decimal::from_str(b).unwrap(); + assert_eq!(expected, a.min(b).to_string()); + } +} + +#[test] +fn test_max_compares() { + let x = "225.33543601344182".parse::().unwrap(); + let y = Decimal::max_value(); + assert!(x < y); + assert!(y > x); + assert_ne!(y, x); +} + +#[test] +fn test_min_compares() { + let x = "225.33543601344182".parse::().unwrap(); + let y = Decimal::min_value(); + assert!(x > y); + assert!(y < x); + assert_ne!(y, x); +} + +#[test] +fn it_can_parse_from_i32() { + use num_traits::FromPrimitive; + + let tests = &[ + (0i32, "0"), + (1i32, "1"), + (-1i32, "-1"), + (i32::max_value(), "2147483647"), + (i32::min_value(), "-2147483648"), + ]; + for &(input, expected) in tests { + let parsed = Decimal::from_i32(input).unwrap(); + assert_eq!( + expected, + parsed.to_string(), + "expected {} does not match parsed {}", + expected, + parsed + ); + assert_eq!( + input.to_string(), + parsed.to_string(), + "i32 to_string {} does not match parsed {}", + input, + parsed + ); + } +} + +#[test] +fn it_can_parse_from_i64() { + use num_traits::FromPrimitive; + + let tests = &[ + (0i64, "0"), + (1i64, "1"), + (-1i64, "-1"), + (i64::max_value(), "9223372036854775807"), + (i64::min_value(), "-9223372036854775808"), + ]; + for &(input, expected) in tests { + let parsed = Decimal::from_i64(input).unwrap(); + assert_eq!( + expected, + parsed.to_string(), + "expected {} does not match parsed {}", + expected, + parsed + ); + assert_eq!( + input.to_string(), + parsed.to_string(), + "i64 to_string {} does not match parsed {}", + input, + parsed + ); + } +} + +#[test] +fn it_can_round_to_2dp() { + let a = Decimal::from_str("6.12345").unwrap(); + let b = (Decimal::from_str("100").unwrap() * a).round() / Decimal::from_str("100").unwrap(); + assert_eq!("6.12", b.to_string()); +} + +#[test] +fn it_can_round_using_bankers_rounding() { + let tests = &[ + ("6.12345", 2, "6.12"), + ("6.126", 2, "6.13"), + ("-6.126", 2, "-6.13"), + ("6.5", 0, "6"), + ("7.5", 0, "8"), + ("1.2250", 2, "1.22"), + ("1.2252", 2, "1.23"), + ("1.2249", 2, "1.22"), + ("6.1", 2, "6.1"), + ("0.0000", 2, "0.00"), + ("0.6666666666666666666666666666", 2, "0.67"), + ("1.40", 0, "1"), + ("2.60", 0, "3"), + ("2.1234567890123456789012345678", 27, "2.123456789012345678901234568"), + ]; + for &(input, dp, expected) in tests { + let a = Decimal::from_str(input).unwrap(); + let b = a.round_dp_with_strategy(dp, RoundingStrategy::BankersRounding); + assert_eq!(expected, b.to_string()); + } +} + +#[test] +fn it_can_round_complex_numbers_using_bankers_rounding() { + // Issue #71 + let rate = Decimal::new(19, 2); // 0.19 + let one = Decimal::new(1, 0); // 1 + let part = rate / (rate + one); // 0.19 / (0.19 + 1) = 0.1596638655462184873949579832 + let part = part.round_dp_with_strategy(2, RoundingStrategy::BankersRounding); // 0.16 + assert_eq!("0.16", part.to_string()); +} + +#[test] +fn it_can_round_using_round_half_up() { + let tests = &[ + ("0", 0, "0"), + ("1.234", 3, "1.234"), + ("1.12", 5, "1.12"), + ("6.34567", 2, "6.35"), + ("6.5", 0, "7"), + ("12.49", 0, "12"), + ("0.6666666666666666666666666666", 2, "0.67"), + ("1.40", 0, "1"), + ("2.60", 0, "3"), + ("2.1234567890123456789012345678", 27, "2.123456789012345678901234568"), + ]; + for &(input, dp, expected) in tests { + let a = Decimal::from_str(input).unwrap(); + let b = a.round_dp_with_strategy(dp, RoundingStrategy::RoundHalfUp); + assert_eq!(expected, b.to_string()); + } +} + +#[test] +fn it_can_round_complex_numbers_using_round_half_up() { + // Issue #71 + let rate = Decimal::new(19, 2); // 0.19 + let one = Decimal::new(1, 0); // 1 + let part = rate / (rate + one); // 0.19 / (0.19 + 1) = 0.1596638655462184873949579832 + let part = part.round_dp_with_strategy(2, RoundingStrategy::RoundHalfUp); // 0.16 + assert_eq!("0.16", part.to_string()); +} + +#[test] +fn it_can_round_using_round_half_down() { + let tests = &[ + ("0", 0, "0"), + ("1.234", 3, "1.234"), + ("1.12", 5, "1.12"), + ("6.34567", 2, "6.35"), + ("6.51", 0, "7"), + ("12.5", 0, "12"), + ("0.6666666666666666666666666666", 2, "0.67"), + ("1.40", 0, "1"), + ("2.60", 0, "3"), + ("2.1234567890123456789012345678", 27, "2.123456789012345678901234568"), + ]; + for &(input, dp, expected) in tests { + let a = Decimal::from_str(input).unwrap(); + let b = a.round_dp_with_strategy(dp, RoundingStrategy::RoundHalfDown); + assert_eq!(expected, b.to_string()); + } +} + +#[test] +fn it_can_round_complex_numbers_using_round_half_down() { + // Issue #71 + let rate = Decimal::new(19, 2); // 0.19 + let one = Decimal::new(1, 0); // 1 + let part = rate / (rate + one); // 0.19 / (0.19 + 1) = 0.1596638655462184873949579832 + let part = part.round_dp_with_strategy(2, RoundingStrategy::RoundHalfDown); // 0.16 + assert_eq!("0.16", part.to_string()); +} + +#[test] +fn it_can_round_to_2dp_using_explicit_function() { + let a = Decimal::from_str("6.12345").unwrap(); + let b = a.round_dp(2u32); + assert_eq!("6.12", b.to_string()); +} + +#[test] +fn it_can_round_up_to_2dp_using_explicit_function() { + let a = Decimal::from_str("6.126").unwrap(); + let b = a.round_dp(2u32); + assert_eq!("6.13", b.to_string()); +} + +#[test] +fn it_can_round_down_to_2dp_using_explicit_function() { + let a = Decimal::from_str("-6.126").unwrap(); + let b = a.round_dp(2u32); + assert_eq!("-6.13", b.to_string()); +} + +#[test] +fn it_can_round_down_using_bankers_rounding() { + let a = Decimal::from_str("6.5").unwrap(); + let b = a.round_dp(0u32); + assert_eq!("6", b.to_string()); +} + +#[test] +fn it_can_round_up_using_bankers_rounding() { + let a = Decimal::from_str("7.5").unwrap(); + let b = a.round_dp(0u32); + assert_eq!("8", b.to_string()); +} + +#[test] +fn it_can_round_correctly_using_bankers_rounding_1() { + let a = Decimal::from_str("1.2250").unwrap(); + let b = a.round_dp(2u32); + assert_eq!("1.22", b.to_string()); +} + +#[test] +fn it_can_round_correctly_using_bankers_rounding_2() { + let a = Decimal::from_str("1.2251").unwrap(); + let b = a.round_dp(2u32); + assert_eq!("1.23", b.to_string()); +} + +#[test] +fn it_can_round_down_when_required() { + let a = Decimal::from_str("1.2249").unwrap(); + let b = a.round_dp(2u32); + assert_eq!("1.22", b.to_string()); +} + +#[test] +fn it_can_round_to_2dp_using_explicit_function_without_changing_value() { + let a = Decimal::from_str("6.1").unwrap(); + let b = a.round_dp(2u32); + assert_eq!("6.1", b.to_string()); +} + +#[test] +fn it_can_round_zero() { + let a = Decimal::from_str("0.0000").unwrap(); + let b = a.round_dp(2u32); + assert_eq!("0.00", b.to_string()); +} + +#[test] +fn it_can_round_large_decimals() { + let a = Decimal::from_str("0.6666666666666666666666666666").unwrap(); + let b = a.round_dp(2u32); + assert_eq!("0.67", b.to_string()); +} + +#[test] +fn it_can_round_simple_numbers_down() { + let a = Decimal::from_str("1.40").unwrap(); + let b = a.round_dp(0u32); + assert_eq!("1", b.to_string()); +} + +#[test] +fn it_can_round_simple_numbers_up() { + let a = Decimal::from_str("2.60").unwrap(); + let b = a.round_dp(0u32); + assert_eq!("3", b.to_string()); +} + +#[test] +fn it_can_round_simple_numbers_with_high_precision() { + let a = Decimal::from_str("2.1234567890123456789012345678").unwrap(); + let b = a.round_dp(27u32); + assert_eq!("2.123456789012345678901234568", b.to_string()); +} + +#[test] +fn it_can_round_complex_numbers() { + // Issue #71 + let rate = Decimal::new(19, 2); // 0.19 + let one = Decimal::new(1, 0); // 1 + let part = rate / (rate + one); // 0.19 / (0.19 + 1) = 0.1596638655462184873949579832 + let part = part.round_dp(2); // 0.16 + assert_eq!("0.16", part.to_string()); +} + +#[test] +fn it_can_round_down() { + let a = Decimal::new(470, 3).round_dp_with_strategy(1, RoundingStrategy::RoundDown); + assert_eq!("0.4", a.to_string()); +} + +#[test] +fn it_only_rounds_down_when_needed() { + let a = Decimal::new(400, 3).round_dp_with_strategy(1, RoundingStrategy::RoundDown); + assert_eq!("0.4", a.to_string()); +} + +#[test] +fn it_can_round_up() { + let a = Decimal::new(320, 3).round_dp_with_strategy(1, RoundingStrategy::RoundUp); + assert_eq!("0.4", a.to_string()); +} + +#[test] +fn it_only_rounds_up_when_needed() { + let a = Decimal::new(300, 3).round_dp_with_strategy(1, RoundingStrategy::RoundUp); + assert_eq!("0.3", a.to_string()); +} + +#[test] +fn it_can_trunc() { + let tests = &[("1.00000000000000000000", "1"), ("1.000000000000000000000001", "1")]; + + for &(value, expected) in tests { + let value = Decimal::from_str(value).unwrap(); + let expected = Decimal::from_str(expected).unwrap(); + let trunc = value.trunc(); + assert_eq!(expected.to_string(), trunc.to_string()); + } +} + +#[test] +fn it_can_fract() { + let tests = &[ + ("1.00000000000000000000", "0.00000000000000000000"), + ("1.000000000000000000000001", "0.000000000000000000000001"), + ]; + + for &(value, expected) in tests { + let value = Decimal::from_str(value).unwrap(); + let expected = Decimal::from_str(expected).unwrap(); + let fract = value.fract(); + assert_eq!(expected.to_string(), fract.to_string()); + } +} + +#[test] +fn it_can_normalize() { + let tests = &[ + ("1.00000000000000000000", "1"), + ("1.10000000000000000000000", "1.1"), + ("1.00010000000000000000000", "1.0001"), + ("1", "1"), + ("1.1", "1.1"), + ("1.0001", "1.0001"), + ("-0", "0"), + ("-0.0", "0"), + ("-0.010", "-0.01"), + ("0.0", "0"), + ]; + + for &(value, expected) in tests { + let value = Decimal::from_str(value).unwrap(); + let expected = Decimal::from_str(expected).unwrap(); + let normalized = value.normalize(); + assert_eq!(expected.to_string(), normalized.to_string()); + } +} + +#[test] +fn it_can_return_the_max_value() { + assert_eq!("79228162514264337593543950335", Decimal::max_value().to_string()); +} + +#[test] +fn it_can_return_the_min_value() { + assert_eq!("-79228162514264337593543950335", Decimal::min_value().to_string()); +} + +#[test] +fn it_can_go_from_and_into() { + let d = Decimal::from_str("5").unwrap(); + let di8 = 5u8.into(); + let di32 = 5i32.into(); + let disize = 5isize.into(); + let di64 = 5i64.into(); + let du8 = 5u8.into(); + let du32 = 5u32.into(); + let dusize = 5usize.into(); + let du64 = 5u64.into(); + + assert_eq!(d, di8); + assert_eq!(di8, di32); + assert_eq!(di32, disize); + assert_eq!(disize, di64); + assert_eq!(di64, du8); + assert_eq!(du8, du32); + assert_eq!(du32, dusize); + assert_eq!(dusize, du64); +} + +#[test] +fn it_converts_to_f64() { + assert_eq!(5f64, Decimal::from_str("5").unwrap().to_f64().unwrap()); + assert_eq!(-5f64, Decimal::from_str("-5").unwrap().to_f64().unwrap()); + assert_eq!(0.1f64, Decimal::from_str("0.1").unwrap().to_f64().unwrap()); + assert_eq!(0f64, Decimal::from_str("0.0").unwrap().to_f64().unwrap()); + assert_eq!(0f64, Decimal::from_str("-0.0").unwrap().to_f64().unwrap()); + assert_eq!( + 0.25e-11f64, + Decimal::from_str("0.0000000000025").unwrap().to_f64().unwrap(), + ); + assert_eq!( + 1e6f64, + Decimal::from_str("1000000.0000000000025").unwrap().to_f64().unwrap() + ); + assert_eq!( + 0.25e-25_f64, + Decimal::from_str("0.000000000000000000000000025") + .unwrap() + .to_f64() + .unwrap(), + ); + assert_eq!( + 2.1234567890123456789012345678_f64, + Decimal::from_str("2.1234567890123456789012345678") + .unwrap() + .to_f64() + .unwrap(), + ); + + assert_eq!( + None, + // Cannot be represented in an f64 + Decimal::from_str("21234567890123456789012345678").unwrap().to_f64(), + ); +} + +#[test] +fn it_converts_to_i64() { + assert_eq!(5i64, Decimal::from_str("5").unwrap().to_i64().unwrap()); + assert_eq!(-5i64, Decimal::from_str("-5").unwrap().to_i64().unwrap()); + assert_eq!(5i64, Decimal::from_str("5.12345").unwrap().to_i64().unwrap()); + assert_eq!(-5i64, Decimal::from_str("-5.12345").unwrap().to_i64().unwrap()); + assert_eq!( + 0x7FFF_FFFF_FFFF_FFFF, + Decimal::from_str("9223372036854775807").unwrap().to_i64().unwrap() + ); + assert_eq!(None, Decimal::from_str("92233720368547758089").unwrap().to_i64()); +} + +#[test] +fn it_converts_to_u64() { + assert_eq!(5u64, Decimal::from_str("5").unwrap().to_u64().unwrap()); + assert_eq!(None, Decimal::from_str("-5").unwrap().to_u64()); + assert_eq!(5u64, Decimal::from_str("5.12345").unwrap().to_u64().unwrap()); + assert_eq!( + 0xFFFF_FFFF_FFFF_FFFF, + Decimal::from_str("18446744073709551615").unwrap().to_u64().unwrap() + ); + assert_eq!(None, Decimal::from_str("18446744073709551616").unwrap().to_u64()); +} + +#[test] +fn it_converts_from_f32() { + fn from_f32(f: f32) -> Option { + num_traits::FromPrimitive::from_f32(f) + } + + assert_eq!("1", from_f32(1f32).unwrap().to_string()); + assert_eq!("0", from_f32(0f32).unwrap().to_string()); + assert_eq!("0.12345", from_f32(0.12345f32).unwrap().to_string()); + assert_eq!( + "0.12345678", + from_f32(0.1234567800123456789012345678f32).unwrap().to_string() + ); + assert_eq!( + "0.12345679", + from_f32(0.12345678901234567890123456789f32).unwrap().to_string() + ); + assert_eq!("0", from_f32(0.00000000000000000000000000001f32).unwrap().to_string()); + + assert!(from_f32(std::f32::NAN).is_none()); + assert!(from_f32(std::f32::INFINITY).is_none()); + + // These both overflow + assert!(from_f32(std::f32::MAX).is_none()); + assert!(from_f32(std::f32::MIN).is_none()); +} + +#[test] +fn it_converts_from_f64() { + fn from_f64(f: f64) -> Option { + num_traits::FromPrimitive::from_f64(f) + } + + assert_eq!("1", from_f64(1f64).unwrap().to_string()); + assert_eq!("0", from_f64(0f64).unwrap().to_string()); + assert_eq!("0.12345", from_f64(0.12345f64).unwrap().to_string()); + assert_eq!( + "0.1234567890123456", + from_f64(0.1234567890123456089012345678f64).unwrap().to_string() + ); + assert_eq!( + "0.1234567890123457", + from_f64(0.12345678901234567890123456789f64).unwrap().to_string() + ); + assert_eq!("0", from_f64(0.00000000000000000000000000001f64).unwrap().to_string()); + assert_eq!("0.6927", from_f64(0.6927f64).unwrap().to_string()); + assert_eq!("0.00006927", from_f64(0.00006927f64).unwrap().to_string()); + assert_eq!("0.000000006927", from_f64(0.000000006927f64).unwrap().to_string()); + + assert!(from_f64(std::f64::NAN).is_none()); + assert!(from_f64(std::f64::INFINITY).is_none()); + + // These both overflow + assert!(from_f64(std::f64::MAX).is_none()); + assert!(from_f64(std::f64::MIN).is_none()); +} + +#[test] +fn it_handles_simple_underflow() { + // Issue #71 + let rate = Decimal::new(19, 2); // 0.19 + let one = Decimal::new(1, 0); // 1 + let part = rate / (rate + one); // 0.19 / (0.19 + 1) = 0.1596638655462184873949579832 + let result = one * part; + assert_eq!("0.1596638655462184873949579832", result.to_string()); + + // 169 * 0.1596638655462184873949579832 = 26.983193277310924 + let result = part * Decimal::new(169, 0); + assert_eq!("26.983193277310924369747899161", result.to_string()); + let result = Decimal::new(169, 0) * part; + assert_eq!("26.983193277310924369747899161", result.to_string()); +} + +#[test] +fn it_can_parse_highly_significant_numbers() { + let tests = &[ + ("11.111111111111111111111111111", "11.111111111111111111111111111"), + ("11.11111111111111111111111111111", "11.111111111111111111111111111"), + ("11.1111111111111111111111111115", "11.111111111111111111111111112"), + ("115.111111111111111111111111111", "115.11111111111111111111111111"), + ("1115.11111111111111111111111111", "1115.1111111111111111111111111"), + ("11.1111111111111111111111111195", "11.111111111111111111111111120"), + ("99.9999999999999999999999999995", "100.00000000000000000000000000"), + ("-11.1111111111111111111111111195", "-11.111111111111111111111111120"), + ("-99.9999999999999999999999999995", "-100.00000000000000000000000000"), + ("3.1415926535897932384626433832", "3.1415926535897932384626433832"), + ( + "8808257419827262908.5944405087133154018", + "8808257419827262908.594440509", + ), + ( + "8097370036018690744.2590371109596744091", + "8097370036018690744.259037111", + ), + ( + "8097370036018690744.2590371149596744091", + "8097370036018690744.259037115", + ), + ( + "8097370036018690744.2590371159596744091", + "8097370036018690744.259037116", + ), + ]; + for &(value, expected) in tests { + assert_eq!(expected, Decimal::from_str(value).unwrap().to_string()); + } +} + +#[test] +fn it_can_parse_alternative_formats() { + let tests = &[ + ("1_000", "1000"), + ("1_000_000", "1000000"), + ("10_000_000", "10000000"), + ("100_000", "100000"), + // At the moment, we'll accept this + ("1_____________0", "10"), + ]; + for &(value, expected) in tests { + assert_eq!(expected, Decimal::from_str(value).unwrap().to_string()); + } +} + +#[test] +fn it_can_parse_fractional_numbers_with_underscore_separators() { + let a = Decimal::from_str("0.1_23_456").unwrap(); + assert_eq!(a.is_sign_negative(), false); + assert_eq!(a.scale(), 6); + assert_eq!("0.123456", a.to_string()); +} + +#[test] +fn it_can_parse_numbers_with_underscore_separators_before_decimal_point() { + let a = Decimal::from_str("1_234.56").unwrap(); + assert_eq!(a.is_sign_negative(), false); + assert_eq!(a.scale(), 2); + assert_eq!("1234.56", a.to_string()); +} + +#[test] +fn it_can_parse_numbers_and_round_correctly_with_underscore_separators_before_decimal_point() { + let tests = &[ + ( + "8_097_370_036_018_690_744.2590371159596744091", + "8097370036018690744.259037116", + ), + ( + "8097370036018690744.259_037_115_959_674_409_1", + "8097370036018690744.259037116", + ), + ( + "8_097_370_036_018_690_744.259_037_115_959_674_409_1", + "8097370036018690744.259037116", + ), + ]; + for &(value, expected) in tests { + assert_eq!(expected, Decimal::from_str(value).unwrap().to_string()); + } +} + +#[test] +fn it_can_reject_invalid_formats() { + let tests = &["_1", "1.0.0", "10_00.0_00.0"]; + for &value in tests { + assert!( + Decimal::from_str(value).is_err(), + "This succeeded unexpectedly: {}", + value + ); + } +} + +#[test] +fn it_can_reject_large_numbers_with_panic() { + let tests = &[ + // The maximum number supported is 79,228,162,514,264,337,593,543,950,335 + "79228162514264337593543950336", + "79228162514264337593543950337", + "79228162514264337593543950338", + "79228162514264337593543950339", + "79228162514264337593543950340", + ]; + for &value in tests { + assert!( + Decimal::from_str(value).is_err(), + "This succeeded unexpectedly: {}", + value + ); + } +} + +#[test] +fn it_can_parse_individual_parts() { + let pi = Decimal::from_parts(1102470952, 185874565, 1703060790, false, 28); + assert_eq!(pi.to_string(), "3.1415926535897932384626433832"); +} + +#[test] +fn it_can_parse_scientific_notation() { + let tests = &[ + ("9.7e-7", "0.00000097"), + ("9e-7", "0.0000009"), + ("1.2e10", "12000000000"), + ("1.2e+10", "12000000000"), + ("12e10", "120000000000"), + ("9.7E-7", "0.00000097"), + ]; + + for &(value, expected) in tests { + assert_eq!(expected, Decimal::from_scientific(value).unwrap().to_string()); + } +} + +#[test] +fn it_can_parse_different_radix() { + use num_traits::Num; + + let tests = &[ + // Input, Radix, Success, to_string() + ("123", 10, true, "123"), + ("123", 8, true, "83"), + ("123", 16, true, "291"), + ("abc", 10, false, ""), + ("abc", 16, true, "2748"), + ("78", 10, true, "78"), + ("78", 8, false, ""), + ("101", 2, true, "5"), + // Parse base 2 + ("1111_1111_1111_1111_1111_1111_1111_1111", 2, true, "4294967295"), + // Max supported value + ( + "1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_\ + 1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111", + 2, + true, + &Decimal::max_value().to_string(), + ), + // We limit to 28 dp + ( + "843.6500000000000000000000000000", + 10, + true, + "843.6500000000000000000000000", + ), + ]; + + for &(input, radix, success, expected) in tests { + let result = Decimal::from_str_radix(input, radix); + assert_eq!( + success, + result.is_ok(), + "Failed to parse: {} radix {}: {:?}", + input, + radix, + result.err() + ); + if result.is_ok() { + assert_eq!( + expected, + result.unwrap().to_string(), + "Original input: {} radix {}", + input, + radix + ); + } + } +} + +#[test] +fn it_can_calculate_signum() { + let tests = &[("123", 1), ("-123", -1), ("0", 0)]; + + for &(input, expected) in tests { + let input = Decimal::from_str(input).unwrap(); + assert_eq!(expected, input.signum().to_i32().unwrap(), "Input: {}", input); + } +} + +#[test] +fn it_can_calculate_abs_sub() { + let tests = &[ + ("123", "124", 0), + ("123", "123", 0), + ("123", "122", 123), + ("-123", "-124", 123), + ("-123", "-123", 0), + ("-123", "-122", 0), + ]; + + for &(input1, input2, expected) in tests { + let input1 = Decimal::from_str(input1).unwrap(); + let input2 = Decimal::from_str(input2).unwrap(); + assert_eq!( + expected, + input1.abs_sub(&input2).to_i32().unwrap(), + "Input: {} {}", + input1, + input2 + ); + } +} + +#[test] +#[should_panic] +fn it_panics_when_scale_too_large() { + let _ = Decimal::new(1, 29); +} + +#[test] +fn test_zero_eq_negative_zero() { + let zero: Decimal = 0.into(); + + assert!(zero == zero); + assert!(-zero == zero); + assert!(zero == -zero); +} + +#[cfg(feature = "postgres")] +#[test] +fn to_from_sql() { + use bytes::BytesMut; + use postgres::types::{FromSql, Kind, ToSql, Type}; + + let tests = &[ + "3950.123456", + "3950", + "0.1", + "0.01", + "0.001", + "0.0001", + "0.00001", + "0.000001", + "1", + "-100", + "-123.456", + "119996.25", + "1000000", + "9999999.99999", + "12340.56789", + "79228162514264337593543950335", // 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF (96 bit) + "4951760157141521099596496895", // 0x0FFF_FFFF_FFFF_FFFF_FFFF_FFFF (95 bit) + "4951760157141521099596496896", // 0x1000_0000_0000_0000_0000_0000 + "18446744073709551615", + "-18446744073709551615", + ]; + + let t = Type::new("".into(), 0, Kind::Simple, "".into()); + + for test in tests { + let input = Decimal::from_str(test).unwrap(); + let mut bytes = BytesMut::new(); + input.to_sql(&t, &mut bytes).unwrap(); + let output = Decimal::from_sql(&t, &bytes).unwrap(); + + assert_eq!(input, output); + } +} + +fn hash_it(d: Decimal) -> u64 { + use std::collections::hash_map::DefaultHasher; + use std::hash::Hash; + use std::hash::Hasher; + + let mut h = DefaultHasher::new(); + d.hash(&mut h); + h.finish() +} + +#[test] +fn it_computes_equal_hashes_for_equal_values() { + // From the Rust Hash docs: + // + // "When implementing both Hash and Eq, it is important that the following property holds: + // + // k1 == k2 -> hash(k1) == hash(k2)" + + let k1 = Decimal::from_str("1").unwrap(); + let k2 = Decimal::from_str("1.0").unwrap(); + let k3 = Decimal::from_str("1.00").unwrap(); + let k4 = Decimal::from_str("1.01").unwrap(); + + assert_eq!(k1, k2); + assert_eq!(k1, k3); + assert_ne!(k1, k4); + + let h1 = hash_it(k1); + let h2 = hash_it(k2); + let h3 = hash_it(k3); + let h4 = hash_it(k4); + + assert_eq!(h1, h2); + assert_eq!(h1, h3); + assert_ne!(h1, h4); + + // Test the application of Hash calculation to a HashMap. + + use std::collections::HashMap; + + let mut map = HashMap::new(); + + map.insert(k1, k1.to_string()); + // map[k2] should overwrite map[k1] because k1 == k2. + map.insert(k2, k2.to_string()); + + assert_eq!("1.0", map.get(&k3).expect("could not get k3")); + assert_eq!(1, map.len()); + + // map[k3] should overwrite map[k2] because k3 == k2. + map.insert(k3, k3.to_string()); + // map[k4] should not overwrite map[k3] because k4 != k3. + map.insert(k4, k4.to_string()); + + assert_eq!(2, map.len()); + assert_eq!("1.00", map.get(&k1).expect("could not get k1")); +} + +#[test] +fn it_computes_equal_hashes_for_positive_and_negative_zero() { + // Verify 0 and -0 have the same hash + let k1 = Decimal::from_str("0").unwrap(); + let k2 = Decimal::from_str("-0").unwrap(); + assert_eq!("-0", k2.to_string()); + assert_eq!(k1, k2); + let h1 = hash_it(k1); + let h2 = hash_it(k2); + assert_eq!(h1, h2); + + // Verify 0 and -0.0 have the same hash + let k1 = Decimal::from_str("0").unwrap(); + let k2 = Decimal::from_str("-0.0").unwrap(); + assert_eq!("-0.0", k2.to_string()); + assert_eq!(k1, k2); + let h1 = hash_it(k1); + let h2 = hash_it(k2); + assert_eq!(h1, h2); +} + +#[test] +#[should_panic] +fn it_handles_i128_min() { + Decimal::from_i128_with_scale(std::i128::MIN, 0); +} + +#[test] +fn it_can_rescale() { + let tests = &[ + ("0", 6, "0.000000"), + ("0.000000", 2, "0.00"), + ("0.12345600000", 6, "0.123456"), + ("0.123456", 12, "0.123456000000"), + ("0.123456", 0, "0"), + ("0.000001", 4, "0.0000"), + ("1233456", 4, "1233456.0000"), + ("1.2", 30, "1.2000000000000000000000000000"), + ("79228162514264337593543950335", 0, "79228162514264337593543950335"), + ("4951760157141521099596496895", 1, "4951760157141521099596496895.0"), + ("4951760157141521099596496896", 1, "4951760157141521099596496896.0"), + ("18446744073709551615", 6, "18446744073709551615.000000"), + ("-18446744073709551615", 6, "-18446744073709551615.000000"), + ]; + + for &(value_raw, new_scale, expected_value) in tests { + let new_value = Decimal::from_str(expected_value).unwrap(); + let mut value = Decimal::from_str(value_raw).unwrap(); + value.rescale(new_scale); + assert_eq!(new_value.to_string(), value.to_string()); + } +} diff --git a/third_party/rust/sfv/.cargo-checksum.json b/third_party/rust/sfv/.cargo-checksum.json new file mode 100644 index 000000000000..6dbb39805409 --- /dev/null +++ b/third_party/rust/sfv/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"75a68f4ae953f65f25e970fed50f05c43f37fdbc15a1d02079c61095bb9d18d7","LICENSE":"1f256ecad192880510e84ad60474eab7589218784b9a50bc7ceee34c2b91f1d5","README.md":"a9b433dfdebdc258e280071d40bb0df840456b6d8d1464b8955706b885eace3c","benches/bench.rs":"bbc60db4b542abb3738eba80f5c7c54ac39301ed5e48e2ae2a94cecfdb42e33f","src/lib.rs":"db8bc1c9f61a424f8923a849d131c71937236b7846d40643d1c6aee952b62236","src/parser.rs":"4de9bc1e04b536357d4c635350ba0dc1fbafae4b5741f6cd47dffd904468c251","src/ref_serializer.rs":"8806ee50e2b2ae466a49788d7e972a47329c0e2c842d669673a152286e81c5d9","src/serializer.rs":"5d7a4d18a4508d433993b6a7ee405285ed2b33cbc6b84101cc4720c897f5586e","src/test_parser.rs":"7a2728e7cbdcb1f3bb42e009045ec0dcfca241316a2aee4905925d4b1ce0bb3a","src/test_serializer.rs":"2419279c9a9a4f48952836d63f3822281c18691d86c146749a573c52a41d6ff0","src/utils.rs":"94c8f79f4747973819b9da2c1a9f6246bf3b5ea7450b376a98eb055f6acf8e73"},"package":"13ed1dd5a626253083678d21b5c38dd94f8717b961d4b7469eb96b41173cc148"} \ No newline at end of file diff --git a/third_party/rust/sfv/Cargo.toml b/third_party/rust/sfv/Cargo.toml new file mode 100644 index 000000000000..4b989dd8bbe7 --- /dev/null +++ b/third_party/rust/sfv/Cargo.toml @@ -0,0 +1,46 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +edition = "2018" +name = "sfv" +version = "0.8.0" +authors = ["Tania Batieva "] +exclude = ["tests/**", ".github/*"] +description = "Structured HTTP field values parser.\nImplementation of IETF draft https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html" +documentation = "https://docs.rs/sfv" +readme = "README.md" +keywords = ["http-header", "structured-header"] +license = "MIT/Apache-2.0" +repository = "https://github.com/undef1nd/sfv" + +[[bench]] +name = "bench" +harness = false +[dependencies.data-encoding] +version = "2.2.1" + +[dependencies.indexmap] +version = "1.1.0" + +[dependencies.rust_decimal] +version = "1.6.0" +[dev-dependencies.criterion] +version = "0.3.3" + +[dev-dependencies.serde] +version = "1.0" +features = ["derive"] + +[dev-dependencies.serde_json] +version = "1.0" +features = ["preserve_order"] diff --git a/third_party/rust/sfv/LICENSE b/third_party/rust/sfv/LICENSE new file mode 100644 index 000000000000..a612ad9813b0 --- /dev/null +++ b/third_party/rust/sfv/LICENSE @@ -0,0 +1,373 @@ +Mozilla Public License Version 2.0 +================================== + +1. Definitions +-------------- + +1.1. "Contributor" + means each individual or legal entity that creates, contributes to + the creation of, or owns Covered Software. + +1.2. "Contributor Version" + means the combination of the Contributions of others (if any) used + by a Contributor and that particular Contributor's Contribution. + +1.3. "Contribution" + means Covered Software of a particular Contributor. + +1.4. "Covered Software" + means Source Code Form to which the initial Contributor has attached + the notice in Exhibit A, the Executable Form of such Source Code + Form, and Modifications of such Source Code Form, in each case + including portions thereof. + +1.5. "Incompatible With Secondary Licenses" + means + + (a) that the initial Contributor has attached the notice described + in Exhibit B to the Covered Software; or + + (b) that the Covered Software was made available under the terms of + version 1.1 or earlier of the License, but not also under the + terms of a Secondary License. + +1.6. "Executable Form" + means any form of the work other than Source Code Form. + +1.7. "Larger Work" + means a work that combines Covered Software with other material, in + a separate file or files, that is not Covered Software. + +1.8. "License" + means this document. + +1.9. "Licensable" + means having the right to grant, to the maximum extent possible, + whether at the time of the initial grant or subsequently, any and + all of the rights conveyed by this License. + +1.10. "Modifications" + means any of the following: + + (a) any file in Source Code Form that results from an addition to, + deletion from, or modification of the contents of Covered + Software; or + + (b) any new file in Source Code Form that contains any Covered + Software. + +1.11. "Patent Claims" of a Contributor + means any patent claim(s), including without limitation, method, + process, and apparatus claims, in any patent Licensable by such + Contributor that would be infringed, but for the grant of the + License, by the making, using, selling, offering for sale, having + made, import, or transfer of either its Contributions or its + Contributor Version. + +1.12. "Secondary License" + means either the GNU General Public License, Version 2.0, the GNU + Lesser General Public License, Version 2.1, the GNU Affero General + Public License, Version 3.0, or any later versions of those + licenses. + +1.13. "Source Code Form" + means the form of the work preferred for making modifications. + +1.14. "You" (or "Your") + means an individual or a legal entity exercising rights under this + License. For legal entities, "You" includes any entity that + controls, is controlled by, or is under common control with You. For + purposes of this definition, "control" means (a) the power, direct + or indirect, to cause the direction or management of such entity, + whether by contract or otherwise, or (b) ownership of more than + fifty percent (50%) of the outstanding shares or beneficial + ownership of such entity. + +2. License Grants and Conditions +-------------------------------- + +2.1. Grants + +Each Contributor hereby grants You a world-wide, royalty-free, +non-exclusive license: + +(a) under intellectual property rights (other than patent or trademark) + Licensable by such Contributor to use, reproduce, make available, + modify, display, perform, distribute, and otherwise exploit its + Contributions, either on an unmodified basis, with Modifications, or + as part of a Larger Work; and + +(b) under Patent Claims of such Contributor to make, use, sell, offer + for sale, have made, import, and otherwise transfer either its + Contributions or its Contributor Version. + +2.2. Effective Date + +The licenses granted in Section 2.1 with respect to any Contribution +become effective for each Contribution on the date the Contributor first +distributes such Contribution. + +2.3. Limitations on Grant Scope + +The licenses granted in this Section 2 are the only rights granted under +this License. No additional rights or licenses will be implied from the +distribution or licensing of Covered Software under this License. +Notwithstanding Section 2.1(b) above, no patent license is granted by a +Contributor: + +(a) for any code that a Contributor has removed from Covered Software; + or + +(b) for infringements caused by: (i) Your and any other third party's + modifications of Covered Software, or (ii) the combination of its + Contributions with other software (except as part of its Contributor + Version); or + +(c) under Patent Claims infringed by Covered Software in the absence of + its Contributions. + +This License does not grant any rights in the trademarks, service marks, +or logos of any Contributor (except as may be necessary to comply with +the notice requirements in Section 3.4). + +2.4. Subsequent Licenses + +No Contributor makes additional grants as a result of Your choice to +distribute the Covered Software under a subsequent version of this +License (see Section 10.2) or under the terms of a Secondary License (if +permitted under the terms of Section 3.3). + +2.5. Representation + +Each Contributor represents that the Contributor believes its +Contributions are its original creation(s) or it has sufficient rights +to grant the rights to its Contributions conveyed by this License. + +2.6. Fair Use + +This License is not intended to limit any rights You have under +applicable copyright doctrines of fair use, fair dealing, or other +equivalents. + +2.7. Conditions + +Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted +in Section 2.1. + +3. Responsibilities +------------------- + +3.1. Distribution of Source Form + +All distribution of Covered Software in Source Code Form, including any +Modifications that You create or to which You contribute, must be under +the terms of this License. You must inform recipients that the Source +Code Form of the Covered Software is governed by the terms of this +License, and how they can obtain a copy of this License. You may not +attempt to alter or restrict the recipients' rights in the Source Code +Form. + +3.2. Distribution of Executable Form + +If You distribute Covered Software in Executable Form then: + +(a) such Covered Software must also be made available in Source Code + Form, as described in Section 3.1, and You must inform recipients of + the Executable Form how they can obtain a copy of such Source Code + Form by reasonable means in a timely manner, at a charge no more + than the cost of distribution to the recipient; and + +(b) You may distribute such Executable Form under the terms of this + License, or sublicense it under different terms, provided that the + license for the Executable Form does not attempt to limit or alter + the recipients' rights in the Source Code Form under this License. + +3.3. Distribution of a Larger Work + +You may create and distribute a Larger Work under terms of Your choice, +provided that You also comply with the requirements of this License for +the Covered Software. If the Larger Work is a combination of Covered +Software with a work governed by one or more Secondary Licenses, and the +Covered Software is not Incompatible With Secondary Licenses, this +License permits You to additionally distribute such Covered Software +under the terms of such Secondary License(s), so that the recipient of +the Larger Work may, at their option, further distribute the Covered +Software under the terms of either this License or such Secondary +License(s). + +3.4. Notices + +You may not remove or alter the substance of any license notices +(including copyright notices, patent notices, disclaimers of warranty, +or limitations of liability) contained within the Source Code Form of +the Covered Software, except that You may alter any license notices to +the extent required to remedy known factual inaccuracies. + +3.5. Application of Additional Terms + +You may choose to offer, and to charge a fee for, warranty, support, +indemnity or liability obligations to one or more recipients of Covered +Software. However, You may do so only on Your own behalf, and not on +behalf of any Contributor. You must make it absolutely clear that any +such warranty, support, indemnity, or liability obligation is offered by +You alone, and You hereby agree to indemnify every Contributor for any +liability incurred by such Contributor as a result of warranty, support, +indemnity or liability terms You offer. You may include additional +disclaimers of warranty and limitations of liability specific to any +jurisdiction. + +4. Inability to Comply Due to Statute or Regulation +--------------------------------------------------- + +If it is impossible for You to comply with any of the terms of this +License with respect to some or all of the Covered Software due to +statute, judicial order, or regulation then You must: (a) comply with +the terms of this License to the maximum extent possible; and (b) +describe the limitations and the code they affect. Such description must +be placed in a text file included with all distributions of the Covered +Software under this License. Except to the extent prohibited by statute +or regulation, such description must be sufficiently detailed for a +recipient of ordinary skill to be able to understand it. + +5. Termination +-------------- + +5.1. The rights granted under this License will terminate automatically +if You fail to comply with any of its terms. However, if You become +compliant, then the rights granted under this License from a particular +Contributor are reinstated (a) provisionally, unless and until such +Contributor explicitly and finally terminates Your grants, and (b) on an +ongoing basis, if such Contributor fails to notify You of the +non-compliance by some reasonable means prior to 60 days after You have +come back into compliance. Moreover, Your grants from a particular +Contributor are reinstated on an ongoing basis if such Contributor +notifies You of the non-compliance by some reasonable means, this is the +first time You have received notice of non-compliance with this License +from such Contributor, and You become compliant prior to 30 days after +Your receipt of the notice. + +5.2. If You initiate litigation against any entity by asserting a patent +infringement claim (excluding declaratory judgment actions, +counter-claims, and cross-claims) alleging that a Contributor Version +directly or indirectly infringes any patent, then the rights granted to +You by any and all Contributors for the Covered Software under Section +2.1 of this License shall terminate. + +5.3. In the event of termination under Sections 5.1 or 5.2 above, all +end user license agreements (excluding distributors and resellers) which +have been validly granted by You or Your distributors under this License +prior to termination shall survive termination. + +************************************************************************ +* * +* 6. Disclaimer of Warranty * +* ------------------------- * +* * +* Covered Software is provided under this License on an "as is" * +* basis, without warranty of any kind, either expressed, implied, or * +* statutory, including, without limitation, warranties that the * +* Covered Software is free of defects, merchantable, fit for a * +* particular purpose or non-infringing. The entire risk as to the * +* quality and performance of the Covered Software is with You. * +* Should any Covered Software prove defective in any respect, You * +* (not any Contributor) assume the cost of any necessary servicing, * +* repair, or correction. This disclaimer of warranty constitutes an * +* essential part of this License. No use of any Covered Software is * +* authorized under this License except under this disclaimer. * +* * +************************************************************************ + +************************************************************************ +* * +* 7. Limitation of Liability * +* -------------------------- * +* * +* Under no circumstances and under no legal theory, whether tort * +* (including negligence), contract, or otherwise, shall any * +* Contributor, or anyone who distributes Covered Software as * +* permitted above, be liable to You for any direct, indirect, * +* special, incidental, or consequential damages of any character * +* including, without limitation, damages for lost profits, loss of * +* goodwill, work stoppage, computer failure or malfunction, or any * +* and all other commercial damages or losses, even if such party * +* shall have been informed of the possibility of such damages. This * +* limitation of liability shall not apply to liability for death or * +* personal injury resulting from such party's negligence to the * +* extent applicable law prohibits such limitation. Some * +* jurisdictions do not allow the exclusion or limitation of * +* incidental or consequential damages, so this exclusion and * +* limitation may not apply to You. * +* * +************************************************************************ + +8. Litigation +------------- + +Any litigation relating to this License may be brought only in the +courts of a jurisdiction where the defendant maintains its principal +place of business and such litigation shall be governed by laws of that +jurisdiction, without reference to its conflict-of-law provisions. +Nothing in this Section shall prevent a party's ability to bring +cross-claims or counter-claims. + +9. Miscellaneous +---------------- + +This License represents the complete agreement concerning the subject +matter hereof. If any provision of this License is held to be +unenforceable, such provision shall be reformed only to the extent +necessary to make it enforceable. Any law or regulation which provides +that the language of a contract shall be construed against the drafter +shall not be used to construe this License against a Contributor. + +10. Versions of the License +--------------------------- + +10.1. New Versions + +Mozilla Foundation is the license steward. Except as provided in Section +10.3, no one other than the license steward has the right to modify or +publish new versions of this License. Each version will be given a +distinguishing version number. + +10.2. Effect of New Versions + +You may distribute the Covered Software under the terms of the version +of the License under which You originally received the Covered Software, +or under the terms of any subsequent version published by the license +steward. + +10.3. Modified Versions + +If you create software not governed by this License, and you want to +create a new license for such software, you may create and use a +modified version of this License if you rename the license and remove +any references to the name of the license steward (except to note that +such modified license differs from this License). + +10.4. Distributing Source Code Form that is Incompatible With Secondary +Licenses + +If You choose to distribute Source Code Form that is Incompatible With +Secondary Licenses under the terms of this version of the License, the +notice described in Exhibit B of this License must be attached. + +Exhibit A - Source Code Form License Notice +------------------------------------------- + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + +If it is not possible or desirable to put the notice in a particular +file, then You may include the notice in a location (such as a LICENSE +file in a relevant directory) where a recipient would be likely to look +for such a notice. + +You may add additional accurate notices of copyright ownership. + +Exhibit B - "Incompatible With Secondary Licenses" Notice +--------------------------------------------------------- + + This Source Code Form is "Incompatible With Secondary Licenses", as + defined by the Mozilla Public License, v. 2.0. diff --git a/third_party/rust/sfv/README.md b/third_party/rust/sfv/README.md new file mode 100644 index 000000000000..f1e431b3d4e1 --- /dev/null +++ b/third_party/rust/sfv/README.md @@ -0,0 +1,10 @@ +![Build Status](https://github.com/undef1nd/structured-headers/workflows/CI/badge.svg) +[![Version](https://img.shields.io/crates/v/sfv.svg)](https://crates.io/crates/sfv) + +# Structured Field Values for HTTP + +[Documentation](https://docs.rs/sfv/) + +`sfv` crate is an implementation of IETF draft [Structured Field Values for HTTP](https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html) for parsing and serializing HTTP field values (also known as "structured headers" or "structured trailers"). + +It also exposes a set of types that might be useful for defining new structured fields. diff --git a/third_party/rust/sfv/benches/bench.rs b/third_party/rust/sfv/benches/bench.rs new file mode 100644 index 000000000000..3e924c1f3218 --- /dev/null +++ b/third_party/rust/sfv/benches/bench.rs @@ -0,0 +1,171 @@ +#[macro_use] +extern crate criterion; + +use criterion::{BenchmarkId, Criterion}; +use rust_decimal::prelude::FromPrimitive; +use sfv::{BareItem, Decimal, Parser, SerializeValue}; +use sfv::{RefBareItem, RefDictSerializer, RefItemSerializer, RefListSerializer}; + +criterion_main!(parsing, serializing, ref_serializing); + +criterion_group!(parsing, parsing_item, parsing_list, parsing_dict); + +fn parsing_item(c: &mut Criterion) { + let fixture = + "c29tZXZlcnlsb25nc3RyaW5ndmFsdWVyZXByZXNlbnRlZGFzYnl0ZXNhbnNvbWVvdGhlcmxvbmdsaW5l"; + c.bench_with_input( + BenchmarkId::new("parsing_item", fixture), + &fixture, + move |bench, &input| { + bench.iter(|| Parser::parse_item(input.as_bytes()).unwrap()); + }, + ); +} + +fn parsing_list(c: &mut Criterion) { + let fixture = "a, abcdefghigklmnoprst, 123456785686457, 99999999999.999, (), (\"somelongstringvalue\" \"anotherlongstringvalue\";key=:c29tZXZlciBsb25nc3RyaW5ndmFsdWVyZXByZXNlbnRlZGFzYnl0ZXM: 145)"; + c.bench_with_input( + BenchmarkId::new("parsing_list", fixture), + &fixture, + move |bench, &input| { + bench.iter(|| Parser::parse_list(input.as_bytes()).unwrap()); + }, + ); +} + +fn parsing_dict(c: &mut Criterion) { + let fixture = "a, dict_key2=abcdefghigklmnoprst, dict_key3=123456785686457, dict_key4=(\"inner-list-member\" :aW5uZXItbGlzdC1tZW1iZXI=:);key=aW5uZXItbGlzdC1wYXJhbWV0ZXJz"; + c.bench_with_input( + BenchmarkId::new("parsing_dict", fixture), + &fixture, + move |bench, &input| { + bench.iter(|| Parser::parse_dictionary(input.as_bytes()).unwrap()); + }, + ); +} + +criterion_group!( + serializing, + serializing_item, + serializing_list, + serializing_dict +); + +fn serializing_item(c: &mut Criterion) { + let fixture = + "c29tZXZlcnlsb25nc3RyaW5ndmFsdWVyZXByZXNlbnRlZGFzYnl0ZXNhbnNvbWVvdGhlcmxvbmdsaW5l"; + c.bench_with_input( + BenchmarkId::new("serializing_item", fixture), + &fixture, + move |bench, &input| { + let parsed_item = Parser::parse_item(input.as_bytes()).unwrap(); + bench.iter(|| parsed_item.serialize_value().unwrap()); + }, + ); +} + +fn serializing_list(c: &mut Criterion) { + let fixture = "a, abcdefghigklmnoprst, 123456785686457, 99999999999.999, (), (\"somelongstringvalue\" \"anotherlongstringvalue\";key=:c29tZXZlciBsb25nc3RyaW5ndmFsdWVyZXByZXNlbnRlZGFzYnl0ZXM: 145)"; + c.bench_with_input( + BenchmarkId::new("serializing_list", fixture), + &fixture, + move |bench, &input| { + let parsed_list = Parser::parse_list(input.as_bytes()).unwrap(); + bench.iter(|| parsed_list.serialize_value().unwrap()); + }, + ); +} + +fn serializing_dict(c: &mut Criterion) { + let fixture = "a, dict_key2=abcdefghigklmnoprst, dict_key3=123456785686457, dict_key4=(\"inner-list-member\" :aW5uZXItbGlzdC1tZW1iZXI=:);key=aW5uZXItbGlzdC1wYXJhbWV0ZXJz"; + c.bench_with_input( + BenchmarkId::new("serializing_dict", fixture), + &fixture, + move |bench, &input| { + let parsed_dict = Parser::parse_dictionary(input.as_bytes()).unwrap(); + bench.iter(|| parsed_dict.serialize_value().unwrap()); + }, + ); +} + +criterion_group!( + ref_serializing, + serializing_ref_item, + serializing_ref_list, + serializing_ref_dict +); + +fn serializing_ref_item(c: &mut Criterion) { + let fixture = + "c29tZXZlcnlsb25nc3RyaW5ndmFsdWVyZXByZXNlbnRlZGFzYnl0ZXNhbnNvbWVvdGhlcmxvbmdsaW5l"; + c.bench_with_input( + BenchmarkId::new("serializing_ref_item", fixture), + &fixture, + move |bench, &input| { + bench.iter(|| { + let mut output = String::new(); + let ser = RefItemSerializer::new(&mut output); + ser.bare_item(&RefBareItem::ByteSeq(input.as_bytes())) + .unwrap(); + }); + }, + ); +} + +fn serializing_ref_list(c: &mut Criterion) { + c.bench_function("serializing_ref_list", move |bench| { + bench.iter(|| { + let mut output = String::new(); + let ser = RefListSerializer::new(&mut output); + ser.bare_item(&RefBareItem::Token("a")) + .unwrap() + .bare_item(&RefBareItem::Token("abcdefghigklmnoprst")) + .unwrap() + .bare_item(&RefBareItem::Integer(123456785686457)) + .unwrap() + .bare_item(&RefBareItem::Decimal( + Decimal::from_f64(99999999999.999).unwrap(), + )) + .unwrap() + .open_inner_list() + .close_inner_list() + .open_inner_list() + .inner_list_bare_item(&RefBareItem::String("somelongstringvalue")) + .unwrap() + .inner_list_bare_item(&RefBareItem::String("anotherlongstringvalue")) + .unwrap() + .inner_list_parameter( + "key", + &RefBareItem::ByteSeq("somever longstringvaluerepresentedasbytes".as_bytes()), + ) + .unwrap() + .inner_list_bare_item(&RefBareItem::Integer(145)) + .unwrap() + .close_inner_list(); + }); + }); +} + +fn serializing_ref_dict(c: &mut Criterion) { + c.bench_function("serializing_ref_dict", move |bench| { + bench.iter(|| { + let mut output = String::new(); + RefDictSerializer::new(&mut output) + .bare_item_member("a", &RefBareItem::Boolean(true)) + .unwrap() + .bare_item_member("dict_key2", &RefBareItem::Token("abcdefghigklmnoprst")) + .unwrap() + .bare_item_member("dict_key3", &RefBareItem::Integer(123456785686457)) + .unwrap() + .open_inner_list("dict_key4") + .unwrap() + .inner_list_bare_item(&RefBareItem::String("inner-list-member")) + .unwrap() + .inner_list_bare_item(&RefBareItem::ByteSeq("inner-list-member".as_bytes())) + .unwrap() + .close_inner_list() + .parameter("key", &RefBareItem::Token("aW5uZXItbGlzdC1wYXJhbWV0ZXJz")) + .unwrap(); + }); + }); +} diff --git a/third_party/rust/sfv/src/lib.rs b/third_party/rust/sfv/src/lib.rs new file mode 100644 index 000000000000..8fa6b2fc6551 --- /dev/null +++ b/third_party/rust/sfv/src/lib.rs @@ -0,0 +1,389 @@ +/*! +`sfv` crate is an implementation of IETF draft [Structured Field Values for HTTP](https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html) +for parsing and serializing structured HTTP field values. +It also exposes a set of types that might be useful for defining new structured fields. + +# Data Structures + +There are three types of structured fields: + +- `Item` - can be an `Integer`, `Decimal`, `String`, `Token`, `Byte Sequence`, or `Boolean`. It can have associated `Parameters`. +- `List` - array of zero or more members, each of which can be an `Item` or an `InnerList`, both of which can be `Parameterized`. +- `Dictionary` - ordered map of name-value pairs, where the names are short textual strings and the values are `Items` or arrays of `Items` (represented with `InnerList`), both of which can be `Parameterized`. There can be zero or more members, and their names are unique in the scope of the `Dictionary` they occur within. + +There's also a few primitive types used to construct structured field values: +- `BareItem` used as `Item`'s value or as a parameter value in `Parameters`. +- `Parameters` are an ordered map of key-value pairs that are associated with an `Item` or `InnerList`. The keys are unique within the scope the `Parameters` they occur within, and the values are `BareItem`. +- `InnerList` is an array of zero or more `Items`. Can have `Parameters`. +- `ListEntry` represents either `Item` or `InnerList` as a member of `List` or as member-value in `Dictionary`. + +# Examples + +### Parsing + +``` +use sfv::Parser; + +// Parsing structured field value of Item type. +let item_header_input = "12.445;foo=bar"; +let item = Parser::parse_item(item_header_input.as_bytes()); +assert!(item.is_ok()); +println!("{:#?}", item); + +// Parsing structured field value of List type. +let list_header_input = "1;a=tok, (\"foo\" \"bar\");baz, ()"; +let list = Parser::parse_list(list_header_input.as_bytes()); +assert!(list.is_ok()); +println!("{:#?}", list); + +// Parsing structured field value of Dictionary type. +let dict_header_input = "a=?0, b, c; foo=bar, rating=1.5, fruits=(apple pear)"; +let dict = Parser::parse_dictionary(dict_header_input.as_bytes()); +assert!(dict.is_ok()); +println!("{:#?}", dict); + +``` + +### Value Creation and Serialization +Creates `Item` with empty parameters: +``` +use sfv::{Item, BareItem, SerializeValue}; + +let str_item = Item::new(BareItem::String(String::from("foo"))); +assert_eq!(str_item.serialize_value().unwrap(), "\"foo\""); +``` + + +Creates `Item` field value with parameters: +``` +use sfv::{Item, BareItem, SerializeValue, Parameters, Decimal, FromPrimitive}; + +let mut params = Parameters::new(); +let decimal = Decimal::from_f64(13.45655).unwrap(); +params.insert("key".into(), BareItem::Decimal(decimal)); +let int_item = Item::with_params(BareItem::Integer(99), params); +assert_eq!(int_item.serialize_value().unwrap(), "99;key=13.457"); +``` + +Creates `List` field value with `Item` and parametrized `InnerList` as members: +``` +use sfv::{Item, BareItem, InnerList, List, SerializeValue, Parameters}; + +let tok_item = BareItem::Token("tok".into()); + +// Creates Item. +let str_item = Item::new(BareItem::String(String::from("foo"))); + +// Creates InnerList members. +let mut int_item_params = Parameters::new(); +int_item_params.insert("key".into(), BareItem::Boolean(false)); +let int_item = Item::with_params(BareItem::Integer(99), int_item_params); + +// Creates InnerList. +let mut inner_list_params = Parameters::new(); +inner_list_params.insert("bar".into(), BareItem::Boolean(true)); +let inner_list = InnerList::with_params(vec![int_item, str_item], inner_list_params); + + +let list: List = vec![Item::new(tok_item).into(), inner_list.into()]; +assert_eq!( + list.serialize_value().unwrap(), + "tok, (99;key=?0 \"foo\");bar" +); +``` + +Creates `Dictionary` field value: +``` +use sfv::{Parser, Item, BareItem, SerializeValue, ParseValue, Dictionary}; + +let member_value1 = Item::new(BareItem::String(String::from("apple"))); +let member_value2 = Item::new(BareItem::Boolean(true)); +let member_value3 = Item::new(BareItem::Boolean(false)); + +let mut dict = Dictionary::new(); +dict.insert("key1".into(), member_value1.into()); +dict.insert("key2".into(), member_value2.into()); +dict.insert("key3".into(), member_value3.into()); + +assert_eq!( + dict.serialize_value().unwrap(), + "key1=\"apple\", key2, key3=?0" +); + +``` +*/ + +mod parser; +mod ref_serializer; +mod serializer; +mod utils; + +#[cfg(test)] +mod test_parser; +#[cfg(test)] +mod test_serializer; +use indexmap::IndexMap; + +pub use rust_decimal::{ + prelude::{FromPrimitive, FromStr}, + Decimal, +}; + +pub use parser::{ParseMore, ParseValue, Parser}; +pub use ref_serializer::{RefDictSerializer, RefItemSerializer, RefListSerializer}; +pub use serializer::SerializeValue; + +type SFVResult = std::result::Result; + +/// Represents `Item` type structured field value. +/// Can be used as a member of `List` or `Dictionary`. +// sf-item = bare-item parameters +// bare-item = sf-integer / sf-decimal / sf-string / sf-token +// / sf-binary / sf-boolean +#[derive(Debug, PartialEq, Clone)] +pub struct Item { + /// Value of `Item`. + pub bare_item: BareItem, + /// `Item`'s associated parameters. Can be empty. + pub params: Parameters, +} + +impl Item { + /// Returns new `Item` with empty `Parameters`. + pub fn new(bare_item: BareItem) -> Item { + Item { + bare_item, + params: Parameters::new(), + } + } + /// Returns new `Item` with specified `Parameters`. + pub fn with_params(bare_item: BareItem, params: Parameters) -> Item { + Item { bare_item, params } + } +} + +/// Represents `Dictionary` type structured field value. +// sf-dictionary = dict-member *( OWS "," OWS dict-member ) +// dict-member = member-name [ "=" member-value ] +// member-name = key +// member-value = sf-item / inner-list +pub type Dictionary = IndexMap; + +/// Represents `List` type structured field value. +// sf-list = list-member *( OWS "," OWS list-member ) +// list-member = sf-item / inner-list +pub type List = Vec; + +/// Parameters of `Item` or `InnerList`. +// parameters = *( ";" *SP parameter ) +// parameter = param-name [ "=" param-value ] +// param-name = key +// key = ( lcalpha / "*" ) +// *( lcalpha / DIGIT / "_" / "-" / "." / "*" ) +// lcalpha = %x61-7A ; a-z +// param-value = bare-item +pub type Parameters = IndexMap; + +/// Represents a member of `List` or `Dictionary` structured field value. +#[derive(Debug, PartialEq, Clone)] +pub enum ListEntry { + /// Member of `Item` type. + Item(Item), + /// Member of `InnerList` (array of `Items`) type. + InnerList(InnerList), +} + +impl From for ListEntry { + fn from(item: Item) -> Self { + ListEntry::Item(item) + } +} + +impl From for ListEntry { + fn from(item: InnerList) -> Self { + ListEntry::InnerList(item) + } +} + +/// Array of `Items` with associated `Parameters`. +// inner-list = "(" *SP [ sf-item *( 1*SP sf-item ) *SP ] ")" +// parameters +#[derive(Debug, PartialEq, Clone)] +pub struct InnerList { + /// `Items` that `InnerList` contains. Can be empty. + pub items: Vec, + /// `InnerList`'s associated parameters. Can be empty. + pub params: Parameters, +} + +impl InnerList { + /// Returns new `InnerList` with empty `Parameters`. + pub fn new(items: Vec) -> InnerList { + InnerList { + items, + params: Parameters::new(), + } + } + + /// Returns new `InnerList` with specified `Parameters`. + pub fn with_params(items: Vec, params: Parameters) -> InnerList { + InnerList { items, params } + } +} + +/// `BareItem` type is used to construct `Items` or `Parameters` values. +#[derive(Debug, PartialEq, Clone)] +pub enum BareItem { + /// Decimal number + // sf-decimal = ["-"] 1*12DIGIT "." 1*3DIGIT + Decimal(Decimal), + /// Integer number + // sf-integer = ["-"] 1*15DIGIT + Integer(i64), + // sf-string = DQUOTE *chr DQUOTE + // chr = unescaped / escaped + // unescaped = %x20-21 / %x23-5B / %x5D-7E + // escaped = "\" ( DQUOTE / "\" ) + String(String), + // ":" *(base64) ":" + // base64 = ALPHA / DIGIT / "+" / "/" / "=" + ByteSeq(Vec), + // sf-boolean = "?" boolean + // boolean = "0" / "1" + Boolean(bool), + // sf-token = ( ALPHA / "*" ) *( tchar / ":" / "/" ) + Token(String), +} + +impl BareItem { + /// If `BareItem` is a decimal, returns `Decimal`, otherwise returns `None`. + /// ``` + /// # use sfv::{BareItem, Decimal, FromPrimitive}; + /// let decimal_number = Decimal::from_f64(415.566).unwrap(); + /// let bare_item: BareItem = decimal_number.into(); + /// assert_eq!(bare_item.as_decimal().unwrap(), decimal_number); + /// ``` + pub fn as_decimal(&self) -> Option { + match *self { + BareItem::Decimal(val) => Some(val), + _ => None, + } + } + /// If `BareItem` is an integer, returns `i64`, otherwise returns `None`. + /// ``` + /// # use sfv::BareItem; + /// let bare_item: BareItem = 100.into(); + /// assert_eq!(bare_item.as_int().unwrap(), 100); + /// ``` + pub fn as_int(&self) -> Option { + match *self { + BareItem::Integer(val) => Some(val), + _ => None, + } + } + /// If `BareItem` is `String`, returns `&str`, otherwise returns `None`. + /// ``` + /// # use sfv::BareItem; + /// let bare_item = BareItem::String("foo".into()); + /// assert_eq!(bare_item.as_str().unwrap(), "foo"); + /// ``` + pub fn as_str(&self) -> Option<&str> { + match *self { + BareItem::String(ref val) => Some(val), + _ => None, + } + } + /// If `BareItem` is a `ByteSeq`, returns `&Vec`, otherwise returns `None`. + /// ``` + /// # use sfv::BareItem; + /// let bare_item = BareItem::ByteSeq("foo".to_owned().into_bytes()); + /// assert_eq!(bare_item.as_byte_seq().unwrap().as_slice(), "foo".as_bytes()); + /// ``` + pub fn as_byte_seq(&self) -> Option<&Vec> { + match *self { + BareItem::ByteSeq(ref val) => Some(val), + _ => None, + } + } + /// If `BareItem` is a `Boolean`, returns `bool`, otherwise returns `None`. + /// ``` + /// # use sfv::{BareItem, Decimal, FromPrimitive}; + /// let bare_item = BareItem::Boolean(true); + /// assert_eq!(bare_item.as_bool().unwrap(), true); + /// ``` + pub fn as_bool(&self) -> Option { + match *self { + BareItem::Boolean(val) => Some(val), + _ => None, + } + } + /// If `BareItem` is a `Token`, returns `&str`, otherwise returns `None`. + /// ``` + /// use sfv::BareItem; + /// + /// let bare_item = BareItem::Token("*bar".into()); + /// assert_eq!(bare_item.as_token().unwrap(), "*bar"); + /// ``` + pub fn as_token(&self) -> Option<&str> { + match *self { + BareItem::Token(ref val) => Some(val), + _ => None, + } + } +} + +impl From for BareItem { + /// Converts `i64` into `BareItem::Integer`. + /// ``` + /// # use sfv::BareItem; + /// let bare_item: BareItem = 456.into(); + /// assert_eq!(bare_item.as_int().unwrap(), 456); + /// ``` + fn from(item: i64) -> Self { + BareItem::Integer(item) + } +} + +impl From for BareItem { + /// Converts `Decimal` into `BareItem::Decimal`. + /// ``` + /// # use sfv::{BareItem, Decimal, FromPrimitive}; + /// let decimal_number = Decimal::from_f64(48.01).unwrap(); + /// let bare_item: BareItem = decimal_number.into(); + /// assert_eq!(bare_item.as_decimal().unwrap(), decimal_number); + /// ``` + fn from(item: Decimal) -> Self { + BareItem::Decimal(item) + } +} + +#[derive(Debug, PartialEq)] +pub(crate) enum Num { + Decimal(Decimal), + Integer(i64), +} + +/// Similar to `BareItem`, but used to serialize values via `RefItemSerializer`, `RefListSerializer`, `RefDictSerializer`. +#[derive(Debug, PartialEq, Clone)] +pub enum RefBareItem<'a> { + Integer(i64), + Decimal(Decimal), + String(&'a str), + ByteSeq(&'a [u8]), + Boolean(bool), + Token(&'a str), +} + +impl BareItem { + /// Converts `BareItem` into `RefBareItem`. + fn to_ref_bare_item(&self) -> RefBareItem { + match self { + BareItem::Integer(val) => RefBareItem::Integer(*val), + BareItem::Decimal(val) => RefBareItem::Decimal(*val), + BareItem::String(val) => RefBareItem::String(val), + BareItem::ByteSeq(val) => RefBareItem::ByteSeq(val.as_slice()), + BareItem::Boolean(val) => RefBareItem::Boolean(*val), + BareItem::Token(val) => RefBareItem::Token(val), + } + } +} diff --git a/third_party/rust/sfv/src/parser.rs b/third_party/rust/sfv/src/parser.rs new file mode 100644 index 000000000000..c018a606953f --- /dev/null +++ b/third_party/rust/sfv/src/parser.rs @@ -0,0 +1,477 @@ +use crate::utils; +use crate::{ + BareItem, Decimal, Dictionary, FromStr, InnerList, Item, List, ListEntry, Num, Parameters, + SFVResult, +}; +use std::iter::Peekable; +use std::str::{from_utf8, Chars}; + +/// Implements parsing logic for each structured field value type. +pub trait ParseValue { + /// This method should not be used for parsing input into structured field value. + /// Use `Parser::parse_item`, `Parser::parse_list` or `Parsers::parse_dictionary` for that. + fn parse(input_chars: &mut Peekable) -> SFVResult + where + Self: Sized; +} + +/// If structured field value of List or Dictionary type is split into multiple lines, +/// allows to parse more lines and merge them into already existing structure field value. +pub trait ParseMore { + /// If structured field value is split across lines, + /// parses and merges next line into a single structured field value. + /// # Examples + /// ``` + /// # use sfv::{Parser, SerializeValue, ParseMore}; + /// + /// let mut list_field = Parser::parse_list("11, (12 13)".as_bytes()).unwrap(); + /// list_field.parse_more("\"foo\", \"bar\"".as_bytes()).unwrap(); + /// + /// assert_eq!(list_field.serialize_value().unwrap(), "11, (12 13), \"foo\", \"bar\""); + fn parse_more(&mut self, input_bytes: &[u8]) -> SFVResult<()> + where + Self: Sized; +} + +impl ParseValue for Item { + fn parse(input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-item + let bare_item = Parser::parse_bare_item(input_chars)?; + let params = Parser::parse_parameters(input_chars)?; + + Ok(Item { bare_item, params }) + } +} + +impl ParseValue for List { + fn parse(input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-list + // List represents an array of (item_or_inner_list, parameters) + + let mut members = vec![]; + + while input_chars.peek().is_some() { + members.push(Parser::parse_list_entry(input_chars)?); + + utils::consume_ows_chars(input_chars); + + if input_chars.peek().is_none() { + return Ok(members); + } + + if let Some(c) = input_chars.next() { + if c != ',' { + return Err("parse_list: trailing characters after list member"); + } + } + + utils::consume_ows_chars(input_chars); + + if input_chars.peek().is_none() { + return Err("parse_list: trailing comma"); + } + } + + Ok(members) + } +} + +impl ParseValue for Dictionary { + fn parse(input_chars: &mut Peekable) -> SFVResult { + let mut dict = Dictionary::new(); + + while input_chars.peek().is_some() { + let this_key = Parser::parse_key(input_chars)?; + + if let Some('=') = input_chars.peek() { + input_chars.next(); + let member = Parser::parse_list_entry(input_chars)?; + dict.insert(this_key, member); + } else { + let value = true; + let params = Parser::parse_parameters(input_chars)?; + let member = Item { + bare_item: BareItem::Boolean(value), + params, + }; + dict.insert(this_key, member.into()); + } + + utils::consume_ows_chars(input_chars); + + if input_chars.peek().is_none() { + return Ok(dict); + } + + if let Some(c) = input_chars.next() { + if c != ',' { + return Err("parse_dict: trailing characters after dictionary member"); + } + } + + utils::consume_ows_chars(input_chars); + + if input_chars.peek().is_none() { + return Err("parse_dict: trailing comma"); + } + } + Ok(dict) + } +} + +impl ParseMore for List { + fn parse_more(&mut self, input_bytes: &[u8]) -> SFVResult<()> { + let parsed_list = Parser::parse_list(input_bytes)?; + self.extend(parsed_list); + Ok(()) + } +} + +impl ParseMore for Dictionary { + fn parse_more(&mut self, input_bytes: &[u8]) -> SFVResult<()> { + let parsed_dict = Parser::parse_dictionary(input_bytes)?; + self.extend(parsed_dict); + Ok(()) + } +} + +/// Exposes methods for parsing input into structured field value. +pub struct Parser; + +impl Parser { + /// Parses input into structured field value of Dictionary type + pub fn parse_dictionary(input_bytes: &[u8]) -> SFVResult { + Self::parse::(input_bytes) + } + + /// Parses input into structured field value of List type + pub fn parse_list(input_bytes: &[u8]) -> SFVResult { + Self::parse::(input_bytes) + } + + /// Parses input into structured field value of Item type + pub fn parse_item(input_bytes: &[u8]) -> SFVResult { + Self::parse::(input_bytes) + } + + // Generic parse method for checking input before parsing + // and handling trailing text error + fn parse(input_bytes: &[u8]) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#text-parse + if !input_bytes.is_ascii() { + return Err("parse: non-ascii characters in input"); + } + + let mut input_chars = from_utf8(input_bytes) + .map_err(|_| "parse: conversion from bytes to str failed")? + .chars() + .peekable(); + utils::consume_sp_chars(&mut input_chars); + + let output = T::parse(&mut input_chars)?; + + utils::consume_sp_chars(&mut input_chars); + + if input_chars.next().is_some() { + return Err("parse: trailing characters after parsed value"); + }; + Ok(output) + } + + fn parse_list_entry(input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-item-or-list + // ListEntry represents a tuple (item_or_inner_list, parameters) + + match input_chars.peek() { + Some('(') => { + let parsed = Self::parse_inner_list(input_chars)?; + Ok(ListEntry::InnerList(parsed)) + } + _ => { + let parsed = Item::parse(input_chars)?; + Ok(ListEntry::Item(parsed)) + } + } + } + + pub(crate) fn parse_inner_list(input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-innerlist + + if Some('(') != input_chars.next() { + return Err("parse_inner_list: input does not start with '('"); + } + + let mut inner_list = Vec::new(); + while input_chars.peek().is_some() { + utils::consume_sp_chars(input_chars); + + if Some(&')') == input_chars.peek() { + input_chars.next(); + let params = Self::parse_parameters(input_chars)?; + return Ok(InnerList { + items: inner_list, + params, + }); + } + + let parsed_item = Item::parse(input_chars)?; + inner_list.push(parsed_item); + + if let Some(c) = input_chars.peek() { + if c != &' ' && c != &')' { + return Err("parse_inner_list: bad delimitation"); + } + } + } + + Err("parse_inner_list: the end of the inner list was not found") + } + + pub(crate) fn parse_bare_item(mut input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-bare-item + if input_chars.peek().is_none() { + return Err("parse_bare_item: empty item"); + } + + match input_chars.peek() { + Some(&'?') => Ok(BareItem::Boolean(Self::parse_bool(&mut input_chars)?)), + Some(&'"') => Ok(BareItem::String(Self::parse_string(&mut input_chars)?)), + Some(&':') => Ok(BareItem::ByteSeq(Self::parse_byte_sequence( + &mut input_chars, + )?)), + Some(&c) if c == '*' || c.is_ascii_alphabetic() => { + Ok(BareItem::Token(Self::parse_token(&mut input_chars)?)) + } + Some(&c) if c == '-' || c.is_ascii_digit() => { + match Self::parse_number(&mut input_chars)? { + Num::Decimal(val) => Ok(BareItem::Decimal(val)), + Num::Integer(val) => Ok(BareItem::Integer(val)), + } + } + _ => Err("parse_bare_item: item type can't be identified"), + } + } + + pub(crate) fn parse_bool(input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-boolean + + if input_chars.next() != Some('?') { + return Err("parse_bool: first character is not '?'"); + } + + match input_chars.next() { + Some('0') => Ok(false), + Some('1') => Ok(true), + _ => Err("parse_bool: invalid variant"), + } + } + + pub(crate) fn parse_string(input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-string + + if input_chars.next() != Some('\"') { + return Err("parse_string: first character is not '\"'"); + } + + let mut output_string = String::from(""); + while let Some(curr_char) = input_chars.next() { + match curr_char { + '\"' => return Ok(output_string), + '\x7f' | '\x00'..='\x1f' => return Err("parse_string: not a visible character"), + '\\' => match input_chars.next() { + Some(c) if c == '\\' || c == '\"' => { + output_string.push(c); + } + None => return Err("parse_string: last input character is '\\'"), + _ => return Err("parse_string: disallowed character after '\\'"), + }, + _ => output_string.push(curr_char), + } + } + Err("parse_string: no closing '\"'") + } + + pub(crate) fn parse_token(input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-token + + if let Some(first_char) = input_chars.peek() { + if !first_char.is_ascii_alphabetic() && first_char != &'*' { + return Err("parse_token: first character is not ALPHA or '*'"); + } + } else { + return Err("parse_token: empty input string"); + } + + let mut output_string = String::from(""); + while let Some(curr_char) = input_chars.peek() { + if !utils::is_tchar(*curr_char) && curr_char != &':' && curr_char != &'/' { + return Ok(output_string); + } + + match input_chars.next() { + Some(c) => output_string.push(c), + None => return Err("parse_token: end of the string"), + } + } + Ok(output_string) + } + + pub(crate) fn parse_byte_sequence(input_chars: &mut Peekable) -> SFVResult> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-binary + + if input_chars.next() != Some(':') { + return Err("parse_byte_seq: first char is not ':'"); + } + + if !input_chars.clone().any(|c| c == ':') { + return Err("parse_byte_seq: no closing ':'"); + } + + let b64_content = input_chars.take_while(|c| c != &':').collect::(); + if !b64_content.chars().all(utils::is_allowed_b64_content) { + return Err("parse_byte_seq: invalid char in byte sequence"); + } + match utils::base64()?.decode(b64_content.as_bytes()) { + Ok(content) => Ok(content), + Err(_) => Err("parse_byte_seq: decoding error"), + } + } + + pub(crate) fn parse_number(input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-number + + let mut sign = 1; + if let Some('-') = input_chars.peek() { + sign = -1; + input_chars.next(); + } + + match input_chars.peek() { + Some(c) if !c.is_ascii_digit() => { + return Err("parse_number: input number does not start with a digit") + } + None => return Err("parse_number: input number lacks a digit"), + _ => (), + } + + // Get number from input as a string and identify whether it's a decimal or integer + let (is_integer, input_number) = Self::extract_digits(input_chars)?; + + // Parse input_number from string into integer + if is_integer { + let output_number = input_number + .parse::() + .map_err(|_err| "parse_number: parsing i64 failed")? + * sign; + + let (min_int, max_int) = (-999_999_999_999_999_i64, 999_999_999_999_999_i64); + if !(min_int <= output_number && output_number <= max_int) { + return Err("parse_number: integer number is out of range"); + } + + return Ok(Num::Integer(output_number)); + } + + // Parse input_number from string into decimal + let chars_after_dot = input_number + .find('.') + .map(|dot_pos| input_number.len() - dot_pos - 1); + + match chars_after_dot { + Some(0) => Err("parse_number: decimal ends with '.'"), + Some(1..=3) => { + let mut output_number = Decimal::from_str(&input_number) + .map_err(|_err| "parse_number: parsing f64 failed")?; + + if sign == -1 { + output_number.set_sign_negative(true) + } + + Ok(Num::Decimal(output_number)) + } + _ => Err("parse_number: invalid decimal fraction length"), + } + } + + fn extract_digits(input_chars: &mut Peekable) -> SFVResult<(bool, String)> { + let mut is_integer = true; + let mut input_number = String::from(""); + while let Some(curr_char) = input_chars.peek() { + if curr_char.is_ascii_digit() { + input_number.push(*curr_char); + input_chars.next(); + } else if curr_char == &'.' && is_integer { + if input_number.len() > 12 { + return Err( + "parse_number: decimal too long, illegal position for decimal point", + ); + } + input_number.push(*curr_char); + is_integer = false; + input_chars.next(); + } else { + break; + } + + if is_integer && input_number.len() > 15 { + return Err("parse_number: integer too long, length > 15"); + } + + if !is_integer && input_number.len() > 16 { + return Err("parse_number: decimal too long, length > 16"); + } + } + Ok((is_integer, input_number)) + } + + pub(crate) fn parse_parameters(input_chars: &mut Peekable) -> SFVResult { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#parse-param + + let mut params = Parameters::new(); + + while let Some(curr_char) = input_chars.peek() { + if curr_char == &';' { + input_chars.next(); + } else { + break; + } + + utils::consume_sp_chars(input_chars); + + let param_name = Self::parse_key(input_chars)?; + let param_value = match input_chars.peek() { + Some('=') => { + input_chars.next(); + Self::parse_bare_item(input_chars)? + } + _ => BareItem::Boolean(true), + }; + params.insert(param_name, param_value); + } + + // If parameters already contains a name param_name (comparing character-for-character), overwrite its value. + // Note that when duplicate Parameter keys are encountered, this has the effect of ignoring all but the last instance. + Ok(params) + } + + pub(crate) fn parse_key(input_chars: &mut Peekable) -> SFVResult { + match input_chars.peek() { + Some(c) if c == &'*' || c.is_ascii_lowercase() => (), + _ => return Err("parse_key: first character is not lcalpha or '*'"), + } + + let mut output = String::new(); + while let Some(curr_char) = input_chars.peek() { + if !curr_char.is_ascii_lowercase() + && !curr_char.is_ascii_digit() + && !"_-*.".contains(*curr_char) + { + return Ok(output); + } + + output.push(*curr_char); + input_chars.next(); + } + Ok(output) + } +} diff --git a/third_party/rust/sfv/src/ref_serializer.rs b/third_party/rust/sfv/src/ref_serializer.rs new file mode 100644 index 000000000000..4bcd29ff3a84 --- /dev/null +++ b/third_party/rust/sfv/src/ref_serializer.rs @@ -0,0 +1,310 @@ +use crate::serializer::Serializer; +use crate::{RefBareItem, SFVResult}; +use std::marker::PhantomData; + +/// Serializes `Item` field value components incrementally. +/// ``` +/// use sfv::{RefBareItem, RefItemSerializer}; +/// +/// let mut serialized_item = String::new(); +/// let serializer = RefItemSerializer::new(&mut serialized_item); +/// serializer +/// .bare_item(&RefBareItem::Integer(11)) +/// .unwrap() +/// .parameter("foo", &RefBareItem::Boolean(true)) +/// .unwrap(); +/// assert_eq!(serialized_item, "11;foo"); +/// ``` +#[derive(Debug)] +pub struct RefItemSerializer<'a> { + pub buffer: &'a mut String, +} + +impl<'a> RefItemSerializer<'a> { + pub fn new(buffer: &'a mut String) -> Self { + RefItemSerializer { buffer } + } + + pub fn bare_item(self, bare_item: &RefBareItem) -> SFVResult> { + Serializer::serialize_ref_bare_item(bare_item, self.buffer)?; + Ok(RefParameterSerializer { + buffer: self.buffer, + }) + } +} + +/// Used by `RefItemSerializer`, `RefListSerializer`, `RefDictSerializer` to serialize a single `Parameter`. +#[derive(Debug)] +pub struct RefParameterSerializer<'a> { + buffer: &'a mut String, +} + +impl<'a> RefParameterSerializer<'a> { + pub fn parameter(self, name: &str, value: &RefBareItem) -> SFVResult { + Serializer::serialize_ref_parameter(name, value, self.buffer)?; + Ok(self) + } +} + +/// Serializes `List` field value components incrementally. +/// ``` +/// use sfv::{RefBareItem, RefListSerializer}; +/// +/// let mut serialized_item = String::new(); +/// let serializer = RefListSerializer::new(&mut serialized_item); +/// serializer +/// .bare_item(&RefBareItem::Integer(11)) +/// .unwrap() +/// .parameter("foo", &RefBareItem::Boolean(true)) +/// .unwrap() +/// .open_inner_list() +/// .inner_list_bare_item(&RefBareItem::Token("abc")) +/// .unwrap() +/// .inner_list_parameter("abc_param", &RefBareItem::Boolean(false)) +/// .unwrap() +/// .inner_list_bare_item(&RefBareItem::Token("def")) +/// .unwrap() +/// .close_inner_list() +/// .parameter("bar", &RefBareItem::String("val")) +/// .unwrap(); +/// assert_eq!( +/// serialized_item, +/// "11;foo, (abc;abc_param=?0 def);bar=\"val\"" +/// ); +/// ``` +#[derive(Debug)] +pub struct RefListSerializer<'a> { + buffer: &'a mut String, +} + +impl<'a> RefListSerializer<'a> { + pub fn new(buffer: &'a mut String) -> Self { + RefListSerializer { buffer } + } + + pub fn bare_item(self, bare_item: &RefBareItem) -> SFVResult { + if !self.buffer.is_empty() { + self.buffer.push_str(", "); + } + Serializer::serialize_ref_bare_item(bare_item, self.buffer)?; + Ok(RefListSerializer { + buffer: self.buffer, + }) + } + + pub fn parameter(self, name: &str, value: &RefBareItem) -> SFVResult { + if self.buffer.is_empty() { + return Err("parameters must be serialized after bare item or inner list"); + } + Serializer::serialize_ref_parameter(name, value, self.buffer)?; + Ok(RefListSerializer { + buffer: self.buffer, + }) + } + pub fn open_inner_list(self) -> RefInnerListSerializer<'a, Self> { + if !self.buffer.is_empty() { + self.buffer.push_str(", "); + } + self.buffer.push('('); + RefInnerListSerializer:: { + buffer: self.buffer, + caller_type: PhantomData, + } + } +} + +/// Serializes `Dictionary` field value components incrementally. +/// ``` +/// use sfv::{RefBareItem, RefDictSerializer, Decimal, FromPrimitive}; +/// +/// let mut serialized_item = String::new(); +/// let serializer = RefDictSerializer::new(&mut serialized_item); +/// serializer +/// .bare_item_member("member1", &RefBareItem::Integer(11)) +/// .unwrap() +/// .parameter("foo", &RefBareItem::Boolean(true)) +/// .unwrap() +/// .open_inner_list("member2") +/// .unwrap() +/// .inner_list_bare_item(&RefBareItem::Token("abc")) +/// .unwrap() +/// .inner_list_parameter("abc_param", &RefBareItem::Boolean(false)) +/// .unwrap() +/// .inner_list_bare_item(&RefBareItem::Token("def")) +/// .unwrap() +/// .close_inner_list() +/// .parameter("bar", &RefBareItem::String("val")) +/// .unwrap() +/// .bare_item_member( +/// "member3", +/// &RefBareItem::Decimal(Decimal::from_f64(12.34566).unwrap()), +/// ) +/// .unwrap(); +/// assert_eq!( +/// serialized_item, +/// "member1=11;foo, member2=(abc;abc_param=?0 def);bar=\"val\", member3=12.346" +/// ); +/// ``` +#[derive(Debug)] +pub struct RefDictSerializer<'a> { + buffer: &'a mut String, +} + +impl<'a> RefDictSerializer<'a> { + pub fn new(buffer: &'a mut String) -> Self { + RefDictSerializer { buffer } + } + + pub fn bare_item_member(self, name: &str, value: &RefBareItem) -> SFVResult { + if !self.buffer.is_empty() { + self.buffer.push_str(", "); + } + Serializer::serialize_key(name, self.buffer)?; + if value != &RefBareItem::Boolean(true) { + self.buffer.push('='); + Serializer::serialize_ref_bare_item(value, self.buffer)?; + } + Ok(self) + } + + pub fn parameter(self, name: &str, value: &RefBareItem) -> SFVResult { + if self.buffer.is_empty() { + return Err("parameters must be serialized after bare item or inner list"); + } + Serializer::serialize_ref_parameter(name, value, self.buffer)?; + Ok(RefDictSerializer { + buffer: self.buffer, + }) + } + + pub fn open_inner_list(self, name: &str) -> SFVResult> { + if !self.buffer.is_empty() { + self.buffer.push_str(", "); + } + Serializer::serialize_key(name, self.buffer)?; + self.buffer.push_str("=("); + Ok(RefInnerListSerializer:: { + buffer: self.buffer, + caller_type: PhantomData, + }) + } +} + +/// Used by `RefItemSerializer`, `RefListSerializer`, `RefDictSerializer` to serialize `InnerList`. +#[derive(Debug)] +pub struct RefInnerListSerializer<'a, T> { + buffer: &'a mut String, + caller_type: PhantomData, +} + +impl<'a, T: Container<'a>> RefInnerListSerializer<'a, T> { + pub fn inner_list_bare_item(self, bare_item: &RefBareItem) -> SFVResult { + if !self.buffer.is_empty() & !self.buffer.ends_with('(') { + self.buffer.push(' '); + } + Serializer::serialize_ref_bare_item(bare_item, self.buffer)?; + Ok(RefInnerListSerializer { + buffer: self.buffer, + caller_type: PhantomData, + }) + } + + pub fn inner_list_parameter(self, name: &str, value: &RefBareItem) -> SFVResult { + if self.buffer.is_empty() { + return Err("parameters must be serialized after bare item or inner list"); + } + Serializer::serialize_ref_parameter(name, value, self.buffer)?; + Ok(RefInnerListSerializer { + buffer: self.buffer, + caller_type: PhantomData, + }) + } + + pub fn close_inner_list(self) -> T { + self.buffer.push(')'); + T::new(self.buffer) + } +} + +pub trait Container<'a> { + fn new(buffer: &'a mut String) -> Self; +} + +impl<'a> Container<'a> for RefListSerializer<'a> { + fn new(buffer: &mut String) -> RefListSerializer { + RefListSerializer { buffer } + } +} + +impl<'a> Container<'a> for RefDictSerializer<'a> { + fn new(buffer: &mut String) -> RefDictSerializer { + RefDictSerializer { buffer } + } +} + +#[cfg(test)] +mod alternative_serializer_tests { + use super::*; + use crate::{Decimal, FromPrimitive}; + + #[test] + fn test_fast_serialize_item() -> SFVResult<()> { + let mut output = String::new(); + let ser = RefItemSerializer::new(&mut output); + ser.bare_item(&RefBareItem::Token("hello"))? + .parameter("abc", &RefBareItem::Boolean(true))?; + assert_eq!("hello;abc", output); + Ok(()) + } + + #[test] + fn test_fast_serialize_list() -> SFVResult<()> { + let mut output = String::new(); + let ser = RefListSerializer::new(&mut output); + ser.bare_item(&RefBareItem::Token("hello"))? + .parameter("key1", &RefBareItem::Boolean(true))? + .parameter("key2", &RefBareItem::Boolean(false))? + .open_inner_list() + .inner_list_bare_item(&RefBareItem::String("some_string"))? + .inner_list_bare_item(&RefBareItem::Integer(12))? + .inner_list_parameter("inner-member-key", &RefBareItem::Boolean(true))? + .close_inner_list() + .parameter("inner-list-param", &RefBareItem::Token("*"))?; + assert_eq!( + "hello;key1;key2=?0, (\"some_string\" 12;inner-member-key);inner-list-param=*", + output + ); + Ok(()) + } + + #[test] + fn test_fast_serialize_dict() -> SFVResult<()> { + let mut output = String::new(); + let ser = RefDictSerializer::new(&mut output); + ser.bare_item_member("member1", &RefBareItem::Token("hello"))? + .parameter("key1", &RefBareItem::Boolean(true))? + .parameter("key2", &RefBareItem::Boolean(false))? + .bare_item_member("member2", &RefBareItem::Boolean(true))? + .parameter( + "key3", + &RefBareItem::Decimal(Decimal::from_f64(45.4586).unwrap()), + )? + .parameter("key4", &RefBareItem::String("str"))? + .open_inner_list("key5")? + .inner_list_bare_item(&RefBareItem::Integer(45))? + .inner_list_bare_item(&RefBareItem::Integer(0))? + .close_inner_list() + .bare_item_member("key6", &RefBareItem::String("foo"))? + .open_inner_list("key7")? + .inner_list_bare_item(&RefBareItem::ByteSeq("some_string".as_bytes()))? + .inner_list_bare_item(&RefBareItem::ByteSeq("other_string".as_bytes()))? + .close_inner_list() + .parameter("lparam", &RefBareItem::Integer(10))? + .bare_item_member("key8", &RefBareItem::Boolean(true))?; + assert_eq!( + "member1=hello;key1;key2=?0, member2;key3=45.459;key4=\"str\", key5=(45 0), key6=\"foo\", key7=(:c29tZV9zdHJpbmc=: :b3RoZXJfc3RyaW5n:);lparam=10, key8", + output + ); + Ok(()) + } +} diff --git a/third_party/rust/sfv/src/serializer.rs b/third_party/rust/sfv/src/serializer.rs new file mode 100644 index 000000000000..f490aaf9ddde --- /dev/null +++ b/third_party/rust/sfv/src/serializer.rs @@ -0,0 +1,320 @@ +use crate::utils; +use crate::{ + BareItem, Decimal, Dictionary, InnerList, Item, List, ListEntry, Parameters, RefBareItem, + SFVResult, +}; +use data_encoding::BASE64; +use rust_decimal::prelude::Zero; + +/// Serializes structured field value into String. +pub trait SerializeValue { + /// Serializes structured field value into String. + /// # Examples + /// ``` + /// # use sfv::{Parser, SerializeValue, ParseValue}; + /// + /// let parsed_list_field = Parser::parse_list("\"london\", \t\t\"berlin\"".as_bytes()); + /// assert!(parsed_list_field.is_ok()); + /// + /// assert_eq!( + /// parsed_list_field.unwrap().serialize_value().unwrap(), + /// "\"london\", \"berlin\"" + /// ); + /// ``` + fn serialize_value(&self) -> SFVResult; +} + +impl SerializeValue for Dictionary { + fn serialize_value(&self) -> SFVResult { + let mut output = String::new(); + Serializer::serialize_dict(self, &mut output)?; + Ok(output) + } +} + +impl SerializeValue for List { + fn serialize_value(&self) -> SFVResult { + let mut output = String::new(); + Serializer::serialize_list(self, &mut output)?; + Ok(output) + } +} + +impl SerializeValue for Item { + fn serialize_value(&self) -> SFVResult { + let mut output = String::new(); + Serializer::serialize_item(self, &mut output)?; + Ok(output) + } +} + +/// Container serialization functions +pub(crate) struct Serializer; + +impl Serializer { + pub(crate) fn serialize_item(input_item: &Item, output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-item + + Self::serialize_bare_item(&input_item.bare_item, output)?; + Self::serialize_parameters(&input_item.params, output)?; + Ok(()) + } + + #[deny(clippy::ptr_arg)] + pub(crate) fn serialize_list(input_list: &List, output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-list + if input_list.len() == 0 { + return Err("serialize_list: serializing empty field is not allowed"); + } + + for (idx, member) in input_list.iter().enumerate() { + match member { + ListEntry::Item(item) => { + Self::serialize_item(item, output)?; + } + ListEntry::InnerList(inner_list) => { + Self::serialize_inner_list(inner_list, output)?; + } + }; + + // If more items remain in input_list: + // Append “,” to output. + // Append a single SP to output. + if idx < input_list.len() - 1 { + output.push_str(", "); + } + } + Ok(()) + } + + pub(crate) fn serialize_dict(input_dict: &Dictionary, output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-dictionary + if input_dict.len() == 0 { + return Err("serialize_dictionary: serializing empty field is not allowed"); + } + + for (idx, (member_name, member_value)) in input_dict.iter().enumerate() { + Serializer::serialize_key(member_name, output)?; + + match member_value { + ListEntry::Item(ref item) => { + // If dict member is boolean true, no need to serialize it: only its params must be serialized + // Otherwise serialize entire item with its params + if item.bare_item == BareItem::Boolean(true) { + Self::serialize_parameters(&item.params, output)?; + } else { + output.push('='); + Self::serialize_item(&item, output)?; + } + } + ListEntry::InnerList(inner_list) => { + output.push('='); + Self::serialize_inner_list(&inner_list, output)?; + } + } + + // If more items remain in input_dictionary: + // Append “,” to output. + // Append a single SP to output. + if idx < input_dict.len() - 1 { + output.push_str(", "); + } + } + Ok(()) + } + + fn serialize_inner_list(input_inner_list: &InnerList, output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-innerlist + + let items = &input_inner_list.items; + let inner_list_parameters = &input_inner_list.params; + + output.push('('); + for (idx, item) in items.iter().enumerate() { + Self::serialize_item(item, output)?; + + // If more values remain in inner_list, append a single SP to output + if idx < items.len() - 1 { + output.push_str(" "); + } + } + output.push(')'); + Self::serialize_parameters(inner_list_parameters, output)?; + Ok(()) + } + + pub(crate) fn serialize_bare_item( + input_bare_item: &BareItem, + output: &mut String, + ) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-bare-item + + let ref_bare_item = input_bare_item.to_ref_bare_item(); + Self::serialize_ref_bare_item(&ref_bare_item, output) + } + + pub(crate) fn serialize_ref_bare_item( + value: &RefBareItem, + output: &mut String, + ) -> SFVResult<()> { + match value { + RefBareItem::Boolean(value) => Self::serialize_bool(*value, output)?, + RefBareItem::String(value) => Self::serialize_string(value, output)?, + RefBareItem::ByteSeq(value) => Self::serialize_byte_sequence(value, output)?, + RefBareItem::Token(value) => Self::serialize_token(value, output)?, + RefBareItem::Integer(value) => Self::serialize_integer(*value, output)?, + RefBareItem::Decimal(value) => Self::serialize_decimal(*value, output)?, + }; + Ok(()) + } + + pub(crate) fn serialize_parameters( + input_params: &Parameters, + output: &mut String, + ) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-params + + for (param_name, param_value) in input_params.iter() { + Self::serialize_ref_parameter(param_name, ¶m_value.to_ref_bare_item(), output)?; + } + Ok(()) + } + + pub(crate) fn serialize_ref_parameter( + name: &str, + value: &RefBareItem, + output: &mut String, + ) -> SFVResult<()> { + output.push(';'); + Self::serialize_key(name, output)?; + + if value != &RefBareItem::Boolean(true) { + output.push('='); + Self::serialize_ref_bare_item(value, output)?; + } + Ok(()) + } + + pub(crate) fn serialize_key(input_key: &str, output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-key + + let disallowed_chars = + |c: char| !(c.is_ascii_lowercase() || c.is_ascii_digit() || "_-*.".contains(c)); + + if input_key.chars().any(disallowed_chars) { + return Err("serialize_key: disallowed character in input"); + } + + if let Some(char) = input_key.chars().next() { + if !(char.is_ascii_lowercase() || char == '*') { + return Err("serialize_key: first character is not lcalpha or '*'"); + } + } + output.push_str(input_key); + Ok(()) + } + + pub(crate) fn serialize_integer(value: i64, output: &mut String) -> SFVResult<()> { + //https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-integer + + let (min_int, max_int) = (-999_999_999_999_999_i64, 999_999_999_999_999_i64); + if !(min_int <= value && value <= max_int) { + return Err("serialize_integer: integer is out of range"); + } + output.push_str(&value.to_string()); + Ok(()) + } + + pub(crate) fn serialize_decimal(value: Decimal, output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-decimal + + let integer_comp_length = 12; + let fraction_length = 3; + + let decimal = value.round_dp(fraction_length); + let int_comp = decimal.trunc(); + let fract_comp = decimal.fract(); + + // TODO: Replace with > 999_999_999_999_u64 + if int_comp.abs().to_string().len() > integer_comp_length { + return Err("serialize_decimal: integer component > 12 digits"); + } + + if fract_comp.is_zero() { + output.push_str(&int_comp.to_string()); + output.push('.'); + output.push('0'); + } else { + output.push_str(&decimal.to_string()); + } + + Ok(()) + } + + pub(crate) fn serialize_string(value: &str, output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-integer + + if !value.is_ascii() { + return Err("serialize_string: non-ascii character"); + } + + let vchar_or_sp = |char| char == '\x7f' || (char >= '\x00' && char <= '\x1f'); + if value.chars().any(vchar_or_sp) { + return Err("serialize_string: not a visible character"); + } + + output.push('\"'); + for char in value.chars() { + if char == '\\' || char == '\"' { + output.push('\\'); + } + output.push(char); + } + output.push('\"'); + + Ok(()) + } + + pub(crate) fn serialize_token(value: &str, output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-token + + if !value.is_ascii() { + return Err("serialize_string: non-ascii character"); + } + + let mut chars = value.chars(); + if let Some(char) = chars.next() { + if !(char.is_ascii_alphabetic() || char == '*') { + return Err("serialise_token: first character is not ALPHA or '*'"); + } + } + + if chars + .clone() + .any(|c| !(utils::is_tchar(c) || c == ':' || c == '/')) + { + return Err("serialise_token: disallowed character"); + } + + output.push_str(value); + Ok(()) + } + + pub(crate) fn serialize_byte_sequence(value: &[u8], output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-binary + + output.push(':'); + let encoded = BASE64.encode(value.as_ref()); + output.push_str(&encoded); + output.push(':'); + Ok(()) + } + + pub(crate) fn serialize_bool(value: bool, output: &mut String) -> SFVResult<()> { + // https://httpwg.org/http-extensions/draft-ietf-httpbis-header-structure.html#ser-boolean + + let val = if value { "?1" } else { "?0" }; + output.push_str(val); + Ok(()) + } +} diff --git a/third_party/rust/sfv/src/test_parser.rs b/third_party/rust/sfv/src/test_parser.rs new file mode 100644 index 000000000000..97404eb9511e --- /dev/null +++ b/third_party/rust/sfv/src/test_parser.rs @@ -0,0 +1,850 @@ +use crate::FromStr; +use crate::{BareItem, Decimal, Dictionary, InnerList, Item, List, Num, Parameters}; +use crate::{ParseMore, ParseValue, Parser}; +use std::error::Error; +use std::iter::FromIterator; + +#[test] +fn parse() -> Result<(), Box> { + let input = "\"some_value\"".as_bytes(); + let parsed_item = Item::new(BareItem::String("some_value".to_owned())); + let expected = parsed_item; + assert_eq!(expected, Parser::parse_item(input)?); + + let input = "12.35;a ".as_bytes(); + let params = Parameters::from_iter(vec![("a".to_owned(), BareItem::Boolean(true))]); + let expected = Item::with_params(Decimal::from_str("12.35")?.into(), params); + + assert_eq!(expected, Parser::parse_item(input)?); + Ok(()) +} + +#[test] +fn parse_errors() -> Result<(), Box> { + let input = "\"some_value¢\"".as_bytes(); + assert_eq!( + Err("parse: non-ascii characters in input"), + Parser::parse_item(input) + ); + let input = "\"some_value\" trailing_text".as_bytes(); + assert_eq!( + Err("parse: trailing characters after parsed value"), + Parser::parse_item(input) + ); + assert_eq!( + Err("parse_bare_item: empty item"), + Parser::parse_item("".as_bytes()) + ); + Ok(()) +} + +#[test] +fn parse_list_of_numbers() -> Result<(), Box> { + let mut input = "1,42".chars().peekable(); + let item1 = Item::new(1.into()); + let item2 = Item::new(42.into()); + let expected_list: List = vec![item1.into(), item2.into()]; + assert_eq!(expected_list, List::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_list_with_multiple_spaces() -> Result<(), Box> { + let mut input = "1 , 42".chars().peekable(); + let item1 = Item::new(1.into()); + let item2 = Item::new(42.into()); + let expected_list: List = vec![item1.into(), item2.into()]; + assert_eq!(expected_list, List::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_list_of_lists() -> Result<(), Box> { + let mut input = "(1 2), (42 43)".chars().peekable(); + let item1 = Item::new(1.into()); + let item2 = Item::new(2.into()); + let item3 = Item::new(42.into()); + let item4 = Item::new(43.into()); + let inner_list_1 = InnerList::new(vec![item1, item2]); + let inner_list_2 = InnerList::new(vec![item3, item4]); + let expected_list: List = vec![inner_list_1.into(), inner_list_2.into()]; + assert_eq!(expected_list, List::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_list_empty_inner_list() -> Result<(), Box> { + let mut input = "()".chars().peekable(); + let inner_list = InnerList::new(vec![]); + let expected_list: List = vec![inner_list.into()]; + assert_eq!(expected_list, List::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_list_empty() -> Result<(), Box> { + let mut input = "".chars().peekable(); + let expected_list: List = vec![]; + assert_eq!(expected_list, List::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_list_of_lists_with_param_and_spaces() -> Result<(), Box> { + let mut input = "( 1 42 ); k=*".chars().peekable(); + let item1 = Item::new(1.into()); + let item2 = Item::new(42.into()); + let inner_list_param = + Parameters::from_iter(vec![("k".to_owned(), BareItem::Token("*".to_owned()))]); + let inner_list = InnerList::with_params(vec![item1, item2], inner_list_param); + let expected_list: List = vec![inner_list.into()]; + assert_eq!(expected_list, List::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_list_of_items_and_lists_with_param() -> Result<(), Box> { + let mut input = "12, 14, (a b); param=\"param_value_1\", ()" + .chars() + .peekable(); + let item1 = Item::new(12.into()); + let item2 = Item::new(14.into()); + let item3 = Item::new(BareItem::Token("a".to_owned())); + let item4 = Item::new(BareItem::Token("b".to_owned())); + let inner_list_param = Parameters::from_iter(vec![( + "param".to_owned(), + BareItem::String("param_value_1".to_owned()), + )]); + let inner_list = InnerList::with_params(vec![item3, item4], inner_list_param); + let empty_inner_list = InnerList::new(vec![]); + let expected_list: List = vec![ + item1.into(), + item2.into(), + inner_list.into(), + empty_inner_list.into(), + ]; + assert_eq!(expected_list, List::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_list_errors() -> Result<(), Box> { + let mut input = ",".chars().peekable(); + assert_eq!( + Err("parse_bare_item: item type can't be identified"), + List::parse(&mut input) + ); + + let mut input = "a, b c".chars().peekable(); + assert_eq!( + Err("parse_list: trailing characters after list member"), + List::parse(&mut input) + ); + + let mut input = "a,".chars().peekable(); + assert_eq!(Err("parse_list: trailing comma"), List::parse(&mut input)); + + let mut input = "a , ".chars().peekable(); + assert_eq!(Err("parse_list: trailing comma"), List::parse(&mut input)); + + let mut input = "a\t \t ,\t ".chars().peekable(); + assert_eq!(Err("parse_list: trailing comma"), List::parse(&mut input)); + + let mut input = "a\t\t,\t\t\t".chars().peekable(); + assert_eq!(Err("parse_list: trailing comma"), List::parse(&mut input)); + + let mut input = "(a b),".chars().peekable(); + assert_eq!(Err("parse_list: trailing comma"), List::parse(&mut input)); + + let mut input = "(1, 2, (a b)".chars().peekable(); + assert_eq!( + Err("parse_inner_list: bad delimitation"), + List::parse(&mut input) + ); + + Ok(()) +} + +#[test] +fn parse_inner_list_errors() -> Result<(), Box> { + let mut input = "c b); a=1".chars().peekable(); + assert_eq!( + Err("parse_inner_list: input does not start with '('"), + Parser::parse_inner_list(&mut input) + ); + Ok(()) +} + +#[test] +fn parse_inner_list_with_param_and_spaces() -> Result<(), Box> { + let mut input = "(c b); a=1".chars().peekable(); + let inner_list_param = Parameters::from_iter(vec![("a".to_owned(), 1.into())]); + + let item1 = Item::new(BareItem::Token("c".to_owned())); + let item2 = Item::new(BareItem::Token("b".to_owned())); + let expected = InnerList::with_params(vec![item1, item2], inner_list_param); + assert_eq!(expected, Parser::parse_inner_list(&mut input)?); + Ok(()) +} + +#[test] +fn parse_item_int_with_space() -> Result<(), Box> { + let mut input = "12 ".chars().peekable(); + assert_eq!(Item::new(12.into()), Item::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_item_decimal_with_bool_param_and_space() -> Result<(), Box> { + let mut input = "12.35;a ".chars().peekable(); + let param = Parameters::from_iter(vec![("a".to_owned(), BareItem::Boolean(true))]); + assert_eq!( + Item::with_params(Decimal::from_str("12.35")?.into(), param), + Item::parse(&mut input)? + ); + Ok(()) +} + +#[test] +fn parse_item_number_with_param() -> Result<(), Box> { + let param = Parameters::from_iter(vec![("a1".to_owned(), BareItem::Token("*".to_owned()))]); + assert_eq!( + Item::with_params(BareItem::String("12.35".to_owned()), param), + Item::parse(&mut "\"12.35\";a1=*".chars().peekable())? + ); + Ok(()) +} + +#[test] +fn parse_item_errors() -> Result<(), Box> { + assert_eq!( + Err("parse_bare_item: empty item"), + Item::parse(&mut "".chars().peekable()) + ); + Ok(()) +} + +#[test] +fn parse_dict_empty() -> Result<(), Box> { + assert_eq!( + Dictionary::new(), + Dictionary::parse(&mut "".chars().peekable())? + ); + Ok(()) +} + +#[test] +fn parse_dict_errors() -> Result<(), Box> { + let mut input = "abc=123;a=1;b=2 def".chars().peekable(); + assert_eq!( + Err("parse_dict: trailing characters after dictionary member"), + Dictionary::parse(&mut input) + ); + let mut input = "abc=123;a=1,".chars().peekable(); + assert_eq!( + Err("parse_dict: trailing comma"), + Dictionary::parse(&mut input) + ); + Ok(()) +} + +#[test] +fn parse_dict_with_spaces_and_params() -> Result<(), Box> { + let mut input = "abc=123;a=1;b=2, def=456, ghi=789;q=9;r=\"+w\"" + .chars() + .peekable(); + let item1_params = + Parameters::from_iter(vec![("a".to_owned(), 1.into()), ("b".to_owned(), 2.into())]); + let item3_params = Parameters::from_iter(vec![ + ("q".to_owned(), 9.into()), + ("r".to_owned(), BareItem::String("+w".to_owned())), + ]); + + let item1 = Item::with_params(123.into(), item1_params); + let item2 = Item::new(456.into()); + let item3 = Item::with_params(789.into(), item3_params); + + let expected_dict = Dictionary::from_iter(vec![ + ("abc".to_owned(), item1.into()), + ("def".to_owned(), item2.into()), + ("ghi".to_owned(), item3.into()), + ]); + assert_eq!(expected_dict, Dictionary::parse(&mut input)?); + + Ok(()) +} + +#[test] +fn parse_dict_empty_value() -> Result<(), Box> { + let mut input = "a=()".chars().peekable(); + let inner_list = InnerList::new(vec![]); + let expected_dict = Dictionary::from_iter(vec![("a".to_owned(), inner_list.into())]); + assert_eq!(expected_dict, Dictionary::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_dict_with_token_param() -> Result<(), Box> { + let mut input = "a=1, b;foo=*, c=3".chars().peekable(); + let item2_params = + Parameters::from_iter(vec![("foo".to_owned(), BareItem::Token("*".to_owned()))]); + let item1 = Item::new(1.into()); + let item2 = Item::with_params(BareItem::Boolean(true), item2_params); + let item3 = Item::new(3.into()); + let expected_dict = Dictionary::from_iter(vec![ + ("a".to_owned(), item1.into()), + ("b".to_owned(), item2.into()), + ("c".to_owned(), item3.into()), + ]); + assert_eq!(expected_dict, Dictionary::parse(&mut input)?); + Ok(()) +} + +#[test] +fn parse_dict_multiple_spaces() -> Result<(), Box> { + // input1, input2, input3 must be parsed into the same structure + let item1 = Item::new(1.into()); + let item2 = Item::new(2.into()); + let expected_dict = Dictionary::from_iter(vec![ + ("a".to_owned(), item1.into()), + ("b".to_owned(), item2.into()), + ]); + + let mut input1 = "a=1 , b=2".chars().peekable(); + let mut input2 = "a=1\t,\tb=2".chars().peekable(); + let mut input3 = "a=1, b=2".chars().peekable(); + assert_eq!(expected_dict, Dictionary::parse(&mut input1)?); + assert_eq!(expected_dict, Dictionary::parse(&mut input2)?); + assert_eq!(expected_dict, Dictionary::parse(&mut input3)?); + + Ok(()) +} + +#[test] +fn parse_bare_item() -> Result<(), Box> { + assert_eq!( + BareItem::Boolean(false), + Parser::parse_bare_item(&mut "?0".chars().peekable())? + ); + assert_eq!( + BareItem::String("test string".to_owned()), + Parser::parse_bare_item(&mut "\"test string\"".chars().peekable())? + ); + assert_eq!( + BareItem::Token("*token".to_owned()), + Parser::parse_bare_item(&mut "*token".chars().peekable())? + ); + assert_eq!( + BareItem::ByteSeq("base_64 encoding test".to_owned().into_bytes()), + Parser::parse_bare_item(&mut ":YmFzZV82NCBlbmNvZGluZyB0ZXN0:".chars().peekable())? + ); + assert_eq!( + BareItem::Decimal(Decimal::from_str("-3.55")?), + Parser::parse_bare_item(&mut "-3.55".chars().peekable())? + ); + Ok(()) +} + +#[test] +fn parse_bare_item_errors() -> Result<(), Box> { + assert_eq!( + Err("parse_bare_item: item type can't be identified"), + Parser::parse_bare_item(&mut "!?0".chars().peekable()) + ); + assert_eq!( + Err("parse_bare_item: item type can't be identified"), + Parser::parse_bare_item(&mut "_11abc".chars().peekable()) + ); + assert_eq!( + Err("parse_bare_item: item type can't be identified"), + Parser::parse_bare_item(&mut " ".chars().peekable()) + ); + Ok(()) +} + +#[test] +fn parse_bool() -> Result<(), Box> { + let mut input = "?0gk".chars().peekable(); + assert_eq!(false, Parser::parse_bool(&mut input)?); + assert_eq!(input.collect::(), "gk"); + + assert_eq!(false, Parser::parse_bool(&mut "?0".chars().peekable())?); + assert_eq!(true, Parser::parse_bool(&mut "?1".chars().peekable())?); + Ok(()) +} + +#[test] +fn parse_bool_errors() -> Result<(), Box> { + assert_eq!( + Err("parse_bool: first character is not '?'"), + Parser::parse_bool(&mut "".chars().peekable()) + ); + assert_eq!( + Err("parse_bool: invalid variant"), + Parser::parse_bool(&mut "?".chars().peekable()) + ); + Ok(()) +} + +#[test] +fn parse_string() -> Result<(), Box> { + let mut input = "\"some string\" ;not string".chars().peekable(); + assert_eq!("some string".to_owned(), Parser::parse_string(&mut input)?); + assert_eq!(input.collect::(), " ;not string"); + + assert_eq!( + "test".to_owned(), + Parser::parse_string(&mut "\"test\"".chars().peekable())? + ); + assert_eq!( + r#"te\st"#.to_owned(), + Parser::parse_string(&mut "\"te\\\\st\"".chars().peekable())? + ); + assert_eq!( + "".to_owned(), + Parser::parse_string(&mut "\"\"".chars().peekable())? + ); + assert_eq!( + "some string".to_owned(), + Parser::parse_string(&mut "\"some string\"".chars().peekable())? + ); + Ok(()) +} + +#[test] +fn parse_string_errors() -> Result<(), Box> { + assert_eq!( + Err("parse_string: first character is not '\"'"), + Parser::parse_string(&mut "test".chars().peekable()) + ); + assert_eq!( + Err("parse_string: last input character is '\\'"), + Parser::parse_string(&mut "\"\\".chars().peekable()) + ); + assert_eq!( + Err("parse_string: disallowed character after '\\'"), + Parser::parse_string(&mut "\"\\l\"".chars().peekable()) + ); + assert_eq!( + Err("parse_string: not a visible character"), + Parser::parse_string(&mut "\"\u{1f}\"".chars().peekable()) + ); + assert_eq!( + Err("parse_string: no closing '\"'"), + Parser::parse_string(&mut "\"smth".chars().peekable()) + ); + Ok(()) +} + +#[test] +fn parse_token() -> Result<(), Box> { + let mut input = "*some:token}not token".chars().peekable(); + assert_eq!("*some:token".to_owned(), Parser::parse_token(&mut input)?); + assert_eq!(input.collect::(), "}not token"); + + assert_eq!( + "token".to_owned(), + Parser::parse_token(&mut "token".chars().peekable())? + ); + assert_eq!( + "a_b-c.d3:f%00/*".to_owned(), + Parser::parse_token(&mut "a_b-c.d3:f%00/*".chars().peekable())? + ); + assert_eq!( + "TestToken".to_owned(), + Parser::parse_token(&mut "TestToken".chars().peekable())? + ); + assert_eq!( + "some".to_owned(), + Parser::parse_token(&mut "some@token".chars().peekable())? + ); + assert_eq!( + "*TestToken*".to_owned(), + Parser::parse_token(&mut "*TestToken*".chars().peekable())? + ); + assert_eq!( + "*".to_owned(), + Parser::parse_token(&mut "*[@:token".chars().peekable())? + ); + assert_eq!( + "test".to_owned(), + Parser::parse_token(&mut "test token".chars().peekable())? + ); + + Ok(()) +} + +#[test] +fn parse_token_errors() -> Result<(), Box> { + let mut input = "765token".chars().peekable(); + assert_eq!( + Err("parse_token: first character is not ALPHA or '*'"), + Parser::parse_token(&mut input) + ); + assert_eq!(input.collect::(), "765token"); + + assert_eq!( + Err("parse_token: first character is not ALPHA or '*'"), + Parser::parse_token(&mut "7token".chars().peekable()) + ); + assert_eq!( + Err("parse_token: empty input string"), + Parser::parse_token(&mut "".chars().peekable()) + ); + Ok(()) +} + +#[test] +fn parse_byte_sequence() -> Result<(), Box> { + let mut input = ":aGVsbG8:rest_of_str".chars().peekable(); + assert_eq!( + "hello".to_owned().into_bytes(), + Parser::parse_byte_sequence(&mut input)? + ); + assert_eq!("rest_of_str", input.collect::()); + + assert_eq!( + "hello".to_owned().into_bytes(), + Parser::parse_byte_sequence(&mut ":aGVsbG8:".chars().peekable())? + ); + assert_eq!( + "test_encode".to_owned().into_bytes(), + Parser::parse_byte_sequence(&mut ":dGVzdF9lbmNvZGU:".chars().peekable())? + ); + assert_eq!( + "new:year tree".to_owned().into_bytes(), + Parser::parse_byte_sequence(&mut ":bmV3OnllYXIgdHJlZQ==:".chars().peekable())? + ); + assert_eq!( + "".to_owned().into_bytes(), + Parser::parse_byte_sequence(&mut "::".chars().peekable())? + ); + Ok(()) +} + +#[test] +fn parse_byte_sequence_errors() -> Result<(), Box> { + assert_eq!( + Err("parse_byte_seq: first char is not ':'"), + Parser::parse_byte_sequence(&mut "aGVsbG8".chars().peekable()) + ); + assert_eq!( + Err("parse_byte_seq: invalid char in byte sequence"), + Parser::parse_byte_sequence(&mut ":aGVsb G8=:".chars().peekable()) + ); + assert_eq!( + Err("parse_byte_seq: no closing ':'"), + Parser::parse_byte_sequence(&mut ":aGVsbG8=".chars().peekable()) + ); + Ok(()) +} + +#[test] +fn parse_number_int() -> Result<(), Box> { + let mut input = "-733333333332d.14".chars().peekable(); + assert_eq!( + Num::Integer(-733333333332), + Parser::parse_number(&mut input)? + ); + assert_eq!("d.14", input.collect::()); + + assert_eq!( + Num::Integer(42), + Parser::parse_number(&mut "42".chars().peekable())? + ); + assert_eq!( + Num::Integer(-42), + Parser::parse_number(&mut "-42".chars().peekable())? + ); + assert_eq!( + Num::Integer(-42), + Parser::parse_number(&mut "-042".chars().peekable())? + ); + assert_eq!( + Num::Integer(0), + Parser::parse_number(&mut "0".chars().peekable())? + ); + assert_eq!( + Num::Integer(0), + Parser::parse_number(&mut "00".chars().peekable())? + ); + assert_eq!( + Num::Integer(123456789012345), + Parser::parse_number(&mut "123456789012345".chars().peekable())? + ); + assert_eq!( + Num::Integer(-123456789012345), + Parser::parse_number(&mut "-123456789012345".chars().peekable())? + ); + assert_eq!( + Num::Integer(2), + Parser::parse_number(&mut "2,3".chars().peekable())? + ); + assert_eq!( + Num::Integer(4), + Parser::parse_number(&mut "4-2".chars().peekable())? + ); + assert_eq!( + Num::Integer(-999999999999999), + Parser::parse_number(&mut "-999999999999999".chars().peekable())? + ); + assert_eq!( + Num::Integer(999999999999999), + Parser::parse_number(&mut "999999999999999".chars().peekable())? + ); + + Ok(()) +} + +#[test] +fn parse_number_decimal() -> Result<(), Box> { + let mut input = "00.42 test string".chars().peekable(); + assert_eq!( + Num::Decimal(Decimal::from_str("0.42")?), + Parser::parse_number(&mut input)? + ); + assert_eq!(" test string", input.collect::()); + + assert_eq!( + Num::Decimal(Decimal::from_str("1.5")?), + Parser::parse_number(&mut "1.5.4.".chars().peekable())? + ); + assert_eq!( + Num::Decimal(Decimal::from_str("1.8")?), + Parser::parse_number(&mut "1.8.".chars().peekable())? + ); + assert_eq!( + Num::Decimal(Decimal::from_str("1.7")?), + Parser::parse_number(&mut "1.7.0".chars().peekable())? + ); + assert_eq!( + Num::Decimal(Decimal::from_str("3.14")?), + Parser::parse_number(&mut "3.14".chars().peekable())? + ); + assert_eq!( + Num::Decimal(Decimal::from_str("-3.14")?), + Parser::parse_number(&mut "-3.14".chars().peekable())? + ); + assert_eq!( + Num::Decimal(Decimal::from_str("123456789012.1")?), + Parser::parse_number(&mut "123456789012.1".chars().peekable())? + ); + assert_eq!( + Num::Decimal(Decimal::from_str("1234567890.112")?), + Parser::parse_number(&mut "1234567890.112".chars().peekable())? + ); + + Ok(()) +} + +#[test] +fn parse_number_errors() -> Result<(), Box> { + let mut input = ":aGVsbG8:rest".chars().peekable(); + assert_eq!( + Err("parse_number: input number does not start with a digit"), + Parser::parse_number(&mut input) + ); + assert_eq!(":aGVsbG8:rest", input.collect::()); + + let mut input = "-11.5555 test string".chars().peekable(); + assert_eq!( + Err("parse_number: invalid decimal fraction length"), + Parser::parse_number(&mut input) + ); + assert_eq!(" test string", input.collect::()); + + assert_eq!( + Err("parse_number: input number does not start with a digit"), + Parser::parse_number(&mut "--0".chars().peekable()) + ); + assert_eq!( + Err("parse_number: decimal too long, illegal position for decimal point"), + Parser::parse_number(&mut "1999999999999.1".chars().peekable()) + ); + assert_eq!( + Err("parse_number: decimal ends with '.'"), + Parser::parse_number(&mut "19888899999.".chars().peekable()) + ); + assert_eq!( + Err("parse_number: integer too long, length > 15"), + Parser::parse_number(&mut "1999999999999999".chars().peekable()) + ); + assert_eq!( + Err("parse_number: decimal too long, length > 16"), + Parser::parse_number(&mut "19999999999.99991".chars().peekable()) + ); + assert_eq!( + Err("parse_number: input number does not start with a digit"), + Parser::parse_number(&mut "- 42".chars().peekable()) + ); + assert_eq!( + Err("parse_number: input number does not start with a digit"), + Parser::parse_number(&mut "- 42".chars().peekable()) + ); + assert_eq!( + Err("parse_number: decimal ends with '.'"), + Parser::parse_number(&mut "1..4".chars().peekable()) + ); + assert_eq!( + Err("parse_number: input number lacks a digit"), + Parser::parse_number(&mut "-".chars().peekable()) + ); + assert_eq!( + Err("parse_number: decimal ends with '.'"), + Parser::parse_number(&mut "-5. 14".chars().peekable()) + ); + assert_eq!( + Err("parse_number: decimal ends with '.'"), + Parser::parse_number(&mut "7. 1".chars().peekable()) + ); + assert_eq!( + Err("parse_number: invalid decimal fraction length"), + Parser::parse_number(&mut "-7.3333333333".chars().peekable()) + ); + assert_eq!( + Err("parse_number: decimal too long, illegal position for decimal point"), + Parser::parse_number(&mut "-7333333333323.12".chars().peekable()) + ); + + Ok(()) +} + +#[test] +fn parse_params_string() -> Result<(), Box> { + let mut input = ";b=\"param_val\"".chars().peekable(); + let expected = Parameters::from_iter(vec![( + "b".to_owned(), + BareItem::String("param_val".to_owned()), + )]); + assert_eq!(expected, Parser::parse_parameters(&mut input)?); + Ok(()) +} + +#[test] +fn parse_params_bool() -> Result<(), Box> { + let mut input = ";b;a".chars().peekable(); + let expected = Parameters::from_iter(vec![ + ("b".to_owned(), BareItem::Boolean(true)), + ("a".to_owned(), BareItem::Boolean(true)), + ]); + assert_eq!(expected, Parser::parse_parameters(&mut input)?); + Ok(()) +} + +#[test] +fn parse_params_mixed_types() -> Result<(), Box> { + let mut input = ";key1=?0;key2=746.15".chars().peekable(); + let expected = Parameters::from_iter(vec![ + ("key1".to_owned(), BareItem::Boolean(false)), + ("key2".to_owned(), Decimal::from_str("746.15")?.into()), + ]); + assert_eq!(expected, Parser::parse_parameters(&mut input)?); + Ok(()) +} + +#[test] +fn parse_params_with_spaces() -> Result<(), Box> { + let mut input = "; key1=?0; key2=11111".chars().peekable(); + let expected = Parameters::from_iter(vec![ + ("key1".to_owned(), BareItem::Boolean(false)), + ("key2".to_owned(), 11111.into()), + ]); + assert_eq!(expected, Parser::parse_parameters(&mut input)?); + Ok(()) +} + +#[test] +fn parse_params_empty() -> Result<(), Box> { + assert_eq!( + Parameters::new(), + Parser::parse_parameters(&mut " key1=?0; key2=11111".chars().peekable())? + ); + assert_eq!( + Parameters::new(), + Parser::parse_parameters(&mut "".chars().peekable())? + ); + assert_eq!( + Parameters::new(), + Parser::parse_parameters(&mut "[;a=1".chars().peekable())? + ); + assert_eq!( + Parameters::new(), + Parser::parse_parameters(&mut String::new().chars().peekable())? + ); + Ok(()) +} + +#[test] +fn parse_key() -> Result<(), Box> { + assert_eq!( + "a".to_owned(), + Parser::parse_key(&mut "a=1".chars().peekable())? + ); + assert_eq!( + "a1".to_owned(), + Parser::parse_key(&mut "a1=10".chars().peekable())? + ); + assert_eq!( + "*1".to_owned(), + Parser::parse_key(&mut "*1=10".chars().peekable())? + ); + assert_eq!( + "f".to_owned(), + Parser::parse_key(&mut "f[f=10".chars().peekable())? + ); + Ok(()) +} + +#[test] +fn parse_key_errors() -> Result<(), Box> { + assert_eq!( + Err("parse_key: first character is not lcalpha or '*'"), + Parser::parse_key(&mut "[*f=10".chars().peekable()) + ); + Ok(()) +} + +#[test] +fn parse_more_list() -> Result<(), Box> { + let item1 = Item::new(1.into()); + let item2 = Item::new(2.into()); + let item3 = Item::new(42.into()); + let inner_list_1 = InnerList::new(vec![item1, item2]); + let expected_list: List = vec![inner_list_1.into(), item3.into()]; + + let mut parsed_header = Parser::parse_list("(1 2)".as_bytes())?; + let _ = parsed_header.parse_more("42".as_bytes())?; + assert_eq!(expected_list, parsed_header); + Ok(()) +} + +#[test] +fn parse_more_dict() -> Result<(), Box> { + let item2_params = + Parameters::from_iter(vec![("foo".to_owned(), BareItem::Token("*".to_owned()))]); + let item1 = Item::new(1.into()); + let item2 = Item::with_params(BareItem::Boolean(true), item2_params); + let item3 = Item::new(3.into()); + let expected_dict = Dictionary::from_iter(vec![ + ("a".to_owned(), item1.into()), + ("b".to_owned(), item2.into()), + ("c".to_owned(), item3.into()), + ]); + + let mut parsed_header = Parser::parse_dictionary("a=1, b;foo=*\t\t".as_bytes())?; + let _ = parsed_header.parse_more(" c=3".as_bytes())?; + assert_eq!(expected_dict, parsed_header); + Ok(()) +} + +#[test] +fn parse_more_errors() -> Result<(), Box> { + let parsed_dict_header = + Parser::parse_dictionary("a=1, b;foo=*".as_bytes())?.parse_more(",a".as_bytes()); + assert!(parsed_dict_header.is_err()); + + let parsed_list_header = + Parser::parse_list("a, b;foo=*".as_bytes())?.parse_more("(a, 2)".as_bytes()); + assert!(parsed_list_header.is_err()); + Ok(()) +} diff --git a/third_party/rust/sfv/src/test_serializer.rs b/third_party/rust/sfv/src/test_serializer.rs new file mode 100644 index 000000000000..edcc79ce2a50 --- /dev/null +++ b/third_party/rust/sfv/src/test_serializer.rs @@ -0,0 +1,531 @@ +use crate::serializer::Serializer; +use crate::FromStr; +use crate::SerializeValue; +use crate::{BareItem, Decimal, Dictionary, InnerList, Item, List, Parameters}; +use std::error::Error; +use std::iter::FromIterator; + +#[test] +fn serialize_value_empty_dict() -> Result<(), Box> { + let dict_field_value = Dictionary::new(); + assert_eq!( + Err("serialize_dictionary: serializing empty field is not allowed"), + dict_field_value.serialize_value() + ); + Ok(()) +} + +#[test] +fn serialize_value_empty_list() -> Result<(), Box> { + let list_field_value = List::new(); + assert_eq!( + Err("serialize_list: serializing empty field is not allowed"), + list_field_value.serialize_value() + ); + Ok(()) +} + +#[test] +fn serialize_value_list_mixed_members_with_params() -> Result<(), Box> { + let item1 = Item::new(Decimal::from_str("42.4568")?.into()); + let item2_param = Parameters::from_iter(vec![("itm2_p".to_owned(), BareItem::Boolean(true))]); + let item2 = Item::with_params(17.into(), item2_param); + + let inner_list_item1_param = + Parameters::from_iter(vec![("in1_p".to_owned(), BareItem::Boolean(false))]); + let inner_list_item1 = + Item::with_params(BareItem::String("str1".to_owned()), inner_list_item1_param); + let inner_list_item2_param = Parameters::from_iter(vec![( + "in2_p".to_owned(), + BareItem::String("valu\\e".to_owned()), + )]); + let inner_list_item2 = + Item::with_params(BareItem::Token("str2".to_owned()), inner_list_item2_param); + let inner_list_param = Parameters::from_iter(vec![( + "inner_list_param".to_owned(), + BareItem::ByteSeq("weather".as_bytes().to_vec()), + )]); + let inner_list = + InnerList::with_params(vec![inner_list_item1, inner_list_item2], inner_list_param); + + let list_field_value: List = vec![item1.into(), item2.into(), inner_list.into()]; + let expected = "42.457, 17;itm2_p, (\"str1\";in1_p=?0 str2;in2_p=\"valu\\\\e\");inner_list_param=:d2VhdGhlcg==:"; + assert_eq!(expected, list_field_value.serialize_value()?); + Ok(()) +} + +#[test] +fn serialize_value_errors() -> Result<(), Box> { + let disallowed_item = Item::new(BareItem::String("non-ascii text 🐹".into())); + assert_eq!( + Err("serialize_string: non-ascii character"), + disallowed_item.serialize_value() + ); + + let disallowed_item = Item::new(Decimal::from_str("12345678912345.123")?.into()); + assert_eq!( + Err("serialize_decimal: integer component > 12 digits"), + disallowed_item.serialize_value() + ); + + let param_with_disallowed_key = Parameters::from_iter(vec![("_key".to_owned(), 13.into())]); + let disallowed_item = Item::with_params(12.into(), param_with_disallowed_key); + assert_eq!( + Err("serialize_key: first character is not lcalpha or '*'"), + disallowed_item.serialize_value() + ); + Ok(()) +} + +#[test] +fn serialize_item_byteseq_with_param() -> Result<(), Box> { + let mut buf = String::new(); + + let item_param = ("a".to_owned(), BareItem::Token("*ab_1".into())); + let item_param = Parameters::from_iter(vec![item_param]); + let item = Item::with_params(BareItem::ByteSeq("parser".as_bytes().to_vec()), item_param); + Serializer::serialize_item(&item, &mut buf)?; + assert_eq!(":cGFyc2Vy:;a=*ab_1", &buf); + Ok(()) +} + +#[test] +fn serialize_item_without_params() -> Result<(), Box> { + let mut buf = String::new(); + let item = Item::new(1.into()); + Serializer::serialize_item(&item, &mut buf)?; + assert_eq!("1", &buf); + Ok(()) +} + +#[test] +fn serialize_item_with_bool_true_param() -> Result<(), Box> { + let mut buf = String::new(); + let param = Parameters::from_iter(vec![("a".to_owned(), BareItem::Boolean(true))]); + let item = Item::with_params(Decimal::from_str("12.35")?.into(), param); + Serializer::serialize_item(&item, &mut buf)?; + assert_eq!("12.35;a", &buf); + Ok(()) +} + +#[test] +fn serialize_item_with_token_param() -> Result<(), Box> { + let mut buf = String::new(); + let param = Parameters::from_iter(vec![("a1".to_owned(), BareItem::Token("*tok".to_owned()))]); + let item = Item::with_params(BareItem::String("12.35".to_owned()), param); + Serializer::serialize_item(&item, &mut buf)?; + assert_eq!("\"12.35\";a1=*tok", &buf); + Ok(()) +} + +#[test] +fn serialize_integer() -> Result<(), Box> { + let mut buf = String::new(); + Serializer::serialize_integer(-12, &mut buf)?; + assert_eq!("-12", &buf); + + buf.clear(); + Serializer::serialize_integer(0, &mut buf)?; + assert_eq!("0", &buf); + + buf.clear(); + Serializer::serialize_integer(999_999_999_999_999, &mut buf)?; + assert_eq!("999999999999999", &buf); + + buf.clear(); + Serializer::serialize_integer(-999_999_999_999_999, &mut buf)?; + assert_eq!("-999999999999999", &buf); + Ok(()) +} + +#[test] +fn serialize_integer_errors() -> Result<(), Box> { + let mut buf = String::new(); + assert_eq!( + Err("serialize_integer: integer is out of range"), + Serializer::serialize_integer(1_000_000_000_000_000, &mut buf) + ); + + buf.clear(); + assert_eq!( + Err("serialize_integer: integer is out of range"), + Serializer::serialize_integer(-1_000_000_000_000_000, &mut buf) + ); + Ok(()) +} + +#[test] +fn serialize_decimal() -> Result<(), Box> { + let mut buf = String::new(); + Serializer::serialize_decimal(Decimal::from_str("-99.1346897")?, &mut buf)?; + assert_eq!("-99.135", &buf); + + buf.clear(); + Serializer::serialize_decimal(Decimal::from_str("-1.00")?, &mut buf)?; + assert_eq!("-1.0", &buf); + + buf.clear(); + Serializer::serialize_decimal( + Decimal::from_str("-00000000000000000000000099.1346897")?, + &mut buf, + )?; + assert_eq!("-99.135", &buf); + + buf.clear(); + Serializer::serialize_decimal(Decimal::from_str("100.13")?, &mut buf)?; + assert_eq!("100.13", &buf); + + buf.clear(); + Serializer::serialize_decimal(Decimal::from_str("-100.130")?, &mut buf)?; + assert_eq!("-100.130", &buf); + + buf.clear(); + Serializer::serialize_decimal(Decimal::from_str("-137.0")?, &mut buf)?; + assert_eq!("-137.0", &buf); + + buf.clear(); + Serializer::serialize_decimal(Decimal::from_str("137121212112.123")?, &mut buf)?; + assert_eq!("137121212112.123", &buf); + + buf.clear(); + Serializer::serialize_decimal(Decimal::from_str("137121212112.1238")?, &mut buf)?; + assert_eq!("137121212112.124", &buf); + Ok(()) +} + +#[test] +fn serialize_decimal_errors() -> Result<(), Box> { + let mut buf = String::new(); + assert_eq!( + Err("serialize_decimal: integer component > 12 digits"), + Serializer::serialize_decimal(Decimal::from_str("1371212121121.1")?, &mut buf) + ); + Ok(()) +} + +#[test] +fn serialize_string() -> Result<(), Box> { + let mut buf = String::new(); + Serializer::serialize_string("1.1 text", &mut buf)?; + assert_eq!("\"1.1 text\"", &buf); + + buf.clear(); + Serializer::serialize_string("hello \"name\"", &mut buf)?; + assert_eq!("\"hello \\\"name\\\"\"", &buf); + + buf.clear(); + Serializer::serialize_string("something\\nothing", &mut buf)?; + assert_eq!("\"something\\\\nothing\"", &buf); + + buf.clear(); + Serializer::serialize_string("", &mut buf)?; + assert_eq!("\"\"", &buf); + + buf.clear(); + Serializer::serialize_string(" ", &mut buf)?; + assert_eq!("\" \"", &buf); + + buf.clear(); + Serializer::serialize_string(" ", &mut buf)?; + assert_eq!("\" \"", &buf); + Ok(()) +} + +#[test] +fn serialize_string_errors() -> Result<(), Box> { + let mut buf = String::new(); + + assert_eq!( + Err("serialize_string: not a visible character"), + Serializer::serialize_string("text \x00", &mut buf) + ); + + assert_eq!( + Err("serialize_string: not a visible character"), + Serializer::serialize_string("text \x1f", &mut buf) + ); + assert_eq!( + Err("serialize_string: not a visible character"), + Serializer::serialize_string("text \x7f", &mut buf) + ); + assert_eq!( + Err("serialize_string: non-ascii character"), + Serializer::serialize_string("рядок", &mut buf) + ); + Ok(()) +} + +#[test] +fn serialize_token() -> Result<(), Box> { + let mut buf = String::new(); + Serializer::serialize_token("*", &mut buf)?; + assert_eq!("*", &buf); + + buf.clear(); + Serializer::serialize_token("abc", &mut buf)?; + assert_eq!("abc", &buf); + + buf.clear(); + Serializer::serialize_token("abc:de", &mut buf)?; + assert_eq!("abc:de", &buf); + + buf.clear(); + Serializer::serialize_token("smth/#!else", &mut buf)?; + assert_eq!("smth/#!else", &buf); + Ok(()) +} + +#[test] +fn serialize_token_errors() -> Result<(), Box> { + let mut buf = String::new(); + + assert_eq!( + Err("serialise_token: first character is not ALPHA or '*'"), + Serializer::serialize_token("#some", &mut buf) + ); + assert_eq!( + Err("serialise_token: disallowed character"), + Serializer::serialize_token("s ", &mut buf) + ); + assert_eq!( + Err("serialise_token: disallowed character"), + Serializer::serialize_token("abc:de\t", &mut buf) + ); + Ok(()) +} + +#[test] +fn serialize_byte_sequence() -> Result<(), Box> { + let mut buf = String::new(); + Serializer::serialize_byte_sequence("hello".as_bytes(), &mut buf)?; + assert_eq!(":aGVsbG8=:", &buf); + + buf.clear(); + Serializer::serialize_byte_sequence("test_encode".as_bytes(), &mut buf)?; + assert_eq!(":dGVzdF9lbmNvZGU=:", &buf); + + buf.clear(); + Serializer::serialize_byte_sequence("".as_bytes(), &mut buf)?; + assert_eq!("::", &buf); + + buf.clear(); + Serializer::serialize_byte_sequence("pleasure.".as_bytes(), &mut buf)?; + assert_eq!(":cGxlYXN1cmUu:", &buf); + + buf.clear(); + Serializer::serialize_byte_sequence("leasure.".as_bytes(), &mut buf)?; + assert_eq!(":bGVhc3VyZS4=:", &buf); + + buf.clear(); + Serializer::serialize_byte_sequence("easure.".as_bytes(), &mut buf)?; + assert_eq!(":ZWFzdXJlLg==:", &buf); + + buf.clear(); + Serializer::serialize_byte_sequence("asure.".as_bytes(), &mut buf)?; + assert_eq!(":YXN1cmUu:", &buf); + + buf.clear(); + Serializer::serialize_byte_sequence("sure.".as_bytes(), &mut buf)?; + assert_eq!(":c3VyZS4=:", &buf); + + Ok(()) +} + +#[test] +fn serialize_bool() -> Result<(), Box> { + let mut buf = String::new(); + Serializer::serialize_bool(true, &mut buf)?; + assert_eq!("?1", &buf); + + buf.clear(); + Serializer::serialize_bool(false, &mut buf)?; + assert_eq!("?0", &buf); + Ok(()) +} + +#[test] +fn serialize_params_bool() -> Result<(), Box> { + let mut buf = String::new(); + + let input = Parameters::from_iter(vec![ + ("*b".to_owned(), BareItem::Boolean(true)), + ("a.a".to_owned(), BareItem::Boolean(true)), + ]); + + Serializer::serialize_parameters(&input, &mut buf)?; + assert_eq!(";*b;a.a", &buf); + Ok(()) +} + +#[test] +fn serialize_params_string() -> Result<(), Box> { + let mut buf = String::new(); + + let input = Parameters::from_iter(vec![( + "b".to_owned(), + BareItem::String("param_val".to_owned()), + )]); + Serializer::serialize_parameters(&input, &mut buf)?; + assert_eq!(";b=\"param_val\"", &buf); + Ok(()) +} + +#[test] +fn serialize_params_numbers() -> Result<(), Box> { + let mut buf = String::new(); + + let input = Parameters::from_iter(vec![ + ("key1".to_owned(), Decimal::from_str("746.15")?.into()), + ("key2".to_owned(), 11111.into()), + ]); + Serializer::serialize_parameters(&input, &mut buf)?; + assert_eq!(";key1=746.15;key2=11111", &buf); + Ok(()) +} + +#[test] +fn serialize_params_mixed_types() -> Result<(), Box> { + let mut buf = String::new(); + + let input = Parameters::from_iter(vec![ + ("key1".to_owned(), BareItem::Boolean(false)), + ("key2".to_owned(), Decimal::from_str("1354.091878")?.into()), + ]); + Serializer::serialize_parameters(&input, &mut buf)?; + assert_eq!(";key1=?0;key2=1354.092", &buf); + Ok(()) +} + +#[test] +fn serialize_key() -> Result<(), Box> { + let mut buf = String::new(); + Serializer::serialize_key("*a_fg", &mut buf)?; + assert_eq!("*a_fg", &buf); + + buf.clear(); + Serializer::serialize_key("*a_fg*", &mut buf)?; + assert_eq!("*a_fg*", &buf); + + buf.clear(); + Serializer::serialize_key("key1", &mut buf)?; + assert_eq!("key1", &buf); + + buf.clear(); + Serializer::serialize_key("ke-y.1", &mut buf)?; + assert_eq!("ke-y.1", &buf); + + Ok(()) +} + +#[test] +fn serialize_key_erros() -> Result<(), Box> { + let mut buf = String::new(); + + assert_eq!( + Err("serialize_key: disallowed character in input"), + Serializer::serialize_key("AND", &mut buf) + ); + assert_eq!( + Err("serialize_key: first character is not lcalpha or '*'"), + Serializer::serialize_key("_key", &mut buf) + ); + assert_eq!( + Err("serialize_key: first character is not lcalpha or '*'"), + Serializer::serialize_key("7key", &mut buf) + ); + Ok(()) +} + +#[test] +fn serialize_list_of_items_and_inner_list() -> Result<(), Box> { + let mut buf = String::new(); + + let item1 = Item::new(12.into()); + let item2 = Item::new(14.into()); + let item3 = Item::new(BareItem::Token("a".to_owned())); + let item4 = Item::new(BareItem::Token("b".to_owned())); + let inner_list_param = Parameters::from_iter(vec![( + "param".to_owned(), + BareItem::String("param_value_1".to_owned()), + )]); + let inner_list = InnerList::with_params(vec![item3, item4], inner_list_param); + let input: List = vec![item1.into(), item2.into(), inner_list.into()]; + + Serializer::serialize_list(&input, &mut buf)?; + assert_eq!("12, 14, (a b);param=\"param_value_1\"", &buf); + Ok(()) +} + +#[test] +fn serialize_list_of_lists() -> Result<(), Box> { + let mut buf = String::new(); + + let item1 = Item::new(1.into()); + let item2 = Item::new(2.into()); + let item3 = Item::new(42.into()); + let item4 = Item::new(43.into()); + let inner_list_1 = InnerList::new(vec![item1, item2]); + let inner_list_2 = InnerList::new(vec![item3, item4]); + let input: List = vec![inner_list_1.into(), inner_list_2.into()]; + + Serializer::serialize_list(&input, &mut buf)?; + assert_eq!("(1 2), (42 43)", &buf); + Ok(()) +} + +#[test] +fn serialize_list_with_bool_item_and_bool_params() -> Result<(), Box> { + let mut buf = String::new(); + + let item1_params = Parameters::from_iter(vec![ + ("a".to_owned(), BareItem::Boolean(true)), + ("b".to_owned(), BareItem::Boolean(false)), + ]); + let item1 = Item::with_params(BareItem::Boolean(false), item1_params); + let item2 = Item::new(BareItem::Token("cde_456".to_owned())); + + let input: List = vec![item1.into(), item2.into()]; + Serializer::serialize_list(&input, &mut buf)?; + assert_eq!("?0;a;b=?0, cde_456", &buf); + Ok(()) +} + +#[test] +fn serialize_dictionary_with_params() -> Result<(), Box> { + let mut buf = String::new(); + + let item1_params = Parameters::from_iter(vec![ + ("a".to_owned(), 1.into()), + ("b".to_owned(), BareItem::Boolean(true)), + ]); + let item2_params = Parameters::new(); + let item3_params = Parameters::from_iter(vec![ + ("q".to_owned(), BareItem::Boolean(false)), + ("r".to_owned(), BareItem::String("+w".to_owned())), + ]); + + let item1 = Item::with_params(123.into(), item1_params); + let item2 = Item::with_params(456.into(), item2_params); + let item3 = Item::with_params(789.into(), item3_params); + + let input = Dictionary::from_iter(vec![ + ("abc".to_owned(), item1.into()), + ("def".to_owned(), item2.into()), + ("ghi".to_owned(), item3.into()), + ]); + + Serializer::serialize_dict(&input, &mut buf)?; + assert_eq!("abc=123;a=1;b, def=456, ghi=789;q=?0;r=\"+w\"", &buf); + Ok(()) +} + +#[test] +fn serialize_dict_empty_member_value() -> Result<(), Box> { + let mut buf = String::new(); + + let inner_list = InnerList::new(vec![]); + let input = Dictionary::from_iter(vec![("a".to_owned(), inner_list.into())]); + Serializer::serialize_dict(&input, &mut buf)?; + assert_eq!("a=()", &buf); + Ok(()) +} diff --git a/third_party/rust/sfv/src/utils.rs b/third_party/rust/sfv/src/utils.rs new file mode 100644 index 000000000000..7eb79340bad7 --- /dev/null +++ b/third_party/rust/sfv/src/utils.rs @@ -0,0 +1,44 @@ +use data_encoding::{Encoding, Specification}; +use std::iter::Peekable; +use std::str::Chars; + +pub(crate) fn base64() -> Result { + let mut spec = Specification::new(); + spec.check_trailing_bits = false; + spec.symbols + .push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); + spec.padding = None; + spec.ignore = "=".to_owned(); + spec.encoding() + .map_err(|_err| "invalid base64 specification") +} + +pub(crate) fn is_tchar(c: char) -> bool { + // See tchar values list in https://tools.ietf.org/html/rfc7230#section-3.2.6 + let tchars = "!#$%&'*+-.^_`|~"; + tchars.contains(c) || c.is_ascii_alphanumeric() +} + +pub(crate) fn is_allowed_b64_content(c: char) -> bool { + c.is_ascii_alphanumeric() || c == '+' || c == '=' || c == '/' +} + +pub(crate) fn consume_ows_chars(input_chars: &mut Peekable) { + while let Some(c) = input_chars.peek() { + if c == &' ' || c == &'\t' { + input_chars.next(); + } else { + break; + } + } +} + +pub(crate) fn consume_sp_chars(input_chars: &mut Peekable) { + while let Some(c) = input_chars.peek() { + if c == &' ' { + input_chars.next(); + } else { + break; + } + } +}