sqlx_pg_uint/
lib.rs

1#![warn(
2    missing_docs,
3    missing_debug_implementations,
4    rust_2018_idioms,
5    missing_copy_implementations
6)]
7#![deny(unsafe_code)]
8
9/*!
10# sqlx-pg-uint
11
12`SQLx` extension to support working with Rust unsigned integers in PostgreSQL.
13
14---
15
16This crate provides types with `sqlx::{Encode, Decode, Type}` implemented for them, which allow you
17to work with fixed-size unsigned integers in PostgreSQL.
18
19```
20use sqlx_pg_uint::PgU64;
21
22let a_u64_number = 2937854645u64;
23let pg_u_64 = PgU64::from(a_u64_number);
24println!("PgU64: {}", pg_u_64);
25let back_to_u64: u64 = pg_u_64.to_uint();
26println!("Back to u64: {}", back_to_u64);
27println!(
28    "Maths work the same way as you'd expect: {}",
29    PgU64::from(67) + PgU64::from(2) * PgU64::from(3) / PgU64::from(3)
30);
31println!(
32    "Interact with the underlying BigDecimal type directly: {}",
33    pg_u_64.as_big_decimal()
34);
35println!("PgUint types can be converted to and from BigDecimals, and are storable in an sqlx::Postgres database.");
36println!("If you load a PgUint from a database successfully, you can be sure that it's a valid fixed-size unsigned integer.");
37```
38*/
39
40mod u128;
41mod u16;
42mod u32;
43mod u64;
44mod u8;
45
46use std::fmt::Display;
47use std::str::FromStr;
48
49pub(crate) use bigdecimal::BigDecimal;
50use thiserror::Error;
51pub use u128::*;
52pub use u16::*;
53pub use u32::*;
54pub use u64::*;
55pub use u8::*;
56
57#[derive(Debug, PartialEq, Clone, Error)]
58/// Error type for conversions between `BigDecimal` and `PgUint` types.
59pub enum Error {
60    #[error("Value is either too large, to small or not an integer")]
61    /// Error when the value is either too large, too small or not an integer.
62    InvalidValue(BigDecimal),
63    #[error("Invalid value for target type")]
64    /// Provided value is a floating point number, which is not supported by the target type.
65    Fractional(BigDecimal),
66    #[cfg(feature = "serde")]
67    #[error(transparent)]
68    /// Error when deserializing a `BigDecimal` from a `serde` deserializer.
69    Serde(#[from] serde::de::value::Error),
70    #[error(transparent)]
71    /// Error when parsing a `BigDecimal` from a string.
72    ParseInt(#[from] std::num::ParseIntError),
73}
74
75mod private {
76    pub trait Sealed {}
77}
78
79impl private::Sealed for u8 {}
80impl private::Sealed for u16 {}
81impl private::Sealed for u32 {}
82impl private::Sealed for u64 {}
83impl private::Sealed for u128 {}
84
85/// Helper trait to define the underlying integer type for a given `PgUint` type. Used in the
86/// `sqlx-pg-uint-macros` crate to generate the necessary code for the `UIntWrapper` derive.
87///
88/// Not intended to be implemented by users, nor is it required to be used directly.
89pub trait UIntType: private::Sealed + Display {
90    /// The underlying integer type for the `PgUint` type.
91    type Uint: private::Sealed + FromStr;
92}
93
94/// Allows for converting an `Option<PgUInt>` to an `Option<[underlying integer type]>`
95pub trait OptionPgUint<T: UIntType> {
96    /// Convert any `Option<PgUint>` to an `Option<[underlying integer type]>`
97    fn to_option_uint(&self) -> Option<T::Uint>;
98}