readstat/rs_var.rs
1//! Variable types and format classification for SAS data.
2//!
3//! [`ReadStatVarFormatClass`] classifies SAS format strings into semantic categories
4//! (Date, `DateTime`, Time, and their sub-second precision variants), which determines
5//! the Arrow data type used during conversion.
6//!
7//! [`ReadStatVarType`] and [`ReadStatVarTypeClass`] map `ReadStat` C type codes to Rust
8//! enums, used during schema construction and builder allocation.
9
10use num_derive::FromPrimitive;
11use serde::Serialize;
12
13/// Semantic classification of a SAS format string.
14///
15/// Determines the Arrow data type used for date/time/datetime variables:
16///
17/// | Variant | Arrow Type |
18/// |---------|------------|
19/// | `Date` | `Date32` |
20/// | `DateTime` | `Timestamp(Second)` |
21/// | `DateTimeWithMilliseconds` | `Timestamp(Millisecond)` |
22/// | `DateTimeWithMicroseconds` | `Timestamp(Microsecond)` |
23/// | `DateTimeWithNanoseconds` | `Timestamp(Nanosecond)` |
24/// | `Time` | `Time32(Second)` |
25/// | `TimeWithMicroseconds` | `Time64(Microsecond)` |
26#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
27pub enum ReadStatVarFormatClass {
28 /// Date format (e.g. `DATE9`, `MMDDYY10`). Maps to Arrow `Date32`.
29 Date,
30 /// `DateTime` format with second precision (e.g. `DATETIME22`).
31 DateTime,
32 /// `DateTime` format with millisecond precision (e.g. `DATETIME22.3`).
33 DateTimeWithMilliseconds,
34 /// `DateTime` format with microsecond precision (e.g. `DATETIME22.6`).
35 DateTimeWithMicroseconds,
36 /// `DateTime` format with nanosecond precision (e.g. `DATETIME22.9`).
37 DateTimeWithNanoseconds,
38 /// Time format with second precision (e.g. `TIME8`).
39 Time,
40 /// Time format with microsecond precision (e.g. `TIME15.6`).
41 TimeWithMicroseconds,
42}
43
44/// The storage type of a SAS variable, as reported by the `ReadStat` C library.
45#[derive(Clone, Copy, Debug, FromPrimitive, Serialize)]
46#[allow(clippy::cast_possible_wrap)]
47pub enum ReadStatVarType {
48 /// Variable-length string.
49 String = readstat_sys::readstat_type_e_READSTAT_TYPE_STRING as isize,
50 /// 8-bit signed integer.
51 Int8 = readstat_sys::readstat_type_e_READSTAT_TYPE_INT8 as isize,
52 /// 16-bit signed integer.
53 Int16 = readstat_sys::readstat_type_e_READSTAT_TYPE_INT16 as isize,
54 /// 32-bit signed integer.
55 Int32 = readstat_sys::readstat_type_e_READSTAT_TYPE_INT32 as isize,
56 /// 32-bit floating point.
57 Float = readstat_sys::readstat_type_e_READSTAT_TYPE_FLOAT as isize,
58 /// 64-bit floating point (also used for dates/times via format class).
59 Double = readstat_sys::readstat_type_e_READSTAT_TYPE_DOUBLE as isize,
60 /// String reference (interned string).
61 StringRef = readstat_sys::readstat_type_e_READSTAT_TYPE_STRING_REF as isize,
62 /// Unknown or unrecognized type.
63 Unknown,
64}
65
66/// High-level type class of a SAS variable: string or numeric.
67#[derive(Clone, Copy, Debug, FromPrimitive, Serialize)]
68#[allow(clippy::cast_possible_wrap)]
69pub enum ReadStatVarTypeClass {
70 /// Character/string data.
71 String = readstat_sys::readstat_type_class_e_READSTAT_TYPE_CLASS_STRING as isize,
72 /// Numeric data (integers, floats, dates, times).
73 Numeric = readstat_sys::readstat_type_class_e_READSTAT_TYPE_CLASS_NUMERIC as isize,
74}