Skip to main content

rypipe_core/
value.rs

1use std::borrow::Cow;
2
3/// A logical value produced by a decoder.
4///
5/// Decoders can emit borrowed strings when possible, or native typed values
6/// for formats like JSON that already distinguish numbers/booleans.
7///
8/// `Str` is a `Cow` so that adapters needing owned storage (e.g. XML entity
9/// unescaping) can move an owned `String` into the value without a dangling
10/// borrow.
11#[derive(Clone, Debug, PartialEq)]
12pub enum Value<'a> {
13    /// UTF-8 string, borrowed from the input buffer or owned.
14    Str(Cow<'a, str>),
15    /// 64-bit signed integer.
16    Int64(i64),
17    /// 64-bit floating point number.
18    Float64(f64),
19    /// Boolean.
20    Bool(bool),
21    /// Calendar date: days since the Unix epoch (Arrow `Date32`).
22    Date32(i32),
23    /// Point in time as a raw integer in the column's `TimeUnit`
24    /// (Arrow `Timestamp`). Adapters should declare the unit via
25    /// `field_types` so the raw integer is interpreted correctly.
26    Timestamp(i64),
27    /// Explicit null / missing value.
28    Null,
29}
30
31impl<'a> Value<'a> {
32    /// Borrow the string content.
33    #[inline]
34    pub fn as_str(&self) -> Option<&str> {
35        match self {
36            Value::Str(s) => Some(s.as_ref()),
37            _ => None,
38        }
39    }
40
41    /// Convert into a `Value<'static>` by cloning any borrowed string data.
42    pub fn into_static(self) -> Value<'static> {
43        match self {
44            Value::Str(s) => Value::Str(Cow::Owned(s.into_owned())),
45            Value::Int64(i) => Value::Int64(i),
46            Value::Float64(f) => Value::Float64(f),
47            Value::Bool(b) => Value::Bool(b),
48            Value::Date32(d) => Value::Date32(d),
49            Value::Timestamp(t) => Value::Timestamp(t),
50            Value::Null => Value::Null,
51        }
52    }
53}