Skip to main content

wasm_dbms_api/dbms/
value.rs

1mod discriminant;
2
3use std::borrow::Cow;
4use std::str::FromStr;
5use std::sync::OnceLock;
6
7use serde::{Deserialize, Serialize};
8
9use super::types;
10use crate::memory::{
11    DEFAULT_ALIGNMENT, DataSize, DecodeError, Encode, MSize, MemoryError, MemoryResult, PageOffset,
12};
13
14/// A generic wrapper enum to hold any DBMS value.
15#[cfg_attr(feature = "candid", derive(candid::CandidType))]
16#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
17pub enum Value {
18    Blob(types::Blob),
19    Boolean(types::Boolean),
20    Date(types::Date),
21    DateTime(types::DateTime),
22    Decimal(types::Decimal),
23    Int8(types::Int8),
24    Int16(types::Int16),
25    Int32(types::Int32),
26    Int64(types::Int64),
27    Json(types::Json),
28    Null,
29    Text(types::Text),
30    Uint8(types::Uint8),
31    Uint16(types::Uint16),
32    Uint32(types::Uint32),
33    Uint64(types::Uint64),
34    Uuid(types::Uuid),
35    Custom(crate::dbms::custom_value::CustomValue),
36}
37
38impl FromStr for Value {
39    type Err = ();
40
41    fn from_str(s: &str) -> Result<Self, Self::Err> {
42        Ok(Self::Text(s.into()))
43    }
44}
45
46// macro rules for implementing From trait for Value enum variants
47macro_rules! impl_conv_for_value {
48    ($variant:ident, $ty:ty, $name:ident, $test_name:ident) => {
49        impl From<$ty> for Value {
50            fn from(value: $ty) -> Self {
51                Value::$variant(value)
52            }
53        }
54
55        impl Value {
56            /// Attempts to extract a reference to the inner value if it matches the variant.
57            pub fn $name(&self) -> Option<&$ty> {
58                if let Value::$variant(v) = self {
59                    Some(v)
60                } else {
61                    None
62                }
63            }
64        }
65
66        #[cfg(test)]
67        mod $test_name {
68            use super::*;
69
70            #[test]
71            fn test_value_conversion() {
72                let value_instance: $ty = Default::default();
73                let value: Value = value_instance.clone().into();
74                assert_eq!(value.$name(), Some(&value_instance));
75            }
76        }
77    };
78}
79
80macro_rules! value_from_primitive {
81    ($variant:ident, $primitive:ty, $test_name:ident) => {
82        value_from_primitive!($variant, $primitive, $test_name, Default::default());
83    };
84
85    ($variant:ident, $primitive:ty, $test_name:ident, $default_value:expr) => {
86        impl From<$primitive> for Value {
87            fn from(value: $primitive) -> Self {
88                Value::$variant($crate::prelude::$variant(value.into()))
89            }
90        }
91
92        #[cfg(test)]
93        mod $test_name {
94            use super::*;
95
96            #[test]
97            fn test_value_from_primitive() {
98                let primitive_value: $primitive = $default_value;
99                if let Value::$variant(inner_value) = Value::from(primitive_value.clone()) {
100                    assert_eq!(inner_value.0, primitive_value);
101                } else {
102                    panic!("Value variant does not match");
103                }
104            }
105        }
106    };
107}
108
109// implement conversions for all Value variants
110impl_conv_for_value!(Blob, types::Blob, as_blob, tests_blob);
111impl_conv_for_value!(Boolean, types::Boolean, as_boolean, tests_boolean);
112impl_conv_for_value!(Date, types::Date, as_date, tests_date);
113impl_conv_for_value!(DateTime, types::DateTime, as_datetime, tests_datetime);
114impl_conv_for_value!(Decimal, types::Decimal, as_decimal, tests_decimal);
115impl_conv_for_value!(Int8, types::Int8, as_int8, tests_int8);
116impl_conv_for_value!(Int16, types::Int16, as_int16, tests_int16);
117impl_conv_for_value!(Int32, types::Int32, as_int32, tests_int32);
118impl_conv_for_value!(Int64, types::Int64, as_int64, tests_int64);
119impl_conv_for_value!(Json, types::Json, as_json, tests_json);
120impl_conv_for_value!(Text, types::Text, as_text, tests_text);
121impl_conv_for_value!(Uint8, types::Uint8, as_uint8, tests_uint8);
122impl_conv_for_value!(Uint16, types::Uint16, as_uint16, tests_uint16);
123impl_conv_for_value!(Uint32, types::Uint32, as_uint32, tests_uint32);
124impl_conv_for_value!(Uint64, types::Uint64, as_uint64, tests_uint64);
125impl_conv_for_value!(Uuid, types::Uuid, as_uuid, tests_uuid);
126
127// from inner values of types
128value_from_primitive!(Blob, &[u8], tests_blob_primitive_slice);
129value_from_primitive!(Blob, Vec<u8>, tests_blob_primitive);
130value_from_primitive!(Boolean, bool, tests_boolean_primitive);
131value_from_primitive!(Decimal, rust_decimal::Decimal, tests_decimal_primitive);
132value_from_primitive!(Int8, i8, tests_int8_primitive);
133value_from_primitive!(Int16, i16, tests_int16_primitive);
134value_from_primitive!(Int32, i32, tests_int32_primitive);
135value_from_primitive!(Int64, i64, tests_int64_primitive);
136value_from_primitive!(Uint8, u8, tests_uint8_primitive);
137value_from_primitive!(Uint16, u16, tests_uint16_primitive);
138value_from_primitive!(Uint32, u32, tests_uint32_primitive);
139value_from_primitive!(Uint64, u64, tests_uint64_primitive);
140value_from_primitive!(Text, String, tests_text_primitive_string);
141value_from_primitive!(Text, &str, tests_text_primitive_str);
142value_from_primitive!(Uuid, uuid::Uuid, tests_uuid_primitive);
143
144impl Value {
145    /// Checks if the value is [`Value::Null`].
146    pub fn is_null(&self) -> bool {
147        matches!(self, Value::Null)
148    }
149
150    /// Returns the type name of the value as a string.
151    pub fn type_name(&self) -> &'static str {
152        match self {
153            Value::Blob(_) => "Blob",
154            Value::Boolean(_) => "Boolean",
155            Value::Date(_) => "Date",
156            Value::DateTime(_) => "DateTime",
157            Value::Decimal(_) => "Decimal",
158            Value::Int8(_) => "Int8",
159            Value::Int16(_) => "Int16",
160            Value::Int32(_) => "Int32",
161            Value::Int64(_) => "Int64",
162            Value::Json(_) => "Json",
163            Value::Null => "Null",
164            Value::Text(_) => "Text",
165            Value::Uint8(_) => "Uint8",
166            Value::Uint16(_) => "Uint16",
167            Value::Uint32(_) => "Uint32",
168            Value::Uint64(_) => "Uint64",
169            Value::Uuid(_) => "Uuid",
170            Value::Custom(cv) => {
171                // Cache custom type names to avoid repeated allocations.
172                // The number of unique type tags is bounded at compile time,
173                // so the map grows to a fixed size. A maximum of 64 entries
174                // is enforced as a safety guard against unbounded memory usage
175                // on the IC, where heap is a scarce resource.
176                const MAX_CACHE_ENTRIES: usize = 64;
177                static CACHE: OnceLock<
178                    std::sync::Mutex<std::collections::HashMap<String, &'static str>>,
179                > = OnceLock::new();
180                let cache =
181                    CACHE.get_or_init(|| std::sync::Mutex::new(std::collections::HashMap::new()));
182                let mut map = cache.lock().unwrap_or_else(|e| e.into_inner());
183                if map.len() >= MAX_CACHE_ENTRIES && !map.contains_key(&cv.type_tag) {
184                    return "Custom(?)";
185                }
186                map.entry(cv.type_tag.clone()).or_insert_with(|| {
187                    let s = format!("Custom({})", cv.type_tag);
188                    s.leak()
189                })
190            }
191        }
192    }
193
194    /// Returns reference to the inner [`CustomValue`] if this is a `Custom` variant.
195    pub fn as_custom(&self) -> Option<&crate::dbms::custom_value::CustomValue> {
196        match self {
197            Value::Custom(v) => Some(v),
198            _ => None,
199        }
200    }
201
202    /// Attempts to decode a `Custom` variant into a concrete [`CustomDataType`](crate::dbms::types::CustomDataType).
203    ///
204    /// Returns `None` if this is not a `Custom` variant, the type tag doesn't
205    /// match, or decoding fails.
206    pub fn as_custom_type<T: crate::dbms::types::CustomDataType>(&self) -> Option<T> {
207        self.as_custom()
208            .filter(|cv| cv.type_tag == T::TYPE_TAG)
209            .and_then(|cv| T::decode(std::borrow::Cow::Borrowed(&cv.encoded)).ok())
210    }
211}
212
213/// Encodes a [`Value`] as `[discriminant: u8] + [inner_type.encode()]`.
214///
215/// For `Null`, only the discriminant byte is written.
216/// For `Custom`, the encoding is `[discriminant] + [tag_len: u16 LE] + [tag_bytes] + [data_len: u16 LE] + [encoded_bytes]`.
217impl Encode for Value {
218    const SIZE: DataSize = DataSize::Dynamic;
219    const ALIGNMENT: PageOffset = DEFAULT_ALIGNMENT;
220
221    fn encode(&'_ self) -> Cow<'_, [u8]> {
222        match self {
223            Value::Blob(v) => encode_with_discriminant(discriminant::BLOB, v.encode()),
224            Value::Boolean(v) => encode_with_discriminant(discriminant::BOOLEAN, v.encode()),
225            Value::Date(v) => encode_with_discriminant(discriminant::DATE, v.encode()),
226            Value::DateTime(v) => encode_with_discriminant(discriminant::DATE_TIME, v.encode()),
227            Value::Decimal(v) => encode_with_discriminant(discriminant::DECIMAL, v.encode()),
228            Value::Int8(v) => encode_with_discriminant(discriminant::INT8, v.encode()),
229            Value::Int16(v) => encode_with_discriminant(discriminant::INT16, v.encode()),
230            Value::Int32(v) => encode_with_discriminant(discriminant::INT32, v.encode()),
231            Value::Int64(v) => encode_with_discriminant(discriminant::INT64, v.encode()),
232            Value::Json(v) => encode_with_discriminant(discriminant::JSON, v.encode()),
233            Value::Null => Cow::Owned(vec![discriminant::NULL]),
234            Value::Text(v) => encode_with_discriminant(discriminant::TEXT, v.encode()),
235            Value::Uint8(v) => encode_with_discriminant(discriminant::UINT8, v.encode()),
236            Value::Uint16(v) => encode_with_discriminant(discriminant::UINT16, v.encode()),
237            Value::Uint32(v) => encode_with_discriminant(discriminant::UINT32, v.encode()),
238            Value::Uint64(v) => encode_with_discriminant(discriminant::UINT64, v.encode()),
239            Value::Uuid(v) => encode_with_discriminant(discriminant::UUID, v.encode()),
240            Value::Custom(cv) => {
241                let tag_bytes = cv.type_tag.as_bytes();
242                let tag_len = tag_bytes.len() as u16;
243                let data_len = cv.encoded.len() as u16;
244                let total = 1 + 2 + tag_bytes.len() + 2 + cv.encoded.len();
245                let mut buf = Vec::with_capacity(total);
246                buf.push(discriminant::CUSTOM);
247                buf.extend_from_slice(&tag_len.to_le_bytes());
248                buf.extend_from_slice(tag_bytes);
249                buf.extend_from_slice(&data_len.to_le_bytes());
250                buf.extend_from_slice(&cv.encoded);
251                Cow::Owned(buf)
252            }
253        }
254    }
255
256    fn decode(data: Cow<[u8]>) -> MemoryResult<Self> {
257        if data.is_empty() {
258            return Err(MemoryError::DecodeError(DecodeError::TooShort));
259        }
260
261        let disc = data[0];
262        let rest = Cow::Owned(data[1..].to_vec());
263
264        match disc {
265            discriminant::BLOB => types::Blob::decode(rest).map(Value::Blob),
266            discriminant::BOOLEAN => types::Boolean::decode(rest).map(Value::Boolean),
267            discriminant::DATE => types::Date::decode(rest).map(Value::Date),
268            discriminant::DATE_TIME => types::DateTime::decode(rest).map(Value::DateTime),
269            discriminant::DECIMAL => types::Decimal::decode(rest).map(Value::Decimal),
270            discriminant::INT8 => types::Int8::decode(rest).map(Value::Int8),
271            discriminant::INT16 => types::Int16::decode(rest).map(Value::Int16),
272            discriminant::INT32 => types::Int32::decode(rest).map(Value::Int32),
273            discriminant::INT64 => types::Int64::decode(rest).map(Value::Int64),
274            discriminant::JSON => types::Json::decode(rest).map(Value::Json),
275            discriminant::NULL => Ok(Value::Null),
276            discriminant::TEXT => types::Text::decode(rest).map(Value::Text),
277            discriminant::UINT8 => types::Uint8::decode(rest).map(Value::Uint8),
278            discriminant::UINT16 => types::Uint16::decode(rest).map(Value::Uint16),
279            discriminant::UINT32 => types::Uint32::decode(rest).map(Value::Uint32),
280            discriminant::UINT64 => types::Uint64::decode(rest).map(Value::Uint64),
281            discriminant::UUID => types::Uuid::decode(rest).map(Value::Uuid),
282            discriminant::CUSTOM => decode_custom_value(&data[1..]),
283            other => Err(MemoryError::DecodeError(DecodeError::InvalidDiscriminant(
284                other,
285            ))),
286        }
287    }
288
289    fn size(&self) -> MSize {
290        1 + match self {
291            Value::Blob(v) => Encode::size(v),
292            Value::Boolean(v) => Encode::size(v),
293            Value::Date(v) => Encode::size(v),
294            Value::DateTime(v) => Encode::size(v),
295            Value::Decimal(v) => Encode::size(v),
296            Value::Int8(v) => Encode::size(v),
297            Value::Int16(v) => Encode::size(v),
298            Value::Int32(v) => Encode::size(v),
299            Value::Int64(v) => Encode::size(v),
300            Value::Json(v) => Encode::size(v),
301            Value::Null => 0,
302            Value::Text(v) => Encode::size(v),
303            Value::Uint8(v) => Encode::size(v),
304            Value::Uint16(v) => Encode::size(v),
305            Value::Uint32(v) => Encode::size(v),
306            Value::Uint64(v) => Encode::size(v),
307            Value::Uuid(v) => Encode::size(v),
308            Value::Custom(cv) => {
309                // tag_len(2) + tag_bytes + data_len(2) + encoded_bytes
310                (2 + cv.type_tag.len() + 2 + cv.encoded.len()) as MSize
311            }
312        }
313    }
314}
315
316/// Prepends the discriminant byte to an already-encoded inner value.
317fn encode_with_discriminant(disc: u8, inner: Cow<[u8]>) -> Cow<'static, [u8]> {
318    let mut buf = Vec::with_capacity(1 + inner.len());
319    buf.push(disc);
320    buf.extend_from_slice(&inner);
321    Cow::Owned(buf)
322}
323
324/// Decodes a [`CustomValue`](crate::dbms::custom_value::CustomValue) from the bytes after the discriminant.
325fn decode_custom_value(data: &[u8]) -> MemoryResult<Value> {
326    if data.len() < 2 {
327        return Err(MemoryError::DecodeError(DecodeError::TooShort));
328    }
329    let tag_len = u16::from_le_bytes([data[0], data[1]]) as usize;
330    if data.len() < 2 + tag_len + 2 {
331        return Err(MemoryError::DecodeError(DecodeError::TooShort));
332    }
333    let type_tag = String::from_utf8(data[2..2 + tag_len].to_vec())?;
334    let data_offset = 2 + tag_len;
335    let data_len = u16::from_le_bytes([data[data_offset], data[data_offset + 1]]) as usize;
336    if data.len() < data_offset + 2 + data_len {
337        return Err(MemoryError::DecodeError(DecodeError::TooShort));
338    }
339    let encoded = data[data_offset + 2..data_offset + 2 + data_len].to_vec();
340    Ok(Value::Custom(crate::dbms::custom_value::CustomValue {
341        type_tag,
342        encoded,
343        display: String::new(),
344    }))
345}
346
347/// Encodes a `Vec<Value>` as `[count: u32 LE] + [for each value: [size: u32 LE] + value.encode()]`.
348///
349/// This is used as the key type for B-tree indexes, supporting both single-column
350/// and composite indexes uniformly.
351impl Encode for Vec<Value> {
352    const SIZE: DataSize = DataSize::Dynamic;
353    const ALIGNMENT: PageOffset = DEFAULT_ALIGNMENT;
354
355    fn encode(&'_ self) -> Cow<'_, [u8]> {
356        let count = self.len() as u32;
357        let mut buf = Vec::new();
358        buf.extend_from_slice(&count.to_le_bytes());
359        for value in self {
360            let encoded = Encode::encode(value);
361            let size = encoded.len() as u32;
362            buf.extend_from_slice(&size.to_le_bytes());
363            buf.extend_from_slice(&encoded);
364        }
365        Cow::Owned(buf)
366    }
367
368    fn decode(data: Cow<[u8]>) -> MemoryResult<Self> {
369        if data.len() < 4 {
370            return Err(MemoryError::DecodeError(DecodeError::TooShort));
371        }
372        let count = u32::from_le_bytes([data[0], data[1], data[2], data[3]]) as usize;
373        let mut offset = 4;
374        let mut values = Vec::with_capacity(count);
375        for _ in 0..count {
376            if offset + 4 > data.len() {
377                return Err(MemoryError::DecodeError(DecodeError::TooShort));
378            }
379            let size = u32::from_le_bytes([
380                data[offset],
381                data[offset + 1],
382                data[offset + 2],
383                data[offset + 3],
384            ]) as usize;
385            offset += 4;
386            if offset + size > data.len() {
387                return Err(MemoryError::DecodeError(DecodeError::TooShort));
388            }
389            let value = Value::decode(Cow::Owned(data[offset..offset + size].to_vec()))?;
390            values.push(value);
391            offset += size;
392        }
393        Ok(values)
394    }
395
396    fn size(&self) -> MSize {
397        let mut total: MSize = 4; // count
398        for value in self {
399            total += 4 + Encode::size(value); // size prefix + encoded value
400        }
401        total
402    }
403}
404
405#[cfg(test)]
406mod tests {
407
408    use uuid::Uuid;
409
410    use super::*;
411
412    #[test]
413    fn test_null() {
414        let int_value: Value = types::Int32(42).into();
415        assert!(!int_value.is_null());
416
417        let null_value = Value::Null;
418        assert!(null_value.is_null());
419    }
420
421    #[test]
422    fn test_value_conversion_blob() {
423        let blob = types::Blob(vec![1, 2, 3]);
424        let value: Value = blob.clone().into();
425        assert_eq!(value.as_blob(), Some(&blob));
426    }
427
428    #[test]
429    fn test_value_conversion_boolean() {
430        let boolean = types::Boolean(true);
431        let value: Value = boolean.into();
432        assert_eq!(value.as_boolean(), Some(&boolean));
433    }
434
435    #[test]
436    fn test_value_conversion_date() {
437        let date = types::Date {
438            year: 2023,
439            month: 3,
440            day: 15,
441        }; // Example date
442        let value: Value = date.into();
443        assert_eq!(value.as_date(), Some(&date));
444    }
445
446    #[test]
447    fn test_value_conversion_datetime() {
448        let datetime = types::DateTime {
449            year: 2023,
450            month: 3,
451            day: 15,
452            hour: 12,
453            minute: 30,
454            second: 45,
455            microsecond: 123456,
456            timezone_offset_minutes: 0,
457        }; // Example datetime
458        let value: Value = datetime.into();
459        assert_eq!(value.as_datetime(), Some(&datetime));
460    }
461
462    #[test]
463    fn test_value_conversion_decimal() {
464        let decimal = types::Decimal(rust_decimal::Decimal::new(12345, 2)); // 123.45
465        let value: Value = decimal.into();
466        assert_eq!(value.as_decimal(), Some(&decimal));
467    }
468
469    #[test]
470    fn test_value_conversion_int32() {
471        let int32 = types::Int32(1234567890);
472        let value: Value = int32.into();
473        assert_eq!(value.as_int32(), Some(&int32));
474    }
475
476    #[test]
477    fn test_value_conversion_int64() {
478        let int64 = types::Int64(1234567890);
479        let value: Value = int64.into();
480        assert_eq!(value.as_int64(), Some(&int64));
481    }
482
483    #[test]
484    fn test_value_conversion_text() {
485        let text = types::Text("Hello, World!".to_string());
486        let value: Value = text.clone().into();
487        assert_eq!(value.as_text(), Some(&text));
488    }
489
490    #[test]
491    fn test_value_conversion_uint32() {
492        let uint32 = types::Uint32(123456);
493        let value: Value = uint32.into();
494        assert_eq!(value.as_uint32(), Some(&uint32));
495    }
496
497    #[test]
498    fn test_value_conversion_uint64() {
499        let uint64 = types::Uint64(12345678901234);
500        let value: Value = uint64.into();
501        assert_eq!(value.as_uint64(), Some(&uint64));
502    }
503
504    #[test]
505    fn test_value_conversion_uuid() {
506        let uuid = types::Uuid(
507            Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").expect("failed to parse uuid"),
508        );
509        let value: Value = uuid.clone().into();
510        assert_eq!(value.as_uuid(), Some(&uuid));
511    }
512
513    #[test]
514    fn test_value_type_name() {
515        let int_value: Value = types::Int32(42).into();
516        assert_eq!(int_value.type_name(), "Int32");
517
518        let text_value: Value = types::Text("Hello".to_string()).into();
519        assert_eq!(text_value.type_name(), "Text");
520
521        let null_value = Value::Null;
522        assert_eq!(null_value.type_name(), "Null");
523    }
524
525    #[test]
526    fn test_value_from_str() {
527        let str_value = "Hello, DBMS!";
528
529        let value = Value::from_str(str_value).unwrap();
530        assert_eq!(value.as_text().unwrap().0, str_value);
531    }
532
533    #[test]
534    fn test_should_create_custom_value() {
535        let cv = crate::dbms::custom_value::CustomValue {
536            type_tag: "role".to_string(),
537            encoded: vec![0x01],
538            display: "Admin".to_string(),
539        };
540        let value = Value::Custom(cv.clone());
541        assert_eq!(value.as_custom(), Some(&cv));
542    }
543
544    #[test]
545    fn test_should_return_none_for_non_custom() {
546        let value = Value::Null;
547        assert_eq!(value.as_custom(), None);
548    }
549
550    #[test]
551    fn test_should_compare_custom_values() {
552        let a = Value::Custom(crate::dbms::custom_value::CustomValue {
553            type_tag: "role".to_string(),
554            encoded: vec![0x01],
555            display: "Admin".to_string(),
556        });
557        let b = Value::Custom(crate::dbms::custom_value::CustomValue {
558            type_tag: "role".to_string(),
559            encoded: vec![0x01],
560            display: "Admin".to_string(),
561        });
562        assert_eq!(a, b);
563    }
564
565    #[test]
566    fn test_should_order_custom_after_builtin() {
567        let builtin = Value::Uuid(types::Uuid::default());
568        let custom = Value::Custom(crate::dbms::custom_value::CustomValue {
569            type_tag: "role".to_string(),
570            encoded: vec![0x01],
571            display: "Admin".to_string(),
572        });
573        assert!(builtin < custom);
574    }
575
576    #[test]
577    fn test_should_get_custom_type_name() {
578        let cv = Value::Custom(crate::dbms::custom_value::CustomValue {
579            type_tag: "role".to_string(),
580            encoded: vec![0x01],
581            display: "Admin".to_string(),
582        });
583        assert_eq!(cv.type_name(), "Custom(role)");
584    }
585
586    // -- Encode round-trip tests for Value --
587
588    #[test]
589    fn test_encode_decode_null() {
590        let original = Value::Null;
591        let encoded = Encode::encode(&original);
592        assert_eq!(encoded.len(), 1);
593        let decoded = Value::decode(encoded).unwrap();
594        assert_eq!(original, decoded);
595    }
596
597    #[test]
598    fn test_encode_decode_uint32() {
599        let original = Value::Uint32(types::Uint32(42));
600        let encoded = Encode::encode(&original);
601        let decoded = Value::decode(encoded).unwrap();
602        assert_eq!(original, decoded);
603    }
604
605    #[test]
606    fn test_encode_decode_text() {
607        let original = Value::Text(types::Text("hello index".to_string()));
608        let encoded = Encode::encode(&original);
609        let decoded = Value::decode(encoded).unwrap();
610        assert_eq!(original, decoded);
611    }
612
613    #[test]
614    fn test_encode_decode_blob() {
615        let original = Value::Blob(types::Blob(vec![0xDE, 0xAD, 0xBE, 0xEF]));
616        let encoded = Encode::encode(&original);
617        let decoded = Value::decode(encoded).unwrap();
618        assert_eq!(original, decoded);
619    }
620
621    #[test]
622    fn test_encode_decode_boolean() {
623        let original = Value::Boolean(types::Boolean(true));
624        let encoded = Encode::encode(&original);
625        let decoded = Value::decode(encoded).unwrap();
626        assert_eq!(original, decoded);
627    }
628
629    #[test]
630    fn test_encode_decode_date() {
631        let original = Value::Date(types::Date {
632            year: 2026,
633            month: 3,
634            day: 29,
635        });
636        let encoded = Encode::encode(&original);
637        let decoded = Value::decode(encoded).unwrap();
638        assert_eq!(original, decoded);
639    }
640
641    #[test]
642    fn test_encode_decode_datetime() {
643        let original = Value::DateTime(types::DateTime {
644            year: 2026,
645            month: 3,
646            day: 29,
647            hour: 14,
648            minute: 30,
649            second: 0,
650            microsecond: 0,
651            timezone_offset_minutes: 60,
652        });
653        let encoded = Encode::encode(&original);
654        let decoded = Value::decode(encoded).unwrap();
655        assert_eq!(original, decoded);
656    }
657
658    #[test]
659    fn test_encode_decode_decimal() {
660        let original = Value::Decimal(types::Decimal(rust_decimal::Decimal::new(12345, 2)));
661        let encoded = Encode::encode(&original);
662        let decoded = Value::decode(encoded).unwrap();
663        assert_eq!(original, decoded);
664    }
665
666    #[test]
667    fn test_encode_decode_int8() {
668        let original = Value::Int8(types::Int8(-42));
669        let encoded = Encode::encode(&original);
670        let decoded = Value::decode(encoded).unwrap();
671        assert_eq!(original, decoded);
672    }
673
674    #[test]
675    fn test_encode_decode_int16() {
676        let original = Value::Int16(types::Int16(-1000));
677        let encoded = Encode::encode(&original);
678        let decoded = Value::decode(encoded).unwrap();
679        assert_eq!(original, decoded);
680    }
681
682    #[test]
683    fn test_encode_decode_int32() {
684        let original = Value::Int32(types::Int32(-100_000));
685        let encoded = Encode::encode(&original);
686        let decoded = Value::decode(encoded).unwrap();
687        assert_eq!(original, decoded);
688    }
689
690    #[test]
691    fn test_encode_decode_int64() {
692        let original = Value::Int64(types::Int64(-9_000_000_000));
693        let encoded = Encode::encode(&original);
694        let decoded = Value::decode(encoded).unwrap();
695        assert_eq!(original, decoded);
696    }
697
698    #[test]
699    fn test_encode_decode_uint8() {
700        let original = Value::Uint8(types::Uint8(255));
701        let encoded = Encode::encode(&original);
702        let decoded = Value::decode(encoded).unwrap();
703        assert_eq!(original, decoded);
704    }
705
706    #[test]
707    fn test_encode_decode_uint16() {
708        let original = Value::Uint16(types::Uint16(60_000));
709        let encoded = Encode::encode(&original);
710        let decoded = Value::decode(encoded).unwrap();
711        assert_eq!(original, decoded);
712    }
713
714    #[test]
715    fn test_encode_decode_uint64() {
716        let original = Value::Uint64(types::Uint64(18_446_744_073_709_551_615));
717        let encoded = Encode::encode(&original);
718        let decoded = Value::decode(encoded).unwrap();
719        assert_eq!(original, decoded);
720    }
721
722    #[test]
723    fn test_encode_decode_uuid() {
724        let original = Value::Uuid(types::Uuid(
725            Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap(),
726        ));
727        let encoded = Encode::encode(&original);
728        let decoded = Value::decode(encoded).unwrap();
729        assert_eq!(original, decoded);
730    }
731
732    #[test]
733    fn test_encode_decode_custom() {
734        let original = Value::Custom(crate::dbms::custom_value::CustomValue {
735            type_tag: "role".to_string(),
736            encoded: vec![0x01, 0x02],
737            display: "Admin".to_string(),
738        });
739        let encoded = Encode::encode(&original);
740        let decoded = Value::decode(encoded).unwrap();
741        // Display is not preserved through encoding
742        assert_eq!(decoded.as_custom().unwrap().type_tag, "role");
743        assert_eq!(decoded.as_custom().unwrap().encoded, vec![0x01, 0x02]);
744    }
745
746    #[test]
747    fn test_encode_decode_invalid_discriminant() {
748        let data = Cow::Owned(vec![0xFF]);
749        let result = Value::decode(data);
750        assert!(result.is_err());
751    }
752
753    #[test]
754    fn test_encode_decode_empty_data() {
755        let data: Cow<[u8]> = Cow::Owned(vec![]);
756        let result = Value::decode(data);
757        assert!(result.is_err());
758    }
759
760    #[test]
761    fn test_value_size_matches_encoded_length() {
762        let values = vec![
763            Value::Null,
764            Value::Uint32(types::Uint32(42)),
765            Value::Text(types::Text("test".to_string())),
766            Value::Boolean(types::Boolean(false)),
767        ];
768        for value in &values {
769            let encoded = Encode::encode(value);
770            assert_eq!(
771                Encode::size(value) as usize,
772                encoded.len(),
773                "size mismatch for {value:?}"
774            );
775        }
776    }
777
778    // -- Encode round-trip tests for Vec<Value> --
779
780    #[test]
781    fn test_encode_decode_vec_single_value() {
782        let original = vec![Value::Uint32(types::Uint32(99))];
783        let encoded = Encode::encode(&original);
784        let decoded = Vec::<Value>::decode(encoded).unwrap();
785        assert_eq!(original, decoded);
786    }
787
788    #[test]
789    fn test_encode_decode_vec_composite() {
790        let original = vec![
791            Value::Text(types::Text("alice".to_string())),
792            Value::Uint32(types::Uint32(30)),
793        ];
794        let encoded = Encode::encode(&original);
795        let decoded = Vec::<Value>::decode(encoded).unwrap();
796        assert_eq!(original, decoded);
797    }
798
799    #[test]
800    fn test_encode_decode_vec_empty() {
801        let original: Vec<Value> = vec![];
802        let encoded = Encode::encode(&original);
803        let decoded = Vec::<Value>::decode(encoded).unwrap();
804        assert_eq!(original, decoded);
805    }
806
807    #[test]
808    fn test_vec_value_size_matches_encoded_length() {
809        let original = vec![
810            Value::Text(types::Text("hello".to_string())),
811            Value::Null,
812            Value::Int64(types::Int64(-1)),
813        ];
814        let encoded = Encode::encode(&original);
815        assert_eq!(Encode::size(&original) as usize, encoded.len());
816    }
817}