Skip to main content

drizzle_postgres/values/
owned.rs

1//! [`OwnedPostgresValue`], a `PostgresValue` that owns its data.
2
3use super::PostgresValue;
4use crate::prelude::*;
5use crate::traits::{FromPostgresValue, PostgresEnum};
6use drizzle_core::{SQLParam, error::DrizzleError, sql::SQL};
7#[cfg(feature = "uuid")]
8use uuid::Uuid;
9
10#[cfg(feature = "chrono")]
11use chrono::{DateTime, Duration, FixedOffset, NaiveDate, NaiveDateTime, NaiveTime};
12
13#[cfg(feature = "time")]
14use time::{
15    Date as TimeDate, Duration as TimeDuration, OffsetDateTime, PrimitiveDateTime, Time as TimeTime,
16};
17
18#[cfg(feature = "jiff")]
19use jiff::{
20    Timestamp as JiffTimestamp,
21    civil::{Date as JiffDate, DateTime as JiffDateTime, Time as JiffTime},
22};
23
24#[cfg(feature = "cidr")]
25use cidr::{IpCidr, IpInet};
26
27#[cfg(feature = "geo-types")]
28use geo_types::{LineString, Point, Rect};
29
30#[cfg(feature = "bit-vec")]
31use bit_vec::BitVec;
32
33#[cfg(feature = "rust-decimal")]
34use rust_decimal::Decimal;
35
36/// A [`PostgresValue`](super::PostgresValue) that owns its data, so it has no lifetime.
37///
38/// Use it to store values beyond the query that produced them, for example
39/// in an owned prepared statement. Convert with `From` in either direction.
40///
41/// # Examples
42///
43/// ```
44/// use drizzle_postgres::values::{OwnedPostgresValue, PostgresValue};
45///
46/// let owned = OwnedPostgresValue::from(PostgresValue::from("hello"));
47/// assert_eq!(owned.as_str(), Some("hello"));
48/// assert_eq!(owned.as_value().as_str(), Some("hello"));
49/// ```
50#[derive(Debug, Clone, PartialEq, Default)]
51pub enum OwnedPostgresValue {
52    /// SMALLINT values (16-bit signed integer)
53    Smallint(i16),
54    /// INTEGER values (32-bit signed integer)
55    Integer(i32),
56    /// BIGINT values (64-bit signed integer)
57    Bigint(i64),
58    /// REAL values (32-bit floating point)
59    Real(f32),
60    /// DOUBLE PRECISION values (64-bit floating point)
61    DoublePrecision(f64),
62    /// NUMERIC/DECIMAL values
63    #[cfg(feature = "rust-decimal")]
64    Numeric(Decimal),
65    /// TEXT, VARCHAR, CHAR values (owned)
66    Text(String),
67    /// BYTEA values (owned binary data)
68    Bytea(Vec<u8>),
69    /// BOOLEAN values
70    Boolean(bool),
71    /// UUID values
72    #[cfg(feature = "uuid")]
73    Uuid(Uuid),
74    /// JSON values (stored as text in `PostgreSQL`)
75    #[cfg(feature = "serde")]
76    Json(serde_json::Value),
77    /// JSONB values (stored as binary in `PostgreSQL`)
78    #[cfg(feature = "serde")]
79    Jsonb(serde_json::Value),
80
81    // Date and time types
82    /// DATE values
83    #[cfg(feature = "chrono")]
84    Date(NaiveDate),
85    /// TIME values
86    #[cfg(feature = "chrono")]
87    Time(NaiveTime),
88    /// TIMESTAMP values (without timezone)
89    #[cfg(feature = "chrono")]
90    Timestamp(NaiveDateTime),
91    /// TIMESTAMPTZ values (with timezone)
92    #[cfg(feature = "chrono")]
93    TimestampTz(DateTime<FixedOffset>),
94    /// INTERVAL values
95    #[cfg(feature = "chrono")]
96    Interval(Duration),
97
98    // Date and time types (time crate)
99    /// DATE values (time crate)
100    #[cfg(feature = "time")]
101    TimeDate(TimeDate),
102    /// TIME values (time crate)
103    #[cfg(feature = "time")]
104    TimeTime(TimeTime),
105    /// TIMESTAMP values without timezone (time crate)
106    #[cfg(feature = "time")]
107    TimeTimestamp(PrimitiveDateTime),
108    /// TIMESTAMPTZ values with timezone (time crate)
109    #[cfg(feature = "time")]
110    TimeTimestampTz(OffsetDateTime),
111    /// INTERVAL values (time crate)
112    #[cfg(feature = "time")]
113    TimeInterval(TimeDuration),
114    /// DATE values (`jiff::civil::Date`)
115    #[cfg(feature = "jiff")]
116    JiffDate(JiffDate),
117    /// TIME values (`jiff::civil::Time`)
118    #[cfg(feature = "jiff")]
119    JiffTime(JiffTime),
120    /// TIMESTAMP values without timezone (`jiff::civil::DateTime`)
121    #[cfg(feature = "jiff")]
122    JiffDateTime(JiffDateTime),
123    /// TIMESTAMPTZ values (`jiff::Timestamp`, an instant)
124    #[cfg(feature = "jiff")]
125    JiffTimestamp(JiffTimestamp),
126
127    // Network address types
128    /// INET values (host address with optional netmask)
129    #[cfg(feature = "cidr")]
130    Inet(IpInet),
131    /// CIDR values (network specification)
132    #[cfg(feature = "cidr")]
133    Cidr(IpCidr),
134    /// MACADDR values (MAC addresses)
135    #[cfg(feature = "cidr")]
136    MacAddr([u8; 6]),
137    /// MACADDR8 values (EUI-64 MAC addresses)
138    #[cfg(feature = "cidr")]
139    MacAddr8([u8; 8]),
140
141    // Geometric types (native PostgreSQL support via postgres-rs)
142    /// POINT values
143    #[cfg(feature = "geo-types")]
144    Point(Point<f64>),
145    /// PATH values (open path from `LineString`)
146    #[cfg(feature = "geo-types")]
147    LineString(LineString<f64>),
148    /// BOX values (bounding rectangle)
149    #[cfg(feature = "geo-types")]
150    Rect(Rect<f64>),
151
152    // Bit string types
153    /// BIT, BIT VARYING values
154    #[cfg(feature = "bit-vec")]
155    BitVec(BitVec),
156
157    // Array types (using Vec for simplicity)
158    /// Array of any `PostgreSQL` type
159    Array(Vec<Self>),
160
161    /// `PostgreSQL` ENUM values (native enum types via `CREATE TYPE ... AS ENUM`)
162    Enum(Box<dyn PostgresEnum>),
163
164    /// NULL value
165    #[default]
166    Null,
167}
168
169impl SQLParam for OwnedPostgresValue {
170    const DIALECT: drizzle_core::Dialect = drizzle_core::Dialect::PostgreSQL;
171    type DialectMarker = drizzle_core::dialect::PostgresDialect;
172
173    /// Binds LIMIT/OFFSET values as `BIGINT` parameters so paginated queries
174    /// share one SQL text (and one cached prepared statement) across pages.
175    #[inline]
176    fn pagination_param(value: usize) -> Option<Self> {
177        i64::try_from(value).ok().map(Self::Bigint)
178    }
179}
180
181impl From<OwnedPostgresValue> for SQL<'_, OwnedPostgresValue> {
182    fn from(value: OwnedPostgresValue) -> Self {
183        SQL::param(value)
184    }
185}
186
187impl From<OwnedPostgresValue> for Cow<'_, OwnedPostgresValue> {
188    fn from(value: OwnedPostgresValue) -> Self {
189        Cow::Owned(value)
190    }
191}
192
193impl<'a> From<&'a OwnedPostgresValue> for Cow<'a, OwnedPostgresValue> {
194    fn from(value: &'a OwnedPostgresValue) -> Self {
195        Cow::Borrowed(value)
196    }
197}
198
199impl OwnedPostgresValue {
200    /// Returns true if this value is NULL.
201    #[inline]
202    #[must_use]
203    pub const fn is_null(&self) -> bool {
204        matches!(self, Self::Null)
205    }
206
207    /// Returns the boolean value if this is BOOLEAN.
208    #[inline]
209    #[must_use]
210    pub const fn as_bool(&self) -> Option<bool> {
211        match self {
212            Self::Boolean(value) => Some(*value),
213            _ => None,
214        }
215    }
216
217    /// Returns the i16 value if this is SMALLINT.
218    #[inline]
219    #[must_use]
220    pub const fn as_i16(&self) -> Option<i16> {
221        match self {
222            Self::Smallint(value) => Some(*value),
223            _ => None,
224        }
225    }
226
227    /// Returns the i32 value if this is INTEGER.
228    #[inline]
229    #[must_use]
230    pub const fn as_i32(&self) -> Option<i32> {
231        match self {
232            Self::Integer(value) => Some(*value),
233            _ => None,
234        }
235    }
236
237    /// Returns the i64 value if this is BIGINT.
238    #[inline]
239    #[must_use]
240    pub const fn as_i64(&self) -> Option<i64> {
241        match self {
242            Self::Bigint(value) => Some(*value),
243            _ => None,
244        }
245    }
246
247    /// Returns the f32 value if this is REAL.
248    #[inline]
249    #[must_use]
250    pub const fn as_f32(&self) -> Option<f32> {
251        match self {
252            Self::Real(value) => Some(*value),
253            _ => None,
254        }
255    }
256
257    /// Returns the f64 value if this is DOUBLE PRECISION.
258    #[inline]
259    #[must_use]
260    pub const fn as_f64(&self) -> Option<f64> {
261        match self {
262            Self::DoublePrecision(value) => Some(*value),
263            _ => None,
264        }
265    }
266
267    /// Returns the decimal value if this is NUMERIC.
268    #[inline]
269    #[cfg(feature = "rust-decimal")]
270    #[must_use]
271    pub const fn as_decimal(&self) -> Option<&Decimal> {
272        match self {
273            Self::Numeric(value) => Some(value),
274            _ => None,
275        }
276    }
277
278    /// Returns the text value if this is TEXT.
279    #[inline]
280    #[must_use]
281    pub const fn as_str(&self) -> Option<&str> {
282        match self {
283            Self::Text(value) => Some(value.as_str()),
284            _ => None,
285        }
286    }
287
288    /// Returns the bytea value if this is BYTEA.
289    #[inline]
290    #[must_use]
291    pub fn as_bytes(&self) -> Option<&[u8]> {
292        match self {
293            Self::Bytea(value) => Some(value.as_ref()),
294            _ => None,
295        }
296    }
297
298    /// Returns the UUID value if this is UUID.
299    #[inline]
300    #[cfg(feature = "uuid")]
301    #[must_use]
302    pub const fn as_uuid(&self) -> Option<Uuid> {
303        match self {
304            Self::Uuid(value) => Some(*value),
305            _ => None,
306        }
307    }
308
309    /// Returns the JSON value if this is JSON.
310    #[inline]
311    #[cfg(feature = "serde")]
312    #[must_use]
313    pub const fn as_json(&self) -> Option<&serde_json::Value> {
314        match self {
315            Self::Json(value) => Some(value),
316            _ => None,
317        }
318    }
319
320    /// Returns the JSONB value if this is JSONB.
321    #[inline]
322    #[cfg(feature = "serde")]
323    #[must_use]
324    pub const fn as_jsonb(&self) -> Option<&serde_json::Value> {
325        match self {
326            Self::Jsonb(value) => Some(value),
327            _ => None,
328        }
329    }
330
331    /// Returns the date value if this is DATE.
332    #[inline]
333    #[cfg(feature = "chrono")]
334    #[must_use]
335    pub const fn as_date(&self) -> Option<&NaiveDate> {
336        match self {
337            Self::Date(value) => Some(value),
338            _ => None,
339        }
340    }
341
342    /// Returns the time value if this is TIME.
343    #[inline]
344    #[cfg(feature = "chrono")]
345    #[must_use]
346    pub const fn as_time(&self) -> Option<&NaiveTime> {
347        match self {
348            Self::Time(value) => Some(value),
349            _ => None,
350        }
351    }
352
353    /// Returns the timestamp value if this is TIMESTAMP.
354    #[inline]
355    #[cfg(feature = "chrono")]
356    #[must_use]
357    pub const fn as_timestamp(&self) -> Option<&NaiveDateTime> {
358        match self {
359            Self::Timestamp(value) => Some(value),
360            _ => None,
361        }
362    }
363
364    /// Returns the timestamp with timezone value if this is TIMESTAMPTZ.
365    #[inline]
366    #[cfg(feature = "chrono")]
367    #[must_use]
368    pub const fn as_timestamp_tz(&self) -> Option<&DateTime<FixedOffset>> {
369        match self {
370            Self::TimestampTz(value) => Some(value),
371            _ => None,
372        }
373    }
374
375    /// Returns the interval value if this is INTERVAL.
376    #[inline]
377    #[cfg(feature = "chrono")]
378    #[must_use]
379    pub const fn as_interval(&self) -> Option<&Duration> {
380        match self {
381            Self::Interval(value) => Some(value),
382            _ => None,
383        }
384    }
385
386    /// Returns the date value if this is DATE (time crate).
387    #[inline]
388    #[cfg(feature = "time")]
389    #[must_use]
390    pub const fn as_time_date(&self) -> Option<&TimeDate> {
391        match self {
392            Self::TimeDate(value) => Some(value),
393            _ => None,
394        }
395    }
396
397    /// Returns the time value if this is TIME (time crate).
398    #[inline]
399    #[cfg(feature = "time")]
400    #[must_use]
401    pub const fn as_time_time(&self) -> Option<&TimeTime> {
402        match self {
403            Self::TimeTime(value) => Some(value),
404            _ => None,
405        }
406    }
407
408    /// Returns the timestamp value if this is TIMESTAMP (time crate).
409    #[inline]
410    #[cfg(feature = "time")]
411    #[must_use]
412    pub const fn as_time_timestamp(&self) -> Option<&PrimitiveDateTime> {
413        match self {
414            Self::TimeTimestamp(value) => Some(value),
415            _ => None,
416        }
417    }
418
419    /// Returns the timestamp with timezone value if this is TIMESTAMPTZ (time crate).
420    #[inline]
421    #[cfg(feature = "time")]
422    #[must_use]
423    pub const fn as_time_timestamp_tz(&self) -> Option<&OffsetDateTime> {
424        match self {
425            Self::TimeTimestampTz(value) => Some(value),
426            _ => None,
427        }
428    }
429
430    /// Returns the interval value if this is INTERVAL (time crate).
431    #[inline]
432    #[cfg(feature = "time")]
433    #[must_use]
434    pub const fn as_time_interval(&self) -> Option<&TimeDuration> {
435        match self {
436            Self::TimeInterval(value) => Some(value),
437            _ => None,
438        }
439    }
440
441    /// Returns the date value if this is DATE (jiff).
442    #[inline]
443    #[cfg(feature = "jiff")]
444    #[must_use]
445    pub const fn as_jiff_date(&self) -> Option<&JiffDate> {
446        match self {
447            Self::JiffDate(value) => Some(value),
448            _ => None,
449        }
450    }
451
452    /// Returns the time value if this is TIME (jiff).
453    #[inline]
454    #[cfg(feature = "jiff")]
455    #[must_use]
456    pub const fn as_jiff_time(&self) -> Option<&JiffTime> {
457        match self {
458            Self::JiffTime(value) => Some(value),
459            _ => None,
460        }
461    }
462
463    /// Returns the timestamp value if this is TIMESTAMP (jiff).
464    #[inline]
465    #[cfg(feature = "jiff")]
466    #[must_use]
467    pub const fn as_jiff_datetime(&self) -> Option<&JiffDateTime> {
468        match self {
469            Self::JiffDateTime(value) => Some(value),
470            _ => None,
471        }
472    }
473
474    /// Returns the timestamp with timezone value if this is TIMESTAMPTZ (jiff).
475    #[inline]
476    #[cfg(feature = "jiff")]
477    #[must_use]
478    pub const fn as_jiff_timestamp(&self) -> Option<&JiffTimestamp> {
479        match self {
480            Self::JiffTimestamp(value) => Some(value),
481            _ => None,
482        }
483    }
484
485    /// Returns the inet value if this is INET.
486    #[inline]
487    #[cfg(feature = "cidr")]
488    #[must_use]
489    pub const fn as_inet(&self) -> Option<&IpInet> {
490        match self {
491            Self::Inet(value) => Some(value),
492            _ => None,
493        }
494    }
495
496    /// Returns the cidr value if this is CIDR.
497    #[inline]
498    #[cfg(feature = "cidr")]
499    #[must_use]
500    pub const fn as_cidr(&self) -> Option<&IpCidr> {
501        match self {
502            Self::Cidr(value) => Some(value),
503            _ => None,
504        }
505    }
506
507    /// Returns the MAC address if this is MACADDR.
508    #[inline]
509    #[cfg(feature = "cidr")]
510    #[must_use]
511    pub const fn as_macaddr(&self) -> Option<[u8; 6]> {
512        match self {
513            Self::MacAddr(value) => Some(*value),
514            _ => None,
515        }
516    }
517
518    /// Returns the MAC address if this is MACADDR8.
519    #[inline]
520    #[cfg(feature = "cidr")]
521    #[must_use]
522    pub const fn as_macaddr8(&self) -> Option<[u8; 8]> {
523        match self {
524            Self::MacAddr8(value) => Some(*value),
525            _ => None,
526        }
527    }
528
529    /// Returns the point value if this is POINT.
530    #[inline]
531    #[cfg(feature = "geo-types")]
532    #[must_use]
533    pub const fn as_point(&self) -> Option<&Point<f64>> {
534        match self {
535            Self::Point(value) => Some(value),
536            _ => None,
537        }
538    }
539
540    /// Returns the line string value if this is PATH.
541    #[inline]
542    #[cfg(feature = "geo-types")]
543    #[must_use]
544    pub const fn as_line_string(&self) -> Option<&LineString<f64>> {
545        match self {
546            Self::LineString(value) => Some(value),
547            _ => None,
548        }
549    }
550
551    /// Returns the rect value if this is BOX.
552    #[inline]
553    #[cfg(feature = "geo-types")]
554    #[must_use]
555    pub const fn as_rect(&self) -> Option<&Rect<f64>> {
556        match self {
557            Self::Rect(value) => Some(value),
558            _ => None,
559        }
560    }
561
562    /// Returns the bit vector if this is BIT/VARBIT.
563    #[inline]
564    #[cfg(feature = "bit-vec")]
565    #[must_use]
566    pub const fn as_bitvec(&self) -> Option<&BitVec> {
567        match self {
568            Self::BitVec(value) => Some(value),
569            _ => None,
570        }
571    }
572
573    /// Returns the array elements if this is an ARRAY.
574    #[inline]
575    #[must_use]
576    pub fn as_array(&self) -> Option<&[Self]> {
577        match self {
578            Self::Array(values) => Some(values),
579            _ => None,
580        }
581    }
582
583    /// Returns a borrowed `PostgresValue` view of this owned value.
584    #[inline]
585    pub fn as_value(&self) -> PostgresValue<'_> {
586        match self {
587            Self::Smallint(value) => PostgresValue::Smallint(*value),
588            Self::Integer(value) => PostgresValue::Integer(*value),
589            Self::Bigint(value) => PostgresValue::Bigint(*value),
590            Self::Real(value) => PostgresValue::Real(*value),
591            Self::DoublePrecision(value) => PostgresValue::DoublePrecision(*value),
592            #[cfg(feature = "rust-decimal")]
593            Self::Numeric(value) => PostgresValue::Numeric(*value),
594            Self::Text(value) => PostgresValue::Text(Cow::Borrowed(value)),
595            Self::Bytea(value) => PostgresValue::Bytea(Cow::Borrowed(value)),
596            Self::Boolean(value) => PostgresValue::Boolean(*value),
597            #[cfg(feature = "uuid")]
598            Self::Uuid(value) => PostgresValue::Uuid(*value),
599            #[cfg(feature = "serde")]
600            Self::Json(value) => PostgresValue::Json(value.clone()),
601            #[cfg(feature = "serde")]
602            Self::Jsonb(value) => PostgresValue::Jsonb(value.clone()),
603            #[cfg(feature = "chrono")]
604            Self::Date(value) => PostgresValue::Date(*value),
605            #[cfg(feature = "chrono")]
606            Self::Time(value) => PostgresValue::Time(*value),
607            #[cfg(feature = "chrono")]
608            Self::Timestamp(value) => PostgresValue::Timestamp(*value),
609            #[cfg(feature = "chrono")]
610            Self::TimestampTz(value) => PostgresValue::TimestampTz(*value),
611            #[cfg(feature = "chrono")]
612            Self::Interval(value) => PostgresValue::Interval(*value),
613            #[cfg(feature = "time")]
614            Self::TimeDate(value) => PostgresValue::TimeDate(*value),
615            #[cfg(feature = "time")]
616            Self::TimeTime(value) => PostgresValue::TimeTime(*value),
617            #[cfg(feature = "time")]
618            Self::TimeTimestamp(value) => PostgresValue::TimeTimestamp(*value),
619            #[cfg(feature = "time")]
620            Self::TimeTimestampTz(value) => PostgresValue::TimeTimestampTz(*value),
621            #[cfg(feature = "time")]
622            Self::TimeInterval(value) => PostgresValue::TimeInterval(*value),
623            #[cfg(feature = "jiff")]
624            Self::JiffDate(value) => PostgresValue::JiffDate(*value),
625            #[cfg(feature = "jiff")]
626            Self::JiffTime(value) => PostgresValue::JiffTime(*value),
627            #[cfg(feature = "jiff")]
628            Self::JiffDateTime(value) => PostgresValue::JiffDateTime(*value),
629            #[cfg(feature = "jiff")]
630            Self::JiffTimestamp(value) => PostgresValue::JiffTimestamp(*value),
631            #[cfg(feature = "cidr")]
632            Self::Inet(value) => PostgresValue::Inet(*value),
633            #[cfg(feature = "cidr")]
634            Self::Cidr(value) => PostgresValue::Cidr(*value),
635            #[cfg(feature = "cidr")]
636            Self::MacAddr(value) => PostgresValue::MacAddr(*value),
637            #[cfg(feature = "cidr")]
638            Self::MacAddr8(value) => PostgresValue::MacAddr8(*value),
639            #[cfg(feature = "geo-types")]
640            Self::Point(value) => PostgresValue::Point(*value),
641            #[cfg(feature = "geo-types")]
642            Self::LineString(value) => PostgresValue::LineString(value.clone()),
643            #[cfg(feature = "geo-types")]
644            Self::Rect(value) => PostgresValue::Rect(*value),
645            #[cfg(feature = "bit-vec")]
646            Self::BitVec(value) => PostgresValue::BitVec(value.clone()),
647            Self::Array(values) => {
648                PostgresValue::Array(values.iter().map(Self::as_value).collect())
649            }
650            Self::Enum(value) => PostgresValue::Enum(value.clone()),
651            Self::Null => PostgresValue::Null,
652        }
653    }
654
655    /// Converts the value into a Rust type through [`FromPostgresValue`].
656    ///
657    /// Behaves like [`PostgresValue::convert`](super::PostgresValue::convert).
658    ///
659    /// # Errors
660    ///
661    /// Returns [`DrizzleError::ConversionError`] when `T` cannot hold the
662    /// stored value.
663    pub fn convert<T: FromPostgresValue>(self) -> Result<T, DrizzleError> {
664        match self {
665            Self::Boolean(value) => T::from_postgres_bool(value),
666            Self::Smallint(value) => T::from_postgres_i16(value),
667            Self::Integer(value) => T::from_postgres_i32(value),
668            Self::Bigint(value) => T::from_postgres_i64(value),
669            Self::Real(value) => T::from_postgres_f32(value),
670            Self::DoublePrecision(value) => T::from_postgres_f64(value),
671            #[cfg(feature = "rust-decimal")]
672            Self::Numeric(value) => {
673                let text = value.to_string();
674                T::from_postgres_text(&text)
675            }
676            Self::Text(value) => T::from_postgres_text(&value),
677            Self::Bytea(value) => T::from_postgres_bytes(&value),
678            #[cfg(feature = "uuid")]
679            Self::Uuid(value) => T::from_postgres_uuid(value),
680            #[cfg(feature = "serde")]
681            Self::Json(value) => T::from_postgres_json(value),
682            #[cfg(feature = "serde")]
683            Self::Jsonb(value) => T::from_postgres_jsonb(value),
684            #[cfg(feature = "chrono")]
685            Self::Date(value) => T::from_postgres_date(value),
686            #[cfg(feature = "chrono")]
687            Self::Time(value) => T::from_postgres_time(value),
688            #[cfg(feature = "chrono")]
689            Self::Timestamp(value) => T::from_postgres_timestamp(value),
690            #[cfg(feature = "chrono")]
691            Self::TimestampTz(value) => T::from_postgres_timestamptz(value),
692            #[cfg(feature = "chrono")]
693            Self::Interval(value) => T::from_postgres_interval(value),
694            #[cfg(feature = "time")]
695            Self::TimeDate(value) => T::from_postgres_time_date(value),
696            #[cfg(feature = "time")]
697            Self::TimeTime(value) => T::from_postgres_time_time(value),
698            #[cfg(feature = "time")]
699            Self::TimeTimestamp(value) => T::from_postgres_time_timestamp(value),
700            #[cfg(feature = "time")]
701            Self::TimeTimestampTz(value) => T::from_postgres_time_timestamptz(value),
702            #[cfg(feature = "time")]
703            Self::TimeInterval(value) => T::from_postgres_time_interval(value),
704            #[cfg(feature = "jiff")]
705            Self::JiffDate(value) => T::from_postgres_jiff_date(value),
706            #[cfg(feature = "jiff")]
707            Self::JiffTime(value) => T::from_postgres_jiff_time(value),
708            #[cfg(feature = "jiff")]
709            Self::JiffDateTime(value) => T::from_postgres_jiff_datetime(value),
710            #[cfg(feature = "jiff")]
711            Self::JiffTimestamp(value) => T::from_postgres_jiff_timestamp(value),
712            #[cfg(feature = "cidr")]
713            Self::Inet(value) => T::from_postgres_inet(value),
714            #[cfg(feature = "cidr")]
715            Self::Cidr(value) => T::from_postgres_cidr(value),
716            #[cfg(feature = "cidr")]
717            Self::MacAddr(value) => T::from_postgres_macaddr(value),
718            #[cfg(feature = "cidr")]
719            Self::MacAddr8(value) => T::from_postgres_macaddr8(value),
720            #[cfg(feature = "geo-types")]
721            Self::Point(value) => T::from_postgres_point(value),
722            #[cfg(feature = "geo-types")]
723            Self::LineString(value) => T::from_postgres_linestring(value),
724            #[cfg(feature = "geo-types")]
725            Self::Rect(value) => T::from_postgres_rect(value),
726            #[cfg(feature = "bit-vec")]
727            Self::BitVec(value) => T::from_postgres_bitvec(value),
728            Self::Array(values) => {
729                let values = values.into_iter().map(PostgresValue::from).collect();
730                T::from_postgres_array(values)
731            }
732            Self::Enum(value) => T::from_postgres_text(value.variant_name()),
733            Self::Null => T::from_postgres_null(),
734        }
735    }
736
737    /// Like [`convert`](Self::convert), but borrows the value (cloning data
738    /// where `T` needs it).
739    ///
740    /// # Errors
741    ///
742    /// Returns [`DrizzleError::ConversionError`] when `T` cannot hold the
743    /// stored value.
744    pub fn convert_ref<T: FromPostgresValue>(&self) -> Result<T, DrizzleError> {
745        match self {
746            Self::Boolean(value) => T::from_postgres_bool(*value),
747            Self::Smallint(value) => T::from_postgres_i16(*value),
748            Self::Integer(value) => T::from_postgres_i32(*value),
749            Self::Bigint(value) => T::from_postgres_i64(*value),
750            Self::Real(value) => T::from_postgres_f32(*value),
751            Self::DoublePrecision(value) => T::from_postgres_f64(*value),
752            #[cfg(feature = "rust-decimal")]
753            Self::Numeric(value) => {
754                let text = value.to_string();
755                T::from_postgres_text(&text)
756            }
757            Self::Text(value) => T::from_postgres_text(value),
758            Self::Bytea(value) => T::from_postgres_bytes(value),
759            #[cfg(feature = "uuid")]
760            Self::Uuid(value) => T::from_postgres_uuid(*value),
761            #[cfg(feature = "serde")]
762            Self::Json(value) => T::from_postgres_json(value.clone()),
763            #[cfg(feature = "serde")]
764            Self::Jsonb(value) => T::from_postgres_jsonb(value.clone()),
765            #[cfg(feature = "chrono")]
766            Self::Date(value) => T::from_postgres_date(*value),
767            #[cfg(feature = "chrono")]
768            Self::Time(value) => T::from_postgres_time(*value),
769            #[cfg(feature = "chrono")]
770            Self::Timestamp(value) => T::from_postgres_timestamp(*value),
771            #[cfg(feature = "chrono")]
772            Self::TimestampTz(value) => T::from_postgres_timestamptz(*value),
773            #[cfg(feature = "chrono")]
774            Self::Interval(value) => T::from_postgres_interval(*value),
775            #[cfg(feature = "time")]
776            Self::TimeDate(value) => T::from_postgres_time_date(*value),
777            #[cfg(feature = "time")]
778            Self::TimeTime(value) => T::from_postgres_time_time(*value),
779            #[cfg(feature = "time")]
780            Self::TimeTimestamp(value) => T::from_postgres_time_timestamp(*value),
781            #[cfg(feature = "time")]
782            Self::TimeTimestampTz(value) => T::from_postgres_time_timestamptz(*value),
783            #[cfg(feature = "time")]
784            Self::TimeInterval(value) => T::from_postgres_time_interval(*value),
785            #[cfg(feature = "jiff")]
786            Self::JiffDate(value) => T::from_postgres_jiff_date(*value),
787            #[cfg(feature = "jiff")]
788            Self::JiffTime(value) => T::from_postgres_jiff_time(*value),
789            #[cfg(feature = "jiff")]
790            Self::JiffDateTime(value) => T::from_postgres_jiff_datetime(*value),
791            #[cfg(feature = "jiff")]
792            Self::JiffTimestamp(value) => T::from_postgres_jiff_timestamp(*value),
793            #[cfg(feature = "cidr")]
794            Self::Inet(value) => T::from_postgres_inet(*value),
795            #[cfg(feature = "cidr")]
796            Self::Cidr(value) => T::from_postgres_cidr(*value),
797            #[cfg(feature = "cidr")]
798            Self::MacAddr(value) => T::from_postgres_macaddr(*value),
799            #[cfg(feature = "cidr")]
800            Self::MacAddr8(value) => T::from_postgres_macaddr8(*value),
801            #[cfg(feature = "geo-types")]
802            Self::Point(value) => T::from_postgres_point(*value),
803            #[cfg(feature = "geo-types")]
804            Self::LineString(value) => T::from_postgres_linestring(value.clone()),
805            #[cfg(feature = "geo-types")]
806            Self::Rect(value) => T::from_postgres_rect(*value),
807            #[cfg(feature = "bit-vec")]
808            Self::BitVec(value) => T::from_postgres_bitvec(value.clone()),
809            Self::Array(values) => {
810                let values = values.iter().map(Self::as_value).collect();
811                T::from_postgres_array(values)
812            }
813            Self::Enum(value) => T::from_postgres_text(value.variant_name()),
814            Self::Null => T::from_postgres_null(),
815        }
816    }
817}
818
819impl core::fmt::Display for OwnedPostgresValue {
820    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
821        let value = match self {
822            Self::Smallint(i) => i.to_string(),
823            Self::Integer(i) => i.to_string(),
824            Self::Bigint(i) => i.to_string(),
825            Self::Real(r) => r.to_string(),
826            Self::DoublePrecision(r) => r.to_string(),
827            #[cfg(feature = "rust-decimal")]
828            Self::Numeric(d) => d.to_string(),
829            Self::Text(s) => s.clone(),
830            Self::Bytea(b) => {
831                use core::fmt::Write;
832                let mut s = String::with_capacity(2 + b.len() * 2);
833                s.push_str("\\x");
834                for byte in b {
835                    write!(s, "{byte:02x}").expect("writing to String cannot fail");
836                }
837                s
838            }
839            Self::Boolean(b) => b.to_string(),
840            #[cfg(feature = "uuid")]
841            Self::Uuid(uuid) => uuid.to_string(),
842            #[cfg(feature = "serde")]
843            Self::Json(json) => json.to_string(),
844            #[cfg(feature = "serde")]
845            Self::Jsonb(json) => json.to_string(),
846
847            // Date and time types
848            #[cfg(feature = "chrono")]
849            Self::Date(date) => date.to_string(),
850            #[cfg(feature = "chrono")]
851            Self::Time(time) => time.to_string(),
852            #[cfg(feature = "chrono")]
853            Self::Timestamp(ts) => ts.to_string(),
854            #[cfg(feature = "chrono")]
855            Self::TimestampTz(ts) => ts.to_string(),
856            #[cfg(feature = "chrono")]
857            Self::Interval(dur) => format!("{} seconds", dur.num_seconds()),
858
859            // Date and time types (time crate)
860            #[cfg(feature = "time")]
861            Self::TimeDate(date) => date.to_string(),
862            #[cfg(feature = "time")]
863            Self::TimeTime(time) => time.to_string(),
864            #[cfg(feature = "time")]
865            Self::TimeTimestamp(ts) => ts.to_string(),
866            #[cfg(feature = "time")]
867            Self::TimeTimestampTz(ts) => ts.to_string(),
868            #[cfg(feature = "time")]
869            Self::TimeInterval(dur) => format!("{} seconds", dur.whole_seconds()),
870            #[cfg(feature = "jiff")]
871            Self::JiffDate(date) => date.to_string(),
872            #[cfg(feature = "jiff")]
873            Self::JiffTime(time) => time.to_string(),
874            #[cfg(feature = "jiff")]
875            Self::JiffDateTime(ts) => ts.to_string(),
876            #[cfg(feature = "jiff")]
877            Self::JiffTimestamp(ts) => ts.to_string(),
878
879            // Network address types
880            #[cfg(feature = "cidr")]
881            Self::Inet(net) => net.to_string(),
882            #[cfg(feature = "cidr")]
883            Self::Cidr(net) => net.to_string(),
884            #[cfg(feature = "cidr")]
885            Self::MacAddr(mac) => format!(
886                "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
887                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
888            ),
889            #[cfg(feature = "cidr")]
890            Self::MacAddr8(mac) => format!(
891                "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
892                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5], mac[6], mac[7]
893            ),
894
895            // Geometric types
896            #[cfg(feature = "geo-types")]
897            Self::Point(point) => format!("({},{})", point.x(), point.y()),
898            #[cfg(feature = "geo-types")]
899            Self::LineString(line) => {
900                let coords: Vec<String> = line
901                    .coords()
902                    .map(|coord| format!("({},{})", coord.x, coord.y))
903                    .collect();
904                format!("[{}]", coords.join(","))
905            }
906            #[cfg(feature = "geo-types")]
907            Self::Rect(rect) => {
908                format!(
909                    "(({},{}),({},{}))",
910                    rect.min().x,
911                    rect.min().y,
912                    rect.max().x,
913                    rect.max().y
914                )
915            }
916
917            // Bit string types
918            #[cfg(feature = "bit-vec")]
919            Self::BitVec(bv) => bv
920                .iter()
921                .map(|b| if b { '1' } else { '0' })
922                .collect::<String>(),
923
924            // Array types
925            Self::Array(arr) => {
926                let elements: Vec<String> = arr.iter().map(ToString::to_string).collect();
927                format!("{{{}}}", elements.join(","))
928            }
929
930            Self::Enum(enum_val) => enum_val.variant_name().to_string(),
931
932            Self::Null => String::new(),
933        };
934        write!(f, "{value}")
935    }
936}
937
938// Conversions from PostgresValue to OwnedPostgresValue
939impl<'a> From<PostgresValue<'a>> for OwnedPostgresValue {
940    fn from(value: PostgresValue<'a>) -> Self {
941        match value {
942            PostgresValue::Smallint(i) => Self::Smallint(i),
943            PostgresValue::Integer(i) => Self::Integer(i),
944            PostgresValue::Bigint(i) => Self::Bigint(i),
945            PostgresValue::Real(r) => Self::Real(r),
946            PostgresValue::DoublePrecision(r) => Self::DoublePrecision(r),
947            #[cfg(feature = "rust-decimal")]
948            PostgresValue::Numeric(d) => Self::Numeric(d),
949            PostgresValue::Text(cow) => Self::Text(cow.into_owned()),
950            PostgresValue::Bytea(cow) => Self::Bytea(cow.into_owned()),
951            PostgresValue::Boolean(b) => Self::Boolean(b),
952            #[cfg(feature = "uuid")]
953            PostgresValue::Uuid(uuid) => Self::Uuid(uuid),
954            #[cfg(feature = "serde")]
955            PostgresValue::Json(json) => Self::Json(json),
956            #[cfg(feature = "serde")]
957            PostgresValue::Jsonb(json) => Self::Jsonb(json),
958            PostgresValue::Enum(enum_val) => Self::Enum(enum_val),
959            PostgresValue::Null => Self::Null,
960            #[cfg(feature = "chrono")]
961            PostgresValue::Date(date) => Self::Date(date),
962            #[cfg(feature = "chrono")]
963            PostgresValue::Time(time) => Self::Time(time),
964            #[cfg(feature = "chrono")]
965            PostgresValue::Timestamp(ts) => Self::Timestamp(ts),
966            #[cfg(feature = "chrono")]
967            PostgresValue::TimestampTz(ts) => Self::TimestampTz(ts),
968            #[cfg(feature = "chrono")]
969            PostgresValue::Interval(dur) => Self::Interval(dur),
970            #[cfg(feature = "time")]
971            PostgresValue::TimeDate(v) => Self::TimeDate(v),
972            #[cfg(feature = "time")]
973            PostgresValue::TimeTime(v) => Self::TimeTime(v),
974            #[cfg(feature = "time")]
975            PostgresValue::TimeTimestamp(v) => Self::TimeTimestamp(v),
976            #[cfg(feature = "time")]
977            PostgresValue::TimeTimestampTz(v) => Self::TimeTimestampTz(v),
978            #[cfg(feature = "time")]
979            PostgresValue::TimeInterval(v) => Self::TimeInterval(v),
980            #[cfg(feature = "jiff")]
981            PostgresValue::JiffDate(v) => Self::JiffDate(v),
982            #[cfg(feature = "jiff")]
983            PostgresValue::JiffTime(v) => Self::JiffTime(v),
984            #[cfg(feature = "jiff")]
985            PostgresValue::JiffDateTime(v) => Self::JiffDateTime(v),
986            #[cfg(feature = "jiff")]
987            PostgresValue::JiffTimestamp(v) => Self::JiffTimestamp(v),
988            #[cfg(feature = "cidr")]
989            PostgresValue::Inet(net) => Self::Inet(net),
990            #[cfg(feature = "cidr")]
991            PostgresValue::Cidr(net) => Self::Cidr(net),
992            #[cfg(feature = "cidr")]
993            PostgresValue::MacAddr(mac) => Self::MacAddr(mac),
994            #[cfg(feature = "cidr")]
995            PostgresValue::MacAddr8(mac) => Self::MacAddr8(mac),
996            #[cfg(feature = "geo-types")]
997            PostgresValue::Point(point) => Self::Point(point),
998            #[cfg(feature = "geo-types")]
999            PostgresValue::LineString(line) => Self::LineString(line),
1000            #[cfg(feature = "geo-types")]
1001            PostgresValue::Rect(rect) => Self::Rect(rect),
1002            #[cfg(feature = "bit-vec")]
1003            PostgresValue::BitVec(bv) => Self::BitVec(bv),
1004            PostgresValue::Array(arr) => {
1005                let owned_arr = arr.into_iter().map(Self::from).collect();
1006                Self::Array(owned_arr)
1007            }
1008        }
1009    }
1010}
1011
1012impl<'a> From<&PostgresValue<'a>> for OwnedPostgresValue {
1013    fn from(value: &PostgresValue<'a>) -> Self {
1014        match value {
1015            PostgresValue::Smallint(i) => Self::Smallint(*i),
1016            PostgresValue::Integer(i) => Self::Integer(*i),
1017            PostgresValue::Bigint(i) => Self::Bigint(*i),
1018            PostgresValue::Real(r) => Self::Real(*r),
1019            PostgresValue::DoublePrecision(r) => Self::DoublePrecision(*r),
1020            #[cfg(feature = "rust-decimal")]
1021            PostgresValue::Numeric(d) => Self::Numeric(*d),
1022            PostgresValue::Text(cow) => Self::Text(cow.clone().into_owned()),
1023            PostgresValue::Bytea(cow) => Self::Bytea(cow.clone().into_owned()),
1024            PostgresValue::Boolean(b) => Self::Boolean(*b),
1025            #[cfg(feature = "uuid")]
1026            PostgresValue::Uuid(uuid) => Self::Uuid(*uuid),
1027            #[cfg(feature = "serde")]
1028            PostgresValue::Json(json) => Self::Json(json.clone()),
1029            #[cfg(feature = "serde")]
1030            PostgresValue::Jsonb(json) => Self::Jsonb(json.clone()),
1031            PostgresValue::Enum(enum_val) => Self::Enum(enum_val.clone()),
1032            #[cfg(feature = "chrono")]
1033            PostgresValue::Date(value) => Self::Date(*value),
1034            #[cfg(feature = "chrono")]
1035            PostgresValue::Time(value) => Self::Time(*value),
1036            #[cfg(feature = "chrono")]
1037            PostgresValue::Timestamp(value) => Self::Timestamp(*value),
1038            #[cfg(feature = "chrono")]
1039            PostgresValue::TimestampTz(value) => Self::TimestampTz(*value),
1040            #[cfg(feature = "chrono")]
1041            PostgresValue::Interval(value) => Self::Interval(*value),
1042            #[cfg(feature = "time")]
1043            PostgresValue::TimeDate(value) => Self::TimeDate(*value),
1044            #[cfg(feature = "time")]
1045            PostgresValue::TimeTime(value) => Self::TimeTime(*value),
1046            #[cfg(feature = "time")]
1047            PostgresValue::TimeTimestamp(value) => Self::TimeTimestamp(*value),
1048            #[cfg(feature = "time")]
1049            PostgresValue::TimeTimestampTz(value) => Self::TimeTimestampTz(*value),
1050            #[cfg(feature = "time")]
1051            PostgresValue::TimeInterval(value) => Self::TimeInterval(*value),
1052            #[cfg(feature = "jiff")]
1053            PostgresValue::JiffDate(value) => Self::JiffDate(*value),
1054            #[cfg(feature = "jiff")]
1055            PostgresValue::JiffTime(value) => Self::JiffTime(*value),
1056            #[cfg(feature = "jiff")]
1057            PostgresValue::JiffDateTime(value) => Self::JiffDateTime(*value),
1058            #[cfg(feature = "jiff")]
1059            PostgresValue::JiffTimestamp(value) => Self::JiffTimestamp(*value),
1060            #[cfg(feature = "cidr")]
1061            PostgresValue::Inet(value) => Self::Inet(*value),
1062            #[cfg(feature = "cidr")]
1063            PostgresValue::Cidr(value) => Self::Cidr(*value),
1064            #[cfg(feature = "cidr")]
1065            PostgresValue::MacAddr(value) => Self::MacAddr(*value),
1066            #[cfg(feature = "cidr")]
1067            PostgresValue::MacAddr8(value) => Self::MacAddr8(*value),
1068            #[cfg(feature = "geo-types")]
1069            PostgresValue::Point(value) => Self::Point(*value),
1070            #[cfg(feature = "geo-types")]
1071            PostgresValue::LineString(value) => Self::LineString(value.clone()),
1072            #[cfg(feature = "geo-types")]
1073            PostgresValue::Rect(value) => Self::Rect(*value),
1074            #[cfg(feature = "bit-vec")]
1075            PostgresValue::BitVec(value) => Self::BitVec(value.clone()),
1076            PostgresValue::Array(arr) => {
1077                let owned_arr = arr.iter().map(Self::from).collect();
1078                Self::Array(owned_arr)
1079            }
1080            PostgresValue::Null => Self::Null,
1081        }
1082    }
1083}
1084
1085// Conversions from OwnedPostgresValue to PostgresValue
1086impl From<OwnedPostgresValue> for PostgresValue<'_> {
1087    fn from(value: OwnedPostgresValue) -> Self {
1088        match value {
1089            OwnedPostgresValue::Smallint(i) => PostgresValue::Smallint(i),
1090            OwnedPostgresValue::Integer(i) => PostgresValue::Integer(i),
1091            OwnedPostgresValue::Bigint(i) => PostgresValue::Bigint(i),
1092            OwnedPostgresValue::Real(r) => PostgresValue::Real(r),
1093            OwnedPostgresValue::DoublePrecision(r) => PostgresValue::DoublePrecision(r),
1094            #[cfg(feature = "rust-decimal")]
1095            OwnedPostgresValue::Numeric(d) => PostgresValue::Numeric(d),
1096            OwnedPostgresValue::Text(s) => PostgresValue::Text(Cow::Owned(s)),
1097            OwnedPostgresValue::Bytea(b) => PostgresValue::Bytea(Cow::Owned(b)),
1098            OwnedPostgresValue::Boolean(b) => PostgresValue::Boolean(b),
1099            #[cfg(feature = "uuid")]
1100            OwnedPostgresValue::Uuid(uuid) => PostgresValue::Uuid(uuid),
1101            #[cfg(feature = "serde")]
1102            OwnedPostgresValue::Json(json) => PostgresValue::Json(json),
1103            #[cfg(feature = "serde")]
1104            OwnedPostgresValue::Jsonb(json) => PostgresValue::Jsonb(json),
1105
1106            // Date and time types
1107            #[cfg(feature = "chrono")]
1108            OwnedPostgresValue::Date(date) => PostgresValue::Date(date),
1109            #[cfg(feature = "chrono")]
1110            OwnedPostgresValue::Time(time) => PostgresValue::Time(time),
1111            #[cfg(feature = "chrono")]
1112            OwnedPostgresValue::Timestamp(ts) => PostgresValue::Timestamp(ts),
1113            #[cfg(feature = "chrono")]
1114            OwnedPostgresValue::TimestampTz(ts) => PostgresValue::TimestampTz(ts),
1115            #[cfg(feature = "chrono")]
1116            OwnedPostgresValue::Interval(dur) => PostgresValue::Interval(dur),
1117            #[cfg(feature = "time")]
1118            OwnedPostgresValue::TimeDate(v) => PostgresValue::TimeDate(v),
1119            #[cfg(feature = "time")]
1120            OwnedPostgresValue::TimeTime(v) => PostgresValue::TimeTime(v),
1121            #[cfg(feature = "time")]
1122            OwnedPostgresValue::TimeTimestamp(v) => PostgresValue::TimeTimestamp(v),
1123            #[cfg(feature = "time")]
1124            OwnedPostgresValue::TimeTimestampTz(v) => PostgresValue::TimeTimestampTz(v),
1125            #[cfg(feature = "time")]
1126            OwnedPostgresValue::TimeInterval(v) => PostgresValue::TimeInterval(v),
1127            #[cfg(feature = "jiff")]
1128            OwnedPostgresValue::JiffDate(v) => PostgresValue::JiffDate(v),
1129            #[cfg(feature = "jiff")]
1130            OwnedPostgresValue::JiffTime(v) => PostgresValue::JiffTime(v),
1131            #[cfg(feature = "jiff")]
1132            OwnedPostgresValue::JiffDateTime(v) => PostgresValue::JiffDateTime(v),
1133            #[cfg(feature = "jiff")]
1134            OwnedPostgresValue::JiffTimestamp(v) => PostgresValue::JiffTimestamp(v),
1135            #[cfg(feature = "cidr")]
1136            OwnedPostgresValue::Inet(net) => PostgresValue::Inet(net),
1137            #[cfg(feature = "cidr")]
1138            OwnedPostgresValue::Cidr(net) => PostgresValue::Cidr(net),
1139            #[cfg(feature = "cidr")]
1140            OwnedPostgresValue::MacAddr(mac) => PostgresValue::MacAddr(mac),
1141            #[cfg(feature = "cidr")]
1142            OwnedPostgresValue::MacAddr8(mac) => PostgresValue::MacAddr8(mac),
1143            #[cfg(feature = "geo-types")]
1144            OwnedPostgresValue::Point(point) => PostgresValue::Point(point),
1145            #[cfg(feature = "geo-types")]
1146            OwnedPostgresValue::LineString(line) => PostgresValue::LineString(line),
1147            #[cfg(feature = "geo-types")]
1148            OwnedPostgresValue::Rect(rect) => PostgresValue::Rect(rect),
1149            #[cfg(feature = "bit-vec")]
1150            OwnedPostgresValue::BitVec(bv) => PostgresValue::BitVec(bv),
1151            OwnedPostgresValue::Array(arr) => {
1152                let postgres_arr = arr.into_iter().map(PostgresValue::from).collect();
1153                PostgresValue::Array(postgres_arr)
1154            }
1155
1156            OwnedPostgresValue::Enum(enum_val) => PostgresValue::Enum(enum_val),
1157
1158            OwnedPostgresValue::Null => PostgresValue::Null,
1159        }
1160    }
1161}
1162
1163impl<'a> From<&'a OwnedPostgresValue> for PostgresValue<'a> {
1164    fn from(value: &'a OwnedPostgresValue) -> Self {
1165        match value {
1166            OwnedPostgresValue::Smallint(i) => PostgresValue::Smallint(*i),
1167            OwnedPostgresValue::Integer(i) => PostgresValue::Integer(*i),
1168            OwnedPostgresValue::Bigint(i) => PostgresValue::Bigint(*i),
1169            OwnedPostgresValue::Real(r) => PostgresValue::Real(*r),
1170            OwnedPostgresValue::DoublePrecision(r) => PostgresValue::DoublePrecision(*r),
1171            #[cfg(feature = "rust-decimal")]
1172            OwnedPostgresValue::Numeric(d) => PostgresValue::Numeric(*d),
1173            OwnedPostgresValue::Text(s) => PostgresValue::Text(Cow::Borrowed(s)),
1174            OwnedPostgresValue::Bytea(b) => PostgresValue::Bytea(Cow::Borrowed(b)),
1175            OwnedPostgresValue::Boolean(b) => PostgresValue::Boolean(*b),
1176            #[cfg(feature = "uuid")]
1177            OwnedPostgresValue::Uuid(uuid) => PostgresValue::Uuid(*uuid),
1178            #[cfg(feature = "serde")]
1179            OwnedPostgresValue::Json(json) => PostgresValue::Json(json.clone()),
1180            #[cfg(feature = "serde")]
1181            OwnedPostgresValue::Jsonb(json) => PostgresValue::Jsonb(json.clone()),
1182            #[cfg(feature = "chrono")]
1183            OwnedPostgresValue::Date(value) => PostgresValue::Date(*value),
1184            #[cfg(feature = "chrono")]
1185            OwnedPostgresValue::Time(value) => PostgresValue::Time(*value),
1186            #[cfg(feature = "chrono")]
1187            OwnedPostgresValue::Timestamp(value) => PostgresValue::Timestamp(*value),
1188            #[cfg(feature = "chrono")]
1189            OwnedPostgresValue::TimestampTz(value) => PostgresValue::TimestampTz(*value),
1190            #[cfg(feature = "chrono")]
1191            OwnedPostgresValue::Interval(value) => PostgresValue::Interval(*value),
1192            #[cfg(feature = "time")]
1193            OwnedPostgresValue::TimeDate(value) => PostgresValue::TimeDate(*value),
1194            #[cfg(feature = "time")]
1195            OwnedPostgresValue::TimeTime(value) => PostgresValue::TimeTime(*value),
1196            #[cfg(feature = "time")]
1197            OwnedPostgresValue::TimeTimestamp(value) => PostgresValue::TimeTimestamp(*value),
1198            #[cfg(feature = "time")]
1199            OwnedPostgresValue::TimeTimestampTz(value) => PostgresValue::TimeTimestampTz(*value),
1200            #[cfg(feature = "time")]
1201            OwnedPostgresValue::TimeInterval(value) => PostgresValue::TimeInterval(*value),
1202            #[cfg(feature = "jiff")]
1203            OwnedPostgresValue::JiffDate(value) => PostgresValue::JiffDate(*value),
1204            #[cfg(feature = "jiff")]
1205            OwnedPostgresValue::JiffTime(value) => PostgresValue::JiffTime(*value),
1206            #[cfg(feature = "jiff")]
1207            OwnedPostgresValue::JiffDateTime(value) => PostgresValue::JiffDateTime(*value),
1208            #[cfg(feature = "jiff")]
1209            OwnedPostgresValue::JiffTimestamp(value) => PostgresValue::JiffTimestamp(*value),
1210            #[cfg(feature = "cidr")]
1211            OwnedPostgresValue::Inet(value) => PostgresValue::Inet(*value),
1212            #[cfg(feature = "cidr")]
1213            OwnedPostgresValue::Cidr(value) => PostgresValue::Cidr(*value),
1214            #[cfg(feature = "cidr")]
1215            OwnedPostgresValue::MacAddr(value) => PostgresValue::MacAddr(*value),
1216            #[cfg(feature = "cidr")]
1217            OwnedPostgresValue::MacAddr8(value) => PostgresValue::MacAddr8(*value),
1218            #[cfg(feature = "geo-types")]
1219            OwnedPostgresValue::Point(value) => PostgresValue::Point(*value),
1220            #[cfg(feature = "geo-types")]
1221            OwnedPostgresValue::LineString(value) => PostgresValue::LineString(value.clone()),
1222            #[cfg(feature = "geo-types")]
1223            OwnedPostgresValue::Rect(value) => PostgresValue::Rect(*value),
1224            #[cfg(feature = "bit-vec")]
1225            OwnedPostgresValue::BitVec(value) => PostgresValue::BitVec(value.clone()),
1226            OwnedPostgresValue::Array(values) => {
1227                PostgresValue::Array(values.iter().map(PostgresValue::from).collect())
1228            }
1229            OwnedPostgresValue::Enum(enum_val) => PostgresValue::Enum(enum_val.clone()),
1230            OwnedPostgresValue::Null => PostgresValue::Null,
1231        }
1232    }
1233}
1234
1235// Direct conversions from Rust types to OwnedPostgresValue
1236impl From<i16> for OwnedPostgresValue {
1237    fn from(value: i16) -> Self {
1238        Self::Smallint(value)
1239    }
1240}
1241
1242impl From<i32> for OwnedPostgresValue {
1243    fn from(value: i32) -> Self {
1244        Self::Integer(value)
1245    }
1246}
1247
1248impl From<i64> for OwnedPostgresValue {
1249    fn from(value: i64) -> Self {
1250        Self::Bigint(value)
1251    }
1252}
1253
1254impl From<f32> for OwnedPostgresValue {
1255    fn from(value: f32) -> Self {
1256        Self::Real(value)
1257    }
1258}
1259
1260impl From<f64> for OwnedPostgresValue {
1261    fn from(value: f64) -> Self {
1262        Self::DoublePrecision(value)
1263    }
1264}
1265
1266#[cfg(feature = "rust-decimal")]
1267impl From<Decimal> for OwnedPostgresValue {
1268    fn from(value: Decimal) -> Self {
1269        Self::Numeric(value)
1270    }
1271}
1272
1273#[cfg(feature = "rust-decimal")]
1274impl From<&Decimal> for OwnedPostgresValue {
1275    fn from(value: &Decimal) -> Self {
1276        Self::Numeric(*value)
1277    }
1278}
1279
1280impl From<&str> for OwnedPostgresValue {
1281    fn from(value: &str) -> Self {
1282        Self::Text(value.to_string())
1283    }
1284}
1285
1286impl From<&String> for OwnedPostgresValue {
1287    fn from(value: &String) -> Self {
1288        Self::Text(value.clone())
1289    }
1290}
1291
1292impl From<Box<str>> for OwnedPostgresValue {
1293    fn from(value: Box<str>) -> Self {
1294        Self::Text(value.into())
1295    }
1296}
1297
1298impl From<&Box<str>> for OwnedPostgresValue {
1299    fn from(value: &Box<str>) -> Self {
1300        Self::Text(value.as_ref().to_string())
1301    }
1302}
1303
1304impl From<Rc<str>> for OwnedPostgresValue {
1305    fn from(value: Rc<str>) -> Self {
1306        Self::Text(value.as_ref().to_string())
1307    }
1308}
1309
1310impl From<&Rc<str>> for OwnedPostgresValue {
1311    fn from(value: &Rc<str>) -> Self {
1312        Self::Text(value.as_ref().to_string())
1313    }
1314}
1315
1316impl From<Arc<str>> for OwnedPostgresValue {
1317    fn from(value: Arc<str>) -> Self {
1318        Self::Text(value.as_ref().to_string())
1319    }
1320}
1321
1322impl From<&Arc<str>> for OwnedPostgresValue {
1323    fn from(value: &Arc<str>) -> Self {
1324        Self::Text(value.as_ref().to_string())
1325    }
1326}
1327
1328impl From<String> for OwnedPostgresValue {
1329    fn from(value: String) -> Self {
1330        Self::Text(value)
1331    }
1332}
1333
1334impl From<Box<String>> for OwnedPostgresValue {
1335    fn from(value: Box<String>) -> Self {
1336        Self::Text(*value)
1337    }
1338}
1339
1340impl From<&Box<String>> for OwnedPostgresValue {
1341    fn from(value: &Box<String>) -> Self {
1342        Self::Text(value.as_ref().clone())
1343    }
1344}
1345
1346impl From<Rc<String>> for OwnedPostgresValue {
1347    fn from(value: Rc<String>) -> Self {
1348        Self::Text(value.as_ref().clone())
1349    }
1350}
1351
1352impl From<&Rc<String>> for OwnedPostgresValue {
1353    fn from(value: &Rc<String>) -> Self {
1354        Self::Text(value.as_ref().clone())
1355    }
1356}
1357
1358impl From<Arc<String>> for OwnedPostgresValue {
1359    fn from(value: Arc<String>) -> Self {
1360        Self::Text(value.as_ref().clone())
1361    }
1362}
1363
1364impl From<&Arc<String>> for OwnedPostgresValue {
1365    fn from(value: &Arc<String>) -> Self {
1366        Self::Text(value.as_ref().clone())
1367    }
1368}
1369
1370impl From<Vec<u8>> for OwnedPostgresValue {
1371    fn from(value: Vec<u8>) -> Self {
1372        Self::Bytea(value)
1373    }
1374}
1375
1376impl From<Box<Vec<u8>>> for OwnedPostgresValue {
1377    fn from(value: Box<Vec<u8>>) -> Self {
1378        Self::Bytea(*value)
1379    }
1380}
1381
1382impl From<&Box<Vec<u8>>> for OwnedPostgresValue {
1383    fn from(value: &Box<Vec<u8>>) -> Self {
1384        Self::Bytea(value.as_ref().clone())
1385    }
1386}
1387
1388impl From<Rc<Vec<u8>>> for OwnedPostgresValue {
1389    fn from(value: Rc<Vec<u8>>) -> Self {
1390        Self::Bytea(value.as_ref().clone())
1391    }
1392}
1393
1394impl From<&Rc<Vec<u8>>> for OwnedPostgresValue {
1395    fn from(value: &Rc<Vec<u8>>) -> Self {
1396        Self::Bytea(value.as_ref().clone())
1397    }
1398}
1399
1400impl From<Arc<Vec<u8>>> for OwnedPostgresValue {
1401    fn from(value: Arc<Vec<u8>>) -> Self {
1402        Self::Bytea(value.as_ref().clone())
1403    }
1404}
1405
1406impl From<&Arc<Vec<u8>>> for OwnedPostgresValue {
1407    fn from(value: &Arc<Vec<u8>>) -> Self {
1408        Self::Bytea(value.as_ref().clone())
1409    }
1410}
1411
1412impl From<bool> for OwnedPostgresValue {
1413    fn from(value: bool) -> Self {
1414        Self::Boolean(value)
1415    }
1416}
1417
1418#[cfg(feature = "uuid")]
1419impl From<Uuid> for OwnedPostgresValue {
1420    fn from(value: Uuid) -> Self {
1421        Self::Uuid(value)
1422    }
1423}
1424
1425#[cfg(feature = "serde")]
1426impl From<serde_json::Value> for OwnedPostgresValue {
1427    fn from(value: serde_json::Value) -> Self {
1428        Self::Json(value)
1429    }
1430}
1431
1432#[cfg(feature = "time")]
1433impl From<TimeDate> for OwnedPostgresValue {
1434    fn from(value: TimeDate) -> Self {
1435        Self::TimeDate(value)
1436    }
1437}
1438
1439#[cfg(feature = "time")]
1440impl From<TimeTime> for OwnedPostgresValue {
1441    fn from(value: TimeTime) -> Self {
1442        Self::TimeTime(value)
1443    }
1444}
1445
1446#[cfg(feature = "time")]
1447impl From<PrimitiveDateTime> for OwnedPostgresValue {
1448    fn from(value: PrimitiveDateTime) -> Self {
1449        Self::TimeTimestamp(value)
1450    }
1451}
1452
1453#[cfg(feature = "time")]
1454impl From<OffsetDateTime> for OwnedPostgresValue {
1455    fn from(value: OffsetDateTime) -> Self {
1456        Self::TimeTimestampTz(value)
1457    }
1458}
1459
1460#[cfg(feature = "time")]
1461impl From<TimeDuration> for OwnedPostgresValue {
1462    fn from(value: TimeDuration) -> Self {
1463        Self::TimeInterval(value)
1464    }
1465}
1466
1467#[cfg(feature = "jiff")]
1468impl From<JiffDate> for OwnedPostgresValue {
1469    fn from(value: JiffDate) -> Self {
1470        Self::JiffDate(value)
1471    }
1472}
1473
1474#[cfg(feature = "jiff")]
1475impl From<JiffTime> for OwnedPostgresValue {
1476    fn from(value: JiffTime) -> Self {
1477        Self::JiffTime(value)
1478    }
1479}
1480
1481#[cfg(feature = "jiff")]
1482impl From<JiffDateTime> for OwnedPostgresValue {
1483    fn from(value: JiffDateTime) -> Self {
1484        Self::JiffDateTime(value)
1485    }
1486}
1487
1488#[cfg(feature = "jiff")]
1489impl From<JiffTimestamp> for OwnedPostgresValue {
1490    fn from(value: JiffTimestamp) -> Self {
1491        Self::JiffTimestamp(value)
1492    }
1493}
1494
1495#[cfg(feature = "arrayvec")]
1496impl<const N: usize> From<arrayvec::ArrayString<N>> for OwnedPostgresValue {
1497    fn from(value: arrayvec::ArrayString<N>) -> Self {
1498        Self::Text(value.to_string())
1499    }
1500}
1501
1502#[cfg(feature = "compact-str")]
1503impl From<compact_str::CompactString> for OwnedPostgresValue {
1504    fn from(value: compact_str::CompactString) -> Self {
1505        Self::Text(value.to_string())
1506    }
1507}
1508
1509#[cfg(feature = "arrayvec")]
1510impl<const N: usize> From<arrayvec::ArrayVec<u8, N>> for OwnedPostgresValue {
1511    fn from(value: arrayvec::ArrayVec<u8, N>) -> Self {
1512        Self::Bytea(value.to_vec())
1513    }
1514}
1515
1516#[cfg(feature = "bytes")]
1517impl From<bytes::Bytes> for OwnedPostgresValue {
1518    fn from(value: bytes::Bytes) -> Self {
1519        Self::Bytea(value.to_vec())
1520    }
1521}
1522
1523#[cfg(feature = "bytes")]
1524impl From<bytes::BytesMut> for OwnedPostgresValue {
1525    fn from(value: bytes::BytesMut) -> Self {
1526        Self::Bytea(value.to_vec())
1527    }
1528}
1529
1530#[cfg(feature = "smallvec")]
1531impl<const N: usize> From<smallvec::SmallVec<[u8; N]>> for OwnedPostgresValue {
1532    fn from(value: smallvec::SmallVec<[u8; N]>) -> Self {
1533        Self::Bytea(value.into_vec())
1534    }
1535}
1536
1537// TryFrom conversions back to Rust types
1538impl TryFrom<OwnedPostgresValue> for i16 {
1539    type Error = DrizzleError;
1540
1541    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1542        match value {
1543            OwnedPostgresValue::Smallint(i) => Ok(i),
1544            OwnedPostgresValue::Integer(i) => Ok(i.try_into()?),
1545            OwnedPostgresValue::Bigint(i) => Ok(i.try_into()?),
1546            _ => Err(DrizzleError::ConversionError(
1547                format!("Cannot convert {value:?} to i16").into(),
1548            )),
1549        }
1550    }
1551}
1552
1553impl TryFrom<OwnedPostgresValue> for i32 {
1554    type Error = DrizzleError;
1555
1556    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1557        match value {
1558            OwnedPostgresValue::Smallint(i) => Ok(i.into()),
1559            OwnedPostgresValue::Integer(i) => Ok(i),
1560            OwnedPostgresValue::Bigint(i) => Ok(i.try_into()?),
1561            _ => Err(DrizzleError::ConversionError(
1562                format!("Cannot convert {value:?} to i32").into(),
1563            )),
1564        }
1565    }
1566}
1567
1568impl TryFrom<OwnedPostgresValue> for i64 {
1569    type Error = DrizzleError;
1570
1571    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1572        match value {
1573            OwnedPostgresValue::Smallint(i) => Ok(i.into()),
1574            OwnedPostgresValue::Integer(i) => Ok(i.into()),
1575            OwnedPostgresValue::Bigint(i) => Ok(i),
1576            _ => Err(DrizzleError::ConversionError(
1577                format!("Cannot convert {value:?} to i64").into(),
1578            )),
1579        }
1580    }
1581}
1582
1583impl TryFrom<OwnedPostgresValue> for f32 {
1584    type Error = DrizzleError;
1585
1586    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1587        fn parse_float<N: core::fmt::Display>(value: N) -> Result<f32, DrizzleError> {
1588            format!("{value}").parse::<f32>().map_err(|e| {
1589                DrizzleError::ConversionError(format!("Cannot convert to f32: {e}").into())
1590            })
1591        }
1592        match value {
1593            OwnedPostgresValue::Real(r) => Ok(r),
1594            OwnedPostgresValue::DoublePrecision(r) => parse_float(r),
1595            OwnedPostgresValue::Smallint(i) => Ok(Self::from(i)),
1596            OwnedPostgresValue::Integer(i) => parse_float(i),
1597            OwnedPostgresValue::Bigint(i) => parse_float(i),
1598            _ => Err(DrizzleError::ConversionError(
1599                format!("Cannot convert {value:?} to f32").into(),
1600            )),
1601        }
1602    }
1603}
1604
1605impl TryFrom<OwnedPostgresValue> for f64 {
1606    type Error = DrizzleError;
1607
1608    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1609        fn parse_double<N: core::fmt::Display>(value: N) -> Result<f64, DrizzleError> {
1610            format!("{value}").parse::<f64>().map_err(|e| {
1611                DrizzleError::ConversionError(format!("Cannot convert to f64: {e}").into())
1612            })
1613        }
1614        match value {
1615            OwnedPostgresValue::Real(r) => Ok(Self::from(r)),
1616            OwnedPostgresValue::DoublePrecision(r) => Ok(r),
1617            OwnedPostgresValue::Smallint(i) => Ok(Self::from(i)),
1618            OwnedPostgresValue::Integer(i) => Ok(Self::from(i)),
1619            OwnedPostgresValue::Bigint(i) => parse_double(i),
1620            _ => Err(DrizzleError::ConversionError(
1621                format!("Cannot convert {value:?} to f64").into(),
1622            )),
1623        }
1624    }
1625}
1626
1627impl TryFrom<OwnedPostgresValue> for String {
1628    type Error = DrizzleError;
1629
1630    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1631        match value {
1632            OwnedPostgresValue::Text(s) => Ok(s),
1633            _ => Err(DrizzleError::ConversionError(
1634                format!("Cannot convert {value:?} to String").into(),
1635            )),
1636        }
1637    }
1638}
1639
1640impl TryFrom<OwnedPostgresValue> for Box<String> {
1641    type Error = DrizzleError;
1642
1643    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1644        String::try_from(value).map(Self::new)
1645    }
1646}
1647
1648impl TryFrom<OwnedPostgresValue> for Rc<String> {
1649    type Error = DrizzleError;
1650
1651    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1652        String::try_from(value).map(Self::new)
1653    }
1654}
1655
1656impl TryFrom<OwnedPostgresValue> for Arc<String> {
1657    type Error = DrizzleError;
1658
1659    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1660        String::try_from(value).map(Self::new)
1661    }
1662}
1663
1664impl TryFrom<OwnedPostgresValue> for Box<str> {
1665    type Error = DrizzleError;
1666
1667    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1668        match value {
1669            OwnedPostgresValue::Text(s) => Ok(s.into_boxed_str()),
1670            _ => Err(DrizzleError::ConversionError(
1671                format!("Cannot convert {value:?} to Box<str>").into(),
1672            )),
1673        }
1674    }
1675}
1676
1677impl TryFrom<OwnedPostgresValue> for Rc<str> {
1678    type Error = DrizzleError;
1679
1680    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1681        match value {
1682            OwnedPostgresValue::Text(s) => Ok(Self::from(s)),
1683            _ => Err(DrizzleError::ConversionError(
1684                format!("Cannot convert {value:?} to Rc<str>").into(),
1685            )),
1686        }
1687    }
1688}
1689
1690impl TryFrom<OwnedPostgresValue> for Arc<str> {
1691    type Error = DrizzleError;
1692
1693    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1694        match value {
1695            OwnedPostgresValue::Text(s) => Ok(Self::from(s)),
1696            _ => Err(DrizzleError::ConversionError(
1697                format!("Cannot convert {value:?} to Arc<str>").into(),
1698            )),
1699        }
1700    }
1701}
1702
1703#[cfg(feature = "compact-str")]
1704impl TryFrom<OwnedPostgresValue> for compact_str::CompactString {
1705    type Error = DrizzleError;
1706
1707    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1708        String::try_from(value).map(Self::new)
1709    }
1710}
1711
1712impl TryFrom<OwnedPostgresValue> for Vec<u8> {
1713    type Error = DrizzleError;
1714
1715    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1716        match value {
1717            OwnedPostgresValue::Bytea(b) => Ok(b),
1718            _ => Err(DrizzleError::ConversionError(
1719                format!("Cannot convert {value:?} to Vec<u8>").into(),
1720            )),
1721        }
1722    }
1723}
1724
1725impl TryFrom<OwnedPostgresValue> for Box<Vec<u8>> {
1726    type Error = DrizzleError;
1727
1728    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1729        Vec::<u8>::try_from(value).map(Self::new)
1730    }
1731}
1732
1733impl TryFrom<OwnedPostgresValue> for Rc<Vec<u8>> {
1734    type Error = DrizzleError;
1735
1736    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1737        Vec::<u8>::try_from(value).map(Self::new)
1738    }
1739}
1740
1741impl TryFrom<OwnedPostgresValue> for Arc<Vec<u8>> {
1742    type Error = DrizzleError;
1743
1744    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1745        Vec::<u8>::try_from(value).map(Self::new)
1746    }
1747}
1748
1749#[cfg(feature = "bytes")]
1750impl TryFrom<OwnedPostgresValue> for bytes::Bytes {
1751    type Error = DrizzleError;
1752
1753    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1754        Vec::<u8>::try_from(value).map(Self::from)
1755    }
1756}
1757
1758#[cfg(feature = "bytes")]
1759impl TryFrom<OwnedPostgresValue> for bytes::BytesMut {
1760    type Error = DrizzleError;
1761
1762    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1763        Vec::<u8>::try_from(value).map(|v| Self::from(v.as_slice()))
1764    }
1765}
1766
1767#[cfg(feature = "smallvec")]
1768impl<const N: usize> TryFrom<OwnedPostgresValue> for smallvec::SmallVec<[u8; N]> {
1769    type Error = DrizzleError;
1770
1771    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1772        Vec::<u8>::try_from(value).map(|v| {
1773            let mut out = Self::new();
1774            out.extend_from_slice(&v);
1775            out
1776        })
1777    }
1778}
1779
1780impl TryFrom<OwnedPostgresValue> for bool {
1781    type Error = DrizzleError;
1782
1783    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1784        match value {
1785            OwnedPostgresValue::Boolean(b) => Ok(b),
1786            _ => Err(DrizzleError::ConversionError(
1787                format!("Cannot convert {value:?} to bool").into(),
1788            )),
1789        }
1790    }
1791}
1792
1793#[cfg(feature = "uuid")]
1794impl TryFrom<OwnedPostgresValue> for Uuid {
1795    type Error = DrizzleError;
1796
1797    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1798        match value {
1799            OwnedPostgresValue::Uuid(uuid) => Ok(uuid),
1800            OwnedPostgresValue::Text(s) => Self::parse_str(&s).map_err(|e| {
1801                DrizzleError::ConversionError(format!("Failed to parse UUID: {e}").into())
1802            }),
1803            _ => Err(DrizzleError::ConversionError(
1804                format!("Cannot convert {value:?} to UUID").into(),
1805            )),
1806        }
1807    }
1808}
1809
1810#[cfg(feature = "serde")]
1811impl TryFrom<OwnedPostgresValue> for serde_json::Value {
1812    type Error = DrizzleError;
1813
1814    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1815        match value {
1816            OwnedPostgresValue::Json(json) | OwnedPostgresValue::Jsonb(json) => Ok(json),
1817            OwnedPostgresValue::Text(s) => serde_json::from_str(&s).map_err(|e| {
1818                DrizzleError::ConversionError(format!("Failed to parse JSON: {e}").into())
1819            }),
1820            _ => Err(DrizzleError::ConversionError(
1821                format!("Cannot convert {value:?} to JSON").into(),
1822            )),
1823        }
1824    }
1825}
1826
1827#[cfg(feature = "arrayvec")]
1828impl<const N: usize> TryFrom<OwnedPostgresValue> for arrayvec::ArrayString<N> {
1829    type Error = DrizzleError;
1830
1831    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1832        match value {
1833            OwnedPostgresValue::Text(s) => Self::from(&s).map_err(|_| {
1834                DrizzleError::ConversionError(
1835                    format!("Text length {} exceeds ArrayString capacity {}", s.len(), N).into(),
1836                )
1837            }),
1838            _ => Err(DrizzleError::ConversionError(
1839                format!("Cannot convert {value:?} to ArrayString").into(),
1840            )),
1841        }
1842    }
1843}
1844
1845#[cfg(feature = "arrayvec")]
1846impl<const N: usize> TryFrom<OwnedPostgresValue> for arrayvec::ArrayVec<u8, N> {
1847    type Error = DrizzleError;
1848
1849    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1850        match value {
1851            OwnedPostgresValue::Bytea(bytes) => Self::try_from(bytes.as_slice()).map_err(|_| {
1852                DrizzleError::ConversionError(
1853                    format!(
1854                        "Bytea length {} exceeds ArrayVec capacity {}",
1855                        bytes.len(),
1856                        N
1857                    )
1858                    .into(),
1859                )
1860            }),
1861            _ => Err(DrizzleError::ConversionError(
1862                format!("Cannot convert {value:?} to ArrayVec<u8>").into(),
1863            )),
1864        }
1865    }
1866}
1867
1868#[cfg(feature = "time")]
1869impl TryFrom<OwnedPostgresValue> for TimeDate {
1870    type Error = DrizzleError;
1871
1872    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1873        match value {
1874            OwnedPostgresValue::TimeDate(date) => Ok(date),
1875            OwnedPostgresValue::TimeTimestamp(ts) => Ok(ts.date()),
1876            OwnedPostgresValue::TimeTimestampTz(ts) => Ok(ts.date()),
1877            _ => Err(DrizzleError::ConversionError(
1878                format!("Cannot convert {value:?} to time::Date").into(),
1879            )),
1880        }
1881    }
1882}
1883
1884#[cfg(feature = "time")]
1885impl TryFrom<OwnedPostgresValue> for TimeTime {
1886    type Error = DrizzleError;
1887
1888    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1889        match value {
1890            OwnedPostgresValue::TimeTime(time) => Ok(time),
1891            OwnedPostgresValue::TimeTimestamp(ts) => Ok(ts.time()),
1892            OwnedPostgresValue::TimeTimestampTz(ts) => Ok(ts.time()),
1893            _ => Err(DrizzleError::ConversionError(
1894                format!("Cannot convert {value:?} to time::Time").into(),
1895            )),
1896        }
1897    }
1898}
1899
1900#[cfg(feature = "time")]
1901impl TryFrom<OwnedPostgresValue> for PrimitiveDateTime {
1902    type Error = DrizzleError;
1903
1904    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1905        match value {
1906            OwnedPostgresValue::TimeTimestamp(ts) => Ok(ts),
1907            _ => Err(DrizzleError::ConversionError(
1908                format!("Cannot convert {value:?} to time::PrimitiveDateTime").into(),
1909            )),
1910        }
1911    }
1912}
1913
1914#[cfg(feature = "time")]
1915impl TryFrom<OwnedPostgresValue> for OffsetDateTime {
1916    type Error = DrizzleError;
1917
1918    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1919        match value {
1920            OwnedPostgresValue::TimeTimestampTz(ts) => Ok(ts),
1921            _ => Err(DrizzleError::ConversionError(
1922                format!("Cannot convert {value:?} to time::OffsetDateTime").into(),
1923            )),
1924        }
1925    }
1926}
1927
1928#[cfg(feature = "time")]
1929impl TryFrom<OwnedPostgresValue> for TimeDuration {
1930    type Error = DrizzleError;
1931
1932    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1933        match value {
1934            OwnedPostgresValue::TimeInterval(dur) => Ok(dur),
1935            _ => Err(DrizzleError::ConversionError(
1936                format!("Cannot convert {value:?} to time::Duration").into(),
1937            )),
1938        }
1939    }
1940}
1941
1942#[cfg(feature = "jiff")]
1943impl TryFrom<OwnedPostgresValue> for JiffDate {
1944    type Error = DrizzleError;
1945
1946    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1947        match value {
1948            OwnedPostgresValue::JiffDate(date) => Ok(date),
1949            OwnedPostgresValue::JiffDateTime(ts) => Ok(ts.date()),
1950            OwnedPostgresValue::JiffTimestamp(ts) => {
1951                Ok(jiff::tz::Offset::UTC.to_datetime(ts).date())
1952            }
1953            _ => Err(DrizzleError::ConversionError(
1954                format!("Cannot convert {value:?} to jiff::civil::Date").into(),
1955            )),
1956        }
1957    }
1958}
1959
1960#[cfg(feature = "jiff")]
1961impl TryFrom<OwnedPostgresValue> for JiffTime {
1962    type Error = DrizzleError;
1963
1964    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1965        match value {
1966            OwnedPostgresValue::JiffTime(time) => Ok(time),
1967            OwnedPostgresValue::JiffDateTime(ts) => Ok(ts.time()),
1968            OwnedPostgresValue::JiffTimestamp(ts) => {
1969                Ok(jiff::tz::Offset::UTC.to_datetime(ts).time())
1970            }
1971            _ => Err(DrizzleError::ConversionError(
1972                format!("Cannot convert {value:?} to jiff::civil::Time").into(),
1973            )),
1974        }
1975    }
1976}
1977
1978#[cfg(feature = "jiff")]
1979impl TryFrom<OwnedPostgresValue> for JiffDateTime {
1980    type Error = DrizzleError;
1981
1982    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1983        match value {
1984            OwnedPostgresValue::JiffDateTime(ts) => Ok(ts),
1985            _ => Err(DrizzleError::ConversionError(
1986                format!("Cannot convert {value:?} to jiff::civil::DateTime").into(),
1987            )),
1988        }
1989    }
1990}
1991
1992#[cfg(feature = "jiff")]
1993impl TryFrom<OwnedPostgresValue> for JiffTimestamp {
1994    type Error = DrizzleError;
1995
1996    fn try_from(value: OwnedPostgresValue) -> Result<Self, Self::Error> {
1997        match value {
1998            OwnedPostgresValue::JiffTimestamp(ts) => Ok(ts),
1999            _ => Err(DrizzleError::ConversionError(
2000                format!("Cannot convert {value:?} to jiff::Timestamp").into(),
2001            )),
2002        }
2003    }
2004}