Skip to main content

sql_values/postgres/
integer.rs

1//! PostgreSQL 18's integer types, `smallint`, `integer` and `bigint`.
2
3use core::ops::RangeInclusive;
4
5use super::{
6    BuiltinType,
7    cast::{CastError, CastValue},
8};
9
10/// A PostgreSQL integer type, ordered from the narrowest to the widest.
11///
12/// ```
13/// use sql_values::postgres::{CastError, BuiltinType, CastValue, IntegerType, cast_value};
14///
15/// assert_eq!(IntegerType::Smallint.range(), -32_768..=32_767);
16/// assert_eq!(IntegerType::Smallint.wider(IntegerType::Integer), IntegerType::Integer);
17/// assert_eq!(IntegerType::Integer.value(7), Ok(CastValue::Integer(7)));
18/// assert_eq!(
19///     IntegerType::Smallint.value(40_000),
20///     Err(CastError::OutOfRange(BuiltinType::Smallint)),
21/// );
22/// assert_eq!(IntegerType::try_from(BuiltinType::Bigint), Ok(IntegerType::Bigint));
23/// assert_eq!(IntegerType::try_from(BuiltinType::Text), Err(BuiltinType::Text));
24///
25/// // `value` is the cast from `bigint` to the type.
26/// for integer in [IntegerType::Smallint, IntegerType::Integer, IntegerType::Bigint] {
27///     assert_eq!(cast_value(CastValue::Bigint(7), integer.into()), integer.value(7).map(Some));
28/// }
29/// ```
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
31pub enum IntegerType {
32    /// `smallint`, 16 bits.
33    Smallint,
34    /// `integer`, 32 bits.
35    Integer,
36    /// `bigint`, 64 bits.
37    Bigint,
38}
39
40impl IntegerType {
41    /// The values the type holds.
42    #[must_use]
43    pub fn range(self) -> RangeInclusive<i64> {
44        match self {
45            Self::Smallint => i64::from(i16::MIN)..=i64::from(i16::MAX),
46            Self::Integer => i64::from(i32::MIN)..=i64::from(i32::MAX),
47            Self::Bigint => i64::MIN..=i64::MAX,
48        }
49    }
50
51    /// The type of an arithmetic operator's result over the two types, the wider of them, as
52    /// `int24pl` returns an `integer` and `int48mul` a `bigint`.
53    #[must_use]
54    pub fn wider(self, other: Self) -> Self {
55        self.max(other)
56    }
57
58    /// The value held as the type, or the error a cast to the type raises for a value past its
59    /// range.
60    ///
61    /// # Errors
62    ///
63    /// [`CastError::OutOfRange`] for a value outside [`IntegerType::range`].
64    pub fn value(self, value: i64) -> Result<CastValue, CastError> {
65        let out_of_range = |_| CastError::OutOfRange(self.into());
66        match self {
67            Self::Smallint => i16::try_from(value)
68                .map(CastValue::Smallint)
69                .map_err(out_of_range),
70            Self::Integer => i32::try_from(value)
71                .map(CastValue::Integer)
72                .map_err(out_of_range),
73            Self::Bigint => Ok(CastValue::Bigint(value)),
74        }
75    }
76}
77
78impl From<IntegerType> for BuiltinType {
79    fn from(integer: IntegerType) -> Self {
80        match integer {
81            IntegerType::Smallint => Self::Smallint,
82            IntegerType::Integer => Self::Integer,
83            IntegerType::Bigint => Self::Bigint,
84        }
85    }
86}
87
88/// Refuses a type that is not an integer type, returning it.
89impl TryFrom<BuiltinType> for IntegerType {
90    type Error = BuiltinType;
91
92    fn try_from(pg_type: BuiltinType) -> Result<Self, Self::Error> {
93        match pg_type {
94            BuiltinType::Smallint => Ok(Self::Smallint),
95            BuiltinType::Integer => Ok(Self::Integer),
96            BuiltinType::Bigint => Ok(Self::Bigint),
97            other => Err(other),
98        }
99    }
100}