Skip to main content

drizzle_sqlite/values/
conversions.rs

1//! From and `TryFrom` implementations for `SQLiteValue`
2
3use super::{OwnedSQLiteValue, SQLiteValue};
4use crate::prelude::*;
5use drizzle_core::{error::DrizzleError, sql::SQL, traits::ToSQL};
6
7#[cfg(feature = "uuid")]
8use uuid::Uuid;
9
10//------------------------------------------------------------------------------
11// ToSQL Implementation
12//------------------------------------------------------------------------------
13
14impl<'a> ToSQL<'a, Self> for SQLiteValue<'a> {
15    fn to_sql(&self) -> SQL<'a, Self> {
16        SQL::param(self.clone())
17    }
18}
19
20//------------------------------------------------------------------------------
21// From OwnedSQLiteValue
22//------------------------------------------------------------------------------
23
24impl From<OwnedSQLiteValue> for SQLiteValue<'_> {
25    fn from(value: OwnedSQLiteValue) -> Self {
26        match value {
27            OwnedSQLiteValue::Integer(f) => SQLiteValue::Integer(f),
28            OwnedSQLiteValue::Real(r) => SQLiteValue::Real(r),
29            OwnedSQLiteValue::Text(v) => SQLiteValue::Text(Cow::Owned(v)),
30            OwnedSQLiteValue::Blob(v) => SQLiteValue::Blob(Cow::Owned(v.into())),
31            OwnedSQLiteValue::Null => SQLiteValue::Null,
32        }
33    }
34}
35
36impl<'a> From<&'a OwnedSQLiteValue> for SQLiteValue<'a> {
37    fn from(value: &'a OwnedSQLiteValue) -> Self {
38        match value {
39            OwnedSQLiteValue::Integer(f) => SQLiteValue::Integer(*f),
40            OwnedSQLiteValue::Real(r) => SQLiteValue::Real(*r),
41            OwnedSQLiteValue::Text(v) => SQLiteValue::Text(Cow::Borrowed(v)),
42            OwnedSQLiteValue::Blob(v) => SQLiteValue::Blob(Cow::Borrowed(v)),
43            OwnedSQLiteValue::Null => SQLiteValue::Null,
44        }
45    }
46}
47
48impl<'a> From<&'a Self> for SQLiteValue<'a> {
49    fn from(value: &'a Self) -> Self {
50        match value {
51            SQLiteValue::Integer(f) => SQLiteValue::Integer(*f),
52            SQLiteValue::Real(r) => SQLiteValue::Real(*r),
53            SQLiteValue::Text(v) => SQLiteValue::Text(Cow::Borrowed(v)),
54            SQLiteValue::Blob(v) => SQLiteValue::Blob(Cow::Borrowed(v)),
55            SQLiteValue::Null => SQLiteValue::Null,
56        }
57    }
58}
59
60impl<'a> From<Cow<'a, Self>> for SQLiteValue<'a> {
61    fn from(value: Cow<'a, Self>) -> Self {
62        match value {
63            Cow::Borrowed(r) => r.into(),
64            Cow::Owned(o) => o,
65        }
66    }
67}
68
69impl<'a> From<&'a Cow<'a, Self>> for SQLiteValue<'a> {
70    fn from(value: &'a Cow<'a, Self>) -> Self {
71        match value {
72            Cow::Borrowed(r) => (*r).into(),
73            Cow::Owned(o) => o.into(),
74        }
75    }
76}
77
78//------------------------------------------------------------------------------
79// From<T> implementations
80// Macro-based to reduce boilerplate
81//------------------------------------------------------------------------------
82
83/// Integer widths where `i64::from(T)` is infallible.
84macro_rules! impl_from_lossless_int_for_sqlite_value {
85    ($($ty:ty),* $(,)?) => {
86        $(
87            impl<'a> From<$ty> for SQLiteValue<'a> {
88                #[inline]
89                fn from(value: $ty) -> Self {
90                    SQLiteValue::Integer(i64::from(value))
91                }
92            }
93
94            impl<'a> From<&$ty> for SQLiteValue<'a> {
95                #[inline]
96                fn from(value: &$ty) -> Self {
97                    SQLiteValue::Integer(i64::from(*value))
98                }
99            }
100        )*
101    };
102}
103
104// Types that widen to i64 without loss.
105impl_from_lossless_int_for_sqlite_value!(i8, i16, i32, u8, u16, u32, bool);
106
107// i64 identity — no conversion needed.
108impl From<i64> for SQLiteValue<'_> {
109    #[inline]
110    fn from(value: i64) -> Self {
111        SQLiteValue::Integer(value)
112    }
113}
114
115impl From<&i64> for SQLiteValue<'_> {
116    #[inline]
117    fn from(value: &i64) -> Self {
118        SQLiteValue::Integer(*value)
119    }
120}
121
122// u64 → i64 by reinterpreting bits. SQLite stores INTEGER as signed 64-bit;
123// this matches the round-trip convention used by the matching `i64 → u64`
124// reinterpretation on read.
125impl From<u64> for SQLiteValue<'_> {
126    #[inline]
127    fn from(value: u64) -> Self {
128        SQLiteValue::Integer(value.cast_signed())
129    }
130}
131
132impl From<&u64> for SQLiteValue<'_> {
133    #[inline]
134    fn from(value: &u64) -> Self {
135        SQLiteValue::Integer(value.cast_signed())
136    }
137}
138
139/// Pointer-sized integer widths (usize/isize). All Rust-supported targets have
140/// pointers ≤ 64 bits, so `i64::try_from` succeeds; the saturating fallback is
141/// defensive only.
142macro_rules! impl_from_pointer_int_for_sqlite_value {
143    ($($ty:ty),* $(,)?) => {
144        $(
145            impl<'a> From<$ty> for SQLiteValue<'a> {
146                #[inline]
147                fn from(value: $ty) -> Self {
148                    SQLiteValue::Integer(i64::try_from(value).unwrap_or(i64::MAX))
149                }
150            }
151
152            impl<'a> From<&$ty> for SQLiteValue<'a> {
153                #[inline]
154                fn from(value: &$ty) -> Self {
155                    SQLiteValue::Integer(i64::try_from(*value).unwrap_or(i64::MAX))
156                }
157            }
158        )*
159    };
160}
161
162impl_from_pointer_int_for_sqlite_value!(isize, usize);
163
164// f32 widens exactly into f64.
165impl From<f32> for SQLiteValue<'_> {
166    #[inline]
167    fn from(value: f32) -> Self {
168        SQLiteValue::Real(f64::from(value))
169    }
170}
171
172impl From<&f32> for SQLiteValue<'_> {
173    #[inline]
174    fn from(value: &f32) -> Self {
175        SQLiteValue::Real(f64::from(*value))
176    }
177}
178
179// f64 identity.
180impl From<f64> for SQLiteValue<'_> {
181    #[inline]
182    fn from(value: f64) -> Self {
183        SQLiteValue::Real(value)
184    }
185}
186
187impl From<&f64> for SQLiteValue<'_> {
188    #[inline]
189    fn from(value: &f64) -> Self {
190        SQLiteValue::Real(*value)
191    }
192}
193
194// --- String Types ---
195
196impl<'a> From<&'a str> for SQLiteValue<'a> {
197    fn from(value: &'a str) -> Self {
198        SQLiteValue::Text(Cow::Borrowed(value))
199    }
200}
201
202impl<'a> From<Cow<'a, str>> for SQLiteValue<'a> {
203    fn from(value: Cow<'a, str>) -> Self {
204        SQLiteValue::Text(value)
205    }
206}
207
208impl From<String> for SQLiteValue<'_> {
209    fn from(value: String) -> Self {
210        SQLiteValue::Text(Cow::Owned(value))
211    }
212}
213
214impl<'a> From<&'a String> for SQLiteValue<'a> {
215    fn from(value: &'a String) -> Self {
216        SQLiteValue::Text(Cow::Borrowed(value))
217    }
218}
219
220impl From<Box<String>> for SQLiteValue<'_> {
221    fn from(value: Box<String>) -> Self {
222        SQLiteValue::Text(Cow::Owned(*value))
223    }
224}
225
226impl<'a> From<&'a Box<String>> for SQLiteValue<'a> {
227    fn from(value: &'a Box<String>) -> Self {
228        SQLiteValue::Text(Cow::Borrowed(value.as_str()))
229    }
230}
231
232impl From<Rc<String>> for SQLiteValue<'_> {
233    fn from(value: Rc<String>) -> Self {
234        SQLiteValue::Text(Cow::Owned(value.as_ref().clone()))
235    }
236}
237
238impl<'a> From<&'a Rc<String>> for SQLiteValue<'a> {
239    fn from(value: &'a Rc<String>) -> Self {
240        SQLiteValue::Text(Cow::Borrowed(value.as_str()))
241    }
242}
243
244impl From<Arc<String>> for SQLiteValue<'_> {
245    fn from(value: Arc<String>) -> Self {
246        SQLiteValue::Text(Cow::Owned(value.as_ref().clone()))
247    }
248}
249
250impl<'a> From<&'a Arc<String>> for SQLiteValue<'a> {
251    fn from(value: &'a Arc<String>) -> Self {
252        SQLiteValue::Text(Cow::Borrowed(value.as_str()))
253    }
254}
255
256impl From<Box<str>> for SQLiteValue<'_> {
257    fn from(value: Box<str>) -> Self {
258        SQLiteValue::Text(Cow::Owned(value.into()))
259    }
260}
261
262impl<'a> From<&'a Box<str>> for SQLiteValue<'a> {
263    fn from(value: &'a Box<str>) -> Self {
264        SQLiteValue::Text(Cow::Borrowed(value.as_ref()))
265    }
266}
267
268impl From<Rc<str>> for SQLiteValue<'_> {
269    fn from(value: Rc<str>) -> Self {
270        SQLiteValue::Text(Cow::Owned(value.as_ref().to_string()))
271    }
272}
273
274impl<'a> From<&'a Rc<str>> for SQLiteValue<'a> {
275    fn from(value: &'a Rc<str>) -> Self {
276        SQLiteValue::Text(Cow::Borrowed(value.as_ref()))
277    }
278}
279
280impl From<Arc<str>> for SQLiteValue<'_> {
281    fn from(value: Arc<str>) -> Self {
282        SQLiteValue::Text(Cow::Owned(value.as_ref().to_string()))
283    }
284}
285
286impl<'a> From<&'a Arc<str>> for SQLiteValue<'a> {
287    fn from(value: &'a Arc<str>) -> Self {
288        SQLiteValue::Text(Cow::Borrowed(value.as_ref()))
289    }
290}
291
292// --- ArrayString ---
293
294#[cfg(feature = "arrayvec")]
295impl<const N: usize> From<arrayvec::ArrayString<N>> for SQLiteValue<'_> {
296    fn from(value: arrayvec::ArrayString<N>) -> Self {
297        SQLiteValue::Text(Cow::Owned(value.to_string()))
298    }
299}
300
301#[cfg(feature = "arrayvec")]
302impl<const N: usize> From<&arrayvec::ArrayString<N>> for SQLiteValue<'_> {
303    fn from(value: &arrayvec::ArrayString<N>) -> Self {
304        SQLiteValue::Text(Cow::Owned(String::from(value.as_str())))
305    }
306}
307
308impl From<compact_str::CompactString> for SQLiteValue<'_> {
309    fn from(value: compact_str::CompactString) -> Self {
310        SQLiteValue::Text(Cow::Owned(value.to_string()))
311    }
312}
313
314impl<'a> From<&'a compact_str::CompactString> for SQLiteValue<'a> {
315    fn from(value: &'a compact_str::CompactString) -> Self {
316        SQLiteValue::Text(Cow::Borrowed(value.as_str()))
317    }
318}
319
320// --- Binary Data ---
321
322impl<'a> From<&'a [u8]> for SQLiteValue<'a> {
323    fn from(value: &'a [u8]) -> Self {
324        SQLiteValue::Blob(Cow::Borrowed(value))
325    }
326}
327
328impl<'a> From<Cow<'a, [u8]>> for SQLiteValue<'a> {
329    fn from(value: Cow<'a, [u8]>) -> Self {
330        SQLiteValue::Blob(value)
331    }
332}
333
334impl From<Vec<u8>> for SQLiteValue<'_> {
335    fn from(value: Vec<u8>) -> Self {
336        SQLiteValue::Blob(Cow::Owned(value))
337    }
338}
339
340impl From<Box<Vec<u8>>> for SQLiteValue<'_> {
341    fn from(value: Box<Vec<u8>>) -> Self {
342        SQLiteValue::Blob(Cow::Owned(*value))
343    }
344}
345
346impl<'a> From<&'a Box<Vec<u8>>> for SQLiteValue<'a> {
347    fn from(value: &'a Box<Vec<u8>>) -> Self {
348        SQLiteValue::Blob(Cow::Borrowed(value.as_slice()))
349    }
350}
351
352impl From<Rc<Vec<u8>>> for SQLiteValue<'_> {
353    fn from(value: Rc<Vec<u8>>) -> Self {
354        SQLiteValue::Blob(Cow::Owned(value.as_ref().clone()))
355    }
356}
357
358impl<'a> From<&'a Rc<Vec<u8>>> for SQLiteValue<'a> {
359    fn from(value: &'a Rc<Vec<u8>>) -> Self {
360        SQLiteValue::Blob(Cow::Borrowed(value.as_slice()))
361    }
362}
363
364impl From<Arc<Vec<u8>>> for SQLiteValue<'_> {
365    fn from(value: Arc<Vec<u8>>) -> Self {
366        SQLiteValue::Blob(Cow::Owned(value.as_ref().clone()))
367    }
368}
369
370impl<'a> From<&'a Arc<Vec<u8>>> for SQLiteValue<'a> {
371    fn from(value: &'a Arc<Vec<u8>>) -> Self {
372        SQLiteValue::Blob(Cow::Borrowed(value.as_slice()))
373    }
374}
375
376// --- ArrayVec<u8, N> ---
377
378#[cfg(feature = "arrayvec")]
379impl<const N: usize> From<arrayvec::ArrayVec<u8, N>> for SQLiteValue<'_> {
380    fn from(value: arrayvec::ArrayVec<u8, N>) -> Self {
381        SQLiteValue::Blob(Cow::Owned(value.to_vec()))
382    }
383}
384
385#[cfg(feature = "arrayvec")]
386impl<const N: usize> From<&arrayvec::ArrayVec<u8, N>> for SQLiteValue<'_> {
387    fn from(value: &arrayvec::ArrayVec<u8, N>) -> Self {
388        SQLiteValue::Blob(Cow::Owned(value.to_vec()))
389    }
390}
391
392#[cfg(feature = "bytes")]
393impl From<bytes::Bytes> for SQLiteValue<'_> {
394    fn from(value: bytes::Bytes) -> Self {
395        SQLiteValue::Blob(Cow::Owned(value.to_vec()))
396    }
397}
398
399#[cfg(feature = "bytes")]
400impl<'a> From<&'a bytes::Bytes> for SQLiteValue<'a> {
401    fn from(value: &'a bytes::Bytes) -> Self {
402        SQLiteValue::Blob(Cow::Owned(value.to_vec()))
403    }
404}
405
406#[cfg(feature = "bytes")]
407impl From<bytes::BytesMut> for SQLiteValue<'_> {
408    fn from(value: bytes::BytesMut) -> Self {
409        SQLiteValue::Blob(Cow::Owned(value.to_vec()))
410    }
411}
412
413#[cfg(feature = "bytes")]
414impl<'a> From<&'a bytes::BytesMut> for SQLiteValue<'a> {
415    fn from(value: &'a bytes::BytesMut) -> Self {
416        SQLiteValue::Blob(Cow::Owned(value.to_vec()))
417    }
418}
419
420#[cfg(feature = "smallvec")]
421impl<const N: usize> From<smallvec::SmallVec<[u8; N]>> for SQLiteValue<'_> {
422    fn from(value: smallvec::SmallVec<[u8; N]>) -> Self {
423        SQLiteValue::Blob(Cow::Owned(value.into_vec()))
424    }
425}
426
427#[cfg(feature = "smallvec")]
428impl<const N: usize> From<&smallvec::SmallVec<[u8; N]>> for SQLiteValue<'_> {
429    fn from(value: &smallvec::SmallVec<[u8; N]>) -> Self {
430        SQLiteValue::Blob(Cow::Owned(value.to_vec()))
431    }
432}
433
434// --- Chrono Date/Time Types (stored as ISO-8601 text) ---
435
436#[cfg(feature = "chrono")]
437impl From<chrono::NaiveDate> for SQLiteValue<'_> {
438    fn from(value: chrono::NaiveDate) -> Self {
439        SQLiteValue::Text(Cow::Owned(value.to_string()))
440    }
441}
442
443#[cfg(feature = "chrono")]
444impl From<&chrono::NaiveDate> for SQLiteValue<'_> {
445    fn from(value: &chrono::NaiveDate) -> Self {
446        SQLiteValue::Text(Cow::Owned(value.to_string()))
447    }
448}
449
450#[cfg(feature = "chrono")]
451impl From<chrono::NaiveTime> for SQLiteValue<'_> {
452    fn from(value: chrono::NaiveTime) -> Self {
453        SQLiteValue::Text(Cow::Owned(value.to_string()))
454    }
455}
456
457#[cfg(feature = "chrono")]
458impl From<&chrono::NaiveTime> for SQLiteValue<'_> {
459    fn from(value: &chrono::NaiveTime) -> Self {
460        SQLiteValue::Text(Cow::Owned(value.to_string()))
461    }
462}
463
464#[cfg(feature = "chrono")]
465impl From<chrono::NaiveDateTime> for SQLiteValue<'_> {
466    fn from(value: chrono::NaiveDateTime) -> Self {
467        SQLiteValue::Text(Cow::Owned(value.to_string()))
468    }
469}
470
471#[cfg(feature = "chrono")]
472impl From<&chrono::NaiveDateTime> for SQLiteValue<'_> {
473    fn from(value: &chrono::NaiveDateTime) -> Self {
474        SQLiteValue::Text(Cow::Owned(value.to_string()))
475    }
476}
477
478#[cfg(feature = "chrono")]
479impl From<chrono::DateTime<chrono::FixedOffset>> for SQLiteValue<'_> {
480    fn from(value: chrono::DateTime<chrono::FixedOffset>) -> Self {
481        SQLiteValue::Text(Cow::Owned(value.to_rfc3339()))
482    }
483}
484
485#[cfg(feature = "chrono")]
486impl From<&chrono::DateTime<chrono::FixedOffset>> for SQLiteValue<'_> {
487    fn from(value: &chrono::DateTime<chrono::FixedOffset>) -> Self {
488        SQLiteValue::Text(Cow::Owned(value.to_rfc3339()))
489    }
490}
491
492#[cfg(feature = "chrono")]
493impl From<chrono::DateTime<chrono::Utc>> for SQLiteValue<'_> {
494    fn from(value: chrono::DateTime<chrono::Utc>) -> Self {
495        SQLiteValue::Text(Cow::Owned(value.to_rfc3339()))
496    }
497}
498
499#[cfg(feature = "chrono")]
500impl From<&chrono::DateTime<chrono::Utc>> for SQLiteValue<'_> {
501    fn from(value: &chrono::DateTime<chrono::Utc>) -> Self {
502        SQLiteValue::Text(Cow::Owned(value.to_rfc3339()))
503    }
504}
505
506#[cfg(feature = "chrono")]
507impl From<chrono::Duration> for SQLiteValue<'_> {
508    fn from(value: chrono::Duration) -> Self {
509        SQLiteValue::Text(Cow::Owned(value.to_string()))
510    }
511}
512
513#[cfg(feature = "chrono")]
514impl From<&chrono::Duration> for SQLiteValue<'_> {
515    fn from(value: &chrono::Duration) -> Self {
516        SQLiteValue::Text(Cow::Owned(value.to_string()))
517    }
518}
519
520// --- Time crate Date/Time Types (stored as ISO-8601 text) ---
521
522#[cfg(feature = "time")]
523impl From<time::Date> for SQLiteValue<'_> {
524    fn from(value: time::Date) -> Self {
525        SQLiteValue::Text(Cow::Owned(
526            value
527                .format(&time::format_description::well_known::Iso8601::DATE)
528                .unwrap_or_default(),
529        ))
530    }
531}
532
533#[cfg(feature = "time")]
534impl From<&time::Date> for SQLiteValue<'_> {
535    fn from(value: &time::Date) -> Self {
536        SQLiteValue::Text(Cow::Owned(
537            value
538                .format(&time::format_description::well_known::Iso8601::DATE)
539                .unwrap_or_default(),
540        ))
541    }
542}
543
544#[cfg(feature = "time")]
545impl From<time::Time> for SQLiteValue<'_> {
546    fn from(value: time::Time) -> Self {
547        SQLiteValue::Text(Cow::Owned(
548            value
549                .format(&time::format_description::well_known::Iso8601::TIME)
550                .unwrap_or_default(),
551        ))
552    }
553}
554
555#[cfg(feature = "time")]
556impl From<&time::Time> for SQLiteValue<'_> {
557    fn from(value: &time::Time) -> Self {
558        SQLiteValue::Text(Cow::Owned(
559            value
560                .format(&time::format_description::well_known::Iso8601::TIME)
561                .unwrap_or_default(),
562        ))
563    }
564}
565
566#[cfg(feature = "time")]
567impl From<time::PrimitiveDateTime> for SQLiteValue<'_> {
568    fn from(value: time::PrimitiveDateTime) -> Self {
569        SQLiteValue::Text(Cow::Owned(
570            value
571                .format(&time::format_description::well_known::Iso8601::DATE_TIME)
572                .unwrap_or_default(),
573        ))
574    }
575}
576
577#[cfg(feature = "time")]
578impl From<&time::PrimitiveDateTime> for SQLiteValue<'_> {
579    fn from(value: &time::PrimitiveDateTime) -> Self {
580        SQLiteValue::Text(Cow::Owned(
581            value
582                .format(&time::format_description::well_known::Iso8601::DATE_TIME)
583                .unwrap_or_default(),
584        ))
585    }
586}
587
588#[cfg(feature = "time")]
589impl From<time::OffsetDateTime> for SQLiteValue<'_> {
590    fn from(value: time::OffsetDateTime) -> Self {
591        SQLiteValue::Text(Cow::Owned(
592            value
593                .format(&time::format_description::well_known::Rfc3339)
594                .unwrap_or_default(),
595        ))
596    }
597}
598
599#[cfg(feature = "time")]
600impl From<&time::OffsetDateTime> for SQLiteValue<'_> {
601    fn from(value: &time::OffsetDateTime) -> Self {
602        SQLiteValue::Text(Cow::Owned(
603            value
604                .format(&time::format_description::well_known::Rfc3339)
605                .unwrap_or_default(),
606        ))
607    }
608}
609
610#[cfg(feature = "time")]
611impl From<time::Duration> for SQLiteValue<'_> {
612    fn from(value: time::Duration) -> Self {
613        SQLiteValue::Text(Cow::Owned(format!("{}s", value.whole_seconds())))
614    }
615}
616
617#[cfg(feature = "time")]
618impl From<&time::Duration> for SQLiteValue<'_> {
619    fn from(value: &time::Duration) -> Self {
620        SQLiteValue::Text(Cow::Owned(format!("{}s", value.whole_seconds())))
621    }
622}
623
624// --- Decimal (stored as text for lossless round-trip) ---
625
626#[cfg(feature = "rust-decimal")]
627impl From<rust_decimal::Decimal> for SQLiteValue<'_> {
628    fn from(value: rust_decimal::Decimal) -> Self {
629        SQLiteValue::Text(Cow::Owned(value.to_string()))
630    }
631}
632
633#[cfg(feature = "rust-decimal")]
634impl From<&rust_decimal::Decimal> for SQLiteValue<'_> {
635    fn from(value: &rust_decimal::Decimal) -> Self {
636        SQLiteValue::Text(Cow::Owned(value.to_string()))
637    }
638}
639
640// --- JSON ---
641
642#[cfg(feature = "serde")]
643impl From<serde_json::Value> for SQLiteValue<'_> {
644    fn from(value: serde_json::Value) -> Self {
645        SQLiteValue::Text(Cow::Owned(value.to_string()))
646    }
647}
648
649#[cfg(feature = "serde")]
650impl From<&serde_json::Value> for SQLiteValue<'_> {
651    fn from(value: &serde_json::Value) -> Self {
652        SQLiteValue::Text(Cow::Owned(value.to_string()))
653    }
654}
655
656// --- UUID ---
657
658#[cfg(feature = "uuid")]
659impl From<Uuid> for SQLiteValue<'_> {
660    fn from(value: Uuid) -> Self {
661        SQLiteValue::Blob(Cow::Owned(value.as_bytes().to_vec()))
662    }
663}
664
665#[cfg(feature = "uuid")]
666impl<'a> From<&'a Uuid> for SQLiteValue<'a> {
667    fn from(value: &'a Uuid) -> Self {
668        SQLiteValue::Blob(Cow::Borrowed(value.as_bytes()))
669    }
670}
671
672// --- Option Types ---
673impl<T> From<Option<T>> for SQLiteValue<'_>
674where
675    T: TryInto<Self>,
676{
677    fn from(value: Option<T>) -> Self {
678        value.map_or(SQLiteValue::Null, |v| {
679            v.try_into().unwrap_or(SQLiteValue::Null)
680        })
681    }
682}
683
684// --- Cow integration for SQL struct ---
685impl<'a> From<SQLiteValue<'a>> for Cow<'a, SQLiteValue<'a>> {
686    fn from(value: SQLiteValue<'a>) -> Self {
687        Cow::Owned(value)
688    }
689}
690
691impl<'a> From<&'a SQLiteValue<'a>> for Cow<'a, SQLiteValue<'a>> {
692    fn from(value: &'a SQLiteValue<'a>) -> Self {
693        Cow::Borrowed(value)
694    }
695}
696
697//------------------------------------------------------------------------------
698// TryFrom<SQLiteValue> implementations
699// Uses the FromSQLiteValue trait via convert() for unified conversion logic
700//------------------------------------------------------------------------------
701
702/// Macro to implement `TryFrom`<SQLiteValue> for types implementing `FromSQLiteValue`
703macro_rules! impl_try_from_sqlite_value {
704    ($($ty:ty),* $(,)?) => {
705        $(
706            impl<'a> TryFrom<SQLiteValue<'a>> for $ty {
707                type Error = DrizzleError;
708
709                #[inline]
710                fn try_from(value: SQLiteValue<'a>) -> Result<Self, Self::Error> {
711                    value.convert()
712                }
713            }
714        )*
715    };
716}
717
718impl_try_from_sqlite_value!(
719    i8,
720    i16,
721    i32,
722    i64,
723    isize,
724    u8,
725    u16,
726    u32,
727    u64,
728    usize,
729    f32,
730    f64,
731    bool,
732    String,
733    Box<String>,
734    Rc<String>,
735    Arc<String>,
736    Box<str>,
737    Rc<str>,
738    Arc<str>,
739    Box<Vec<u8>>,
740    Rc<Vec<u8>>,
741    Arc<Vec<u8>>,
742    Vec<u8>,
743    compact_str::CompactString,
744);
745
746#[cfg(feature = "uuid")]
747impl_try_from_sqlite_value!(Uuid);
748
749#[cfg(feature = "chrono")]
750impl_try_from_sqlite_value!(
751    chrono::NaiveDate,
752    chrono::NaiveTime,
753    chrono::NaiveDateTime,
754    chrono::DateTime<chrono::FixedOffset>,
755    chrono::DateTime<chrono::Utc>,
756    chrono::Duration,
757);
758
759#[cfg(feature = "time")]
760impl_try_from_sqlite_value!(
761    time::Date,
762    time::Time,
763    time::PrimitiveDateTime,
764    time::OffsetDateTime,
765    time::Duration,
766);
767
768#[cfg(feature = "rust-decimal")]
769impl_try_from_sqlite_value!(rust_decimal::Decimal);
770
771#[cfg(feature = "serde")]
772impl_try_from_sqlite_value!(serde_json::Value);
773
774#[cfg(feature = "bytes")]
775impl_try_from_sqlite_value!(bytes::Bytes, bytes::BytesMut);
776
777#[cfg(feature = "smallvec")]
778impl<const N: usize> TryFrom<SQLiteValue<'_>> for smallvec::SmallVec<[u8; N]> {
779    type Error = DrizzleError;
780
781    fn try_from(value: SQLiteValue<'_>) -> Result<Self, Self::Error> {
782        value.convert()
783    }
784}
785
786//------------------------------------------------------------------------------
787// TryFrom<&SQLiteValue> implementations for borrowing without consuming
788// Uses the FromSQLiteValue trait via convert_ref() for unified conversion logic
789//------------------------------------------------------------------------------
790
791/// Macro to implement `TryFrom`<&`SQLiteValue`> for types implementing `FromSQLiteValue`
792macro_rules! impl_try_from_sqlite_value_ref {
793    ($($ty:ty),* $(,)?) => {
794        $(
795            impl<'a> TryFrom<&SQLiteValue<'a>> for $ty {
796                type Error = DrizzleError;
797
798                #[inline]
799                fn try_from(value: &SQLiteValue<'a>) -> Result<Self, Self::Error> {
800                    value.convert_ref()
801                }
802            }
803        )*
804    };
805}
806
807impl_try_from_sqlite_value_ref!(
808    i8,
809    i16,
810    i32,
811    i64,
812    isize,
813    u8,
814    u16,
815    u32,
816    u64,
817    usize,
818    f32,
819    f64,
820    bool,
821    String,
822    Box<String>,
823    Rc<String>,
824    Arc<String>,
825    Box<str>,
826    Rc<str>,
827    Arc<str>,
828    Box<Vec<u8>>,
829    Rc<Vec<u8>>,
830    Arc<Vec<u8>>,
831    Vec<u8>,
832    compact_str::CompactString,
833);
834
835#[cfg(feature = "uuid")]
836impl_try_from_sqlite_value_ref!(Uuid);
837
838#[cfg(feature = "chrono")]
839impl_try_from_sqlite_value_ref!(
840    chrono::NaiveDate,
841    chrono::NaiveTime,
842    chrono::NaiveDateTime,
843    chrono::DateTime<chrono::FixedOffset>,
844    chrono::DateTime<chrono::Utc>,
845    chrono::Duration,
846);
847
848#[cfg(feature = "time")]
849impl_try_from_sqlite_value_ref!(
850    time::Date,
851    time::Time,
852    time::PrimitiveDateTime,
853    time::OffsetDateTime,
854    time::Duration,
855);
856
857#[cfg(feature = "rust-decimal")]
858impl_try_from_sqlite_value_ref!(rust_decimal::Decimal);
859
860#[cfg(feature = "serde")]
861impl_try_from_sqlite_value_ref!(serde_json::Value);
862
863#[cfg(feature = "bytes")]
864impl_try_from_sqlite_value_ref!(bytes::Bytes, bytes::BytesMut);
865
866#[cfg(feature = "smallvec")]
867impl<const N: usize> TryFrom<&SQLiteValue<'_>> for smallvec::SmallVec<[u8; N]> {
868    type Error = DrizzleError;
869
870    fn try_from(value: &SQLiteValue<'_>) -> Result<Self, Self::Error> {
871        value.convert_ref()
872    }
873}
874
875// --- Borrowed reference types (cannot use FromSQLiteValue) ---
876
877impl<'a> TryFrom<&'a SQLiteValue<'a>> for &'a str {
878    type Error = DrizzleError;
879
880    fn try_from(value: &'a SQLiteValue<'a>) -> Result<Self, Self::Error> {
881        match value {
882            SQLiteValue::Text(cow) => Ok(cow.as_ref()),
883            _ => Err(DrizzleError::ConversionError(
884                format!("Cannot convert {value:?} to &str").into(),
885            )),
886        }
887    }
888}
889
890impl<'a> TryFrom<&'a SQLiteValue<'a>> for &'a [u8] {
891    type Error = DrizzleError;
892
893    fn try_from(value: &'a SQLiteValue<'a>) -> Result<Self, Self::Error> {
894        match value {
895            SQLiteValue::Blob(cow) => Ok(cow.as_ref()),
896            _ => Err(DrizzleError::ConversionError(
897                format!("Cannot convert {value:?} to &[u8]").into(),
898            )),
899        }
900    }
901}