Skip to main content

drizzle_postgres/values/
owned.rs

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