use super::Value;
use super::impls::value_variant_name;
#[cfg(feature = "cube")]
use crate::Cube;
#[cfg(feature = "matrix")]
use crate::Matrix;
#[cfg(feature = "scalar_type")]
use crate::Scalar;
use crate::{Array, FieldArray, Table, enums::error::MinarrowError};
use std::convert::TryFrom;
use std::sync::Arc;
#[cfg(feature = "chunked")]
use crate::{SuperArray, SuperTable};
#[cfg(feature = "views")]
use crate::{ArrayV, NumericArrayV, TableV, TextArrayV};
#[cfg(all(feature = "views", feature = "datetime"))]
use crate::TemporalArrayV;
#[cfg(all(feature = "chunked", feature = "views"))]
use crate::{SuperArrayV, SuperTableV};
use crate::traits::custom_value::CustomValue;
impl Value {
#[cfg(feature = "scalar_type")]
#[inline]
pub fn scalar(&self) -> &Scalar {
match self {
Value::Scalar(s) => s,
_ => panic!("Expected Value::Scalar, found {}", value_variant_name(self)),
}
}
#[cfg(feature = "scalar_type")]
#[inline]
pub fn try_scalar(&self) -> Result<&Scalar, MinarrowError> {
match self {
Value::Scalar(s) => Ok(s),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "Scalar",
message: None,
}),
}
}
#[inline]
pub fn arr(&self) -> &Array {
match self {
Value::Array(a) => a,
_ => panic!("Expected Value::Array, found {}", value_variant_name(self)),
}
}
#[inline]
pub fn try_arr(&self) -> Result<&Array, MinarrowError> {
match self {
Value::Array(a) => Ok(a),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "Array",
message: None,
}),
}
}
#[cfg(feature = "views")]
#[inline]
pub fn av(&self) -> &ArrayV {
match self {
Value::ArrayView(av) => av,
_ => panic!(
"Expected Value::ArrayView, found {}",
value_variant_name(self)
),
}
}
#[cfg(feature = "views")]
#[inline]
pub fn try_av(&self) -> Result<&ArrayV, MinarrowError> {
match self {
Value::ArrayView(av) => Ok(av),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "ArrayV",
message: None,
}),
}
}
#[inline]
pub fn fa(&self) -> &FieldArray {
match self {
Value::FieldArray(fa) => fa,
_ => panic!(
"Expected Value::FieldArray, found {}",
value_variant_name(self)
),
}
}
#[inline]
pub fn try_fa(&self) -> Result<&FieldArray, MinarrowError> {
match self {
Value::FieldArray(fa) => Ok(fa),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "FieldArray",
message: None,
}),
}
}
#[inline]
pub fn table(&self) -> &Table {
match self {
Value::Table(t) => t,
_ => panic!("Expected Value::Table, found {}", value_variant_name(self)),
}
}
#[inline]
pub fn try_table(&self) -> Result<&Table, MinarrowError> {
match self {
Value::Table(t) => Ok(t),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "Table",
message: None,
}),
}
}
#[cfg(feature = "views")]
#[inline]
pub fn tv(&self) -> &TableV {
match self {
Value::TableView(tv) => tv,
_ => panic!(
"Expected Value::TableView, found {}",
value_variant_name(self)
),
}
}
#[cfg(feature = "views")]
#[inline]
pub fn try_tv(&self) -> Result<&TableV, MinarrowError> {
match self {
Value::TableView(tv) => Ok(tv),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "TableV",
message: None,
}),
}
}
#[cfg(feature = "chunked")]
#[inline]
pub fn sa(&self) -> &SuperArray {
match self {
Value::SuperArray(sa) => sa,
_ => panic!(
"Expected Value::SuperArray, found {}",
value_variant_name(self)
),
}
}
#[cfg(feature = "chunked")]
#[inline]
pub fn try_sa(&self) -> Result<&SuperArray, MinarrowError> {
match self {
Value::SuperArray(sa) => Ok(sa),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "SuperArray",
message: None,
}),
}
}
#[cfg(all(feature = "chunked", feature = "views"))]
#[inline]
pub fn sav(&self) -> &SuperArrayV {
match self {
Value::SuperArrayView(sav) => sav,
_ => panic!(
"Expected Value::SuperArrayView, found {}",
value_variant_name(self)
),
}
}
#[cfg(all(feature = "chunked", feature = "views"))]
#[inline]
pub fn try_sav(&self) -> Result<&SuperArrayV, MinarrowError> {
match self {
Value::SuperArrayView(sav) => Ok(sav),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "SuperArrayV",
message: None,
}),
}
}
#[cfg(feature = "chunked")]
#[inline]
pub fn st(&self) -> &SuperTable {
match self {
Value::SuperTable(st) => st,
_ => panic!(
"Expected Value::SuperTable, found {}",
value_variant_name(self)
),
}
}
#[cfg(feature = "chunked")]
#[inline]
pub fn try_st(&self) -> Result<&SuperTable, MinarrowError> {
match self {
Value::SuperTable(st) => Ok(st),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "SuperTable",
message: None,
}),
}
}
#[cfg(all(feature = "chunked", feature = "views"))]
#[inline]
pub fn stv(&self) -> &SuperTableV {
match self {
Value::SuperTableView(stv) => stv,
_ => panic!(
"Expected Value::SuperTableView, found {}",
value_variant_name(self)
),
}
}
#[cfg(all(feature = "chunked", feature = "views"))]
#[inline]
pub fn try_stv(&self) -> Result<&SuperTableV, MinarrowError> {
match self {
Value::SuperTableView(stv) => Ok(stv),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "SuperTableV",
message: None,
}),
}
}
#[cfg(feature = "matrix")]
#[inline]
pub fn mat(&self) -> &Matrix {
match self {
Value::Matrix(m) => m,
_ => panic!("Expected Value::Matrix, found {}", value_variant_name(self)),
}
}
#[cfg(feature = "matrix")]
#[inline]
pub fn try_mat(&self) -> Result<&Matrix, MinarrowError> {
match self {
Value::Matrix(m) => Ok(m),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "Matrix",
message: None,
}),
}
}
#[cfg(feature = "cube")]
#[inline]
pub fn cube(&self) -> &Cube {
match self {
Value::Cube(c) => c,
_ => panic!("Expected Value::Cube, found {}", value_variant_name(self)),
}
}
#[cfg(feature = "cube")]
#[inline]
pub fn try_cube(&self) -> Result<&Cube, MinarrowError> {
match self {
Value::Cube(c) => Ok(c),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "Cube",
message: None,
}),
}
}
#[inline]
pub fn vec_val(&self) -> &Vec<Value> {
match self {
Value::VecValue(v) => v,
_ => panic!(
"Expected Value::VecValue, found {}",
value_variant_name(self)
),
}
}
#[inline]
pub fn try_vec_val(&self) -> Result<&Vec<Value>, MinarrowError> {
match self {
Value::VecValue(v) => Ok(v),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "Vec<Value>",
message: None,
}),
}
}
#[inline]
pub fn box_val(&self) -> &Value {
match self {
Value::BoxValue(b) => b,
_ => panic!(
"Expected Value::BoxValue, found {}",
value_variant_name(self)
),
}
}
#[inline]
pub fn try_box_val(&self) -> Result<&Value, MinarrowError> {
match self {
Value::BoxValue(b) => Ok(b),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "Box<Value>",
message: None,
}),
}
}
#[inline]
pub fn arc_val(&self) -> &Value {
match self {
Value::ArcValue(a) => a,
_ => panic!(
"Expected Value::ArcValue, found {}",
value_variant_name(self)
),
}
}
#[inline]
pub fn try_arc_val(&self) -> Result<&Value, MinarrowError> {
match self {
Value::ArcValue(a) => Ok(a),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "Arc<Value>",
message: None,
}),
}
}
#[inline]
pub fn t2(&self) -> &(Value, Value) {
match self {
Value::Tuple2(t) => t,
_ => panic!("Expected Value::Tuple2, found {}", value_variant_name(self)),
}
}
#[inline]
pub fn try_t2(&self) -> Result<&(Value, Value), MinarrowError> {
match self {
Value::Tuple2(t) => Ok(t),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "(Value, Value)",
message: None,
}),
}
}
#[inline]
pub fn t3(&self) -> &(Value, Value, Value) {
match self {
Value::Tuple3(t) => t,
_ => panic!("Expected Value::Tuple3, found {}", value_variant_name(self)),
}
}
#[inline]
pub fn try_t3(&self) -> Result<&(Value, Value, Value), MinarrowError> {
match self {
Value::Tuple3(t) => Ok(t),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "(Value, Value, Value)",
message: None,
}),
}
}
#[inline]
pub fn t4(&self) -> &(Value, Value, Value, Value) {
match self {
Value::Tuple4(t) => t,
_ => panic!("Expected Value::Tuple4, found {}", value_variant_name(self)),
}
}
#[inline]
pub fn try_t4(&self) -> Result<&(Value, Value, Value, Value), MinarrowError> {
match self {
Value::Tuple4(t) => Ok(t),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "(Value, Value, Value, Value)",
message: None,
}),
}
}
#[inline]
pub fn t5(&self) -> &(Value, Value, Value, Value, Value) {
match self {
Value::Tuple5(t) => t,
_ => panic!("Expected Value::Tuple5, found {}", value_variant_name(self)),
}
}
#[inline]
pub fn try_t5(&self) -> Result<&(Value, Value, Value, Value, Value), MinarrowError> {
match self {
Value::Tuple5(t) => Ok(t),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "(Value, Value, Value, Value, Value)",
message: None,
}),
}
}
#[inline]
pub fn t6(&self) -> &(Value, Value, Value, Value, Value, Value) {
match self {
Value::Tuple6(t) => t,
_ => panic!("Expected Value::Tuple6, found {}", value_variant_name(self)),
}
}
#[inline]
pub fn try_t6(&self) -> Result<&(Value, Value, Value, Value, Value, Value), MinarrowError> {
match self {
Value::Tuple6(t) => Ok(t),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "(Value, Value, Value, Value, Value, Value)",
message: None,
}),
}
}
#[inline]
pub fn custom(&self) -> &dyn CustomValue {
match self {
Value::Custom(c) => c.as_ref(),
_ => panic!("Expected Value::Custom, found {}", value_variant_name(self)),
}
}
#[inline]
pub fn try_custom(&self) -> Result<&dyn CustomValue, MinarrowError> {
match self {
Value::Custom(c) => Ok(c.as_ref()),
_ => Err(MinarrowError::TypeError {
from: value_variant_name(self),
to: "Custom",
message: None,
}),
}
}
}
macro_rules! impl_value_from {
($variant:ident: $t:ty) => {
impl From<$t> for Value {
#[inline]
fn from(v: $t) -> Self {
Value::$variant(v)
}
}
};
}
macro_rules! impl_tryfrom_value {
($variant:ident: $t:ty) => {
impl TryFrom<Value> for $t {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::$variant(inner) => Ok(inner),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: stringify!($t),
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
};
}
#[cfg(feature = "scalar_type")]
impl_value_from!(Scalar: Scalar);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value!(Scalar: Scalar);
#[cfg(feature = "scalar_type")]
macro_rules! impl_tryfrom_value_numeric {
($t:ty, $method:ident) => {
impl TryFrom<Value> for $t {
type Error = MinarrowError;
#[inline]
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::Scalar(s) => match s {
Scalar::Null => Err(MinarrowError::TypeError {
from: "Value::Scalar(Null)",
to: stringify!($t),
message: Some("Cannot convert Null to numeric type".to_owned()),
}),
_ => Ok(s.$method()),
},
_ => Err(MinarrowError::TypeError {
from: "Value",
to: stringify!($t),
message: Some("Expected Value::Scalar variant".to_owned()),
}),
}
}
}
};
}
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_numeric!(f64, f64);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_numeric!(f32, f32);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_numeric!(i64, i64);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_numeric!(i32, i32);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_numeric!(u64, u64);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_numeric!(u32, u32);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_numeric!(bool, bool);
#[cfg(feature = "scalar_type")]
macro_rules! impl_tryfrom_value_option {
($t:ty, $method:ident) => {
impl TryFrom<Value> for Option<$t> {
type Error = MinarrowError;
#[inline]
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::Scalar(s) => match s {
Scalar::Null => Ok(None),
_ => Ok(Some(s.$method())),
},
_ => Err(MinarrowError::TypeError {
from: "Value",
to: concat!("Option<", stringify!($t), ">"),
message: Some("Expected Value::Scalar variant".to_owned()),
}),
}
}
}
};
}
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_option!(f64, f64);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_option!(f32, f32);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_option!(i64, i64);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_option!(i32, i32);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_option!(u64, u64);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_option!(u32, u32);
#[cfg(feature = "scalar_type")]
impl_tryfrom_value_option!(bool, bool);
impl From<Array> for Value {
#[inline]
fn from(v: Array) -> Self {
Value::Array(Arc::new(v))
}
}
#[cfg(feature = "views")]
impl From<ArrayV> for Value {
#[inline]
fn from(v: ArrayV) -> Self {
Value::ArrayView(Arc::new(v))
}
}
impl From<Table> for Value {
#[inline]
fn from(v: Table) -> Self {
Value::Table(Arc::new(v))
}
}
#[cfg(feature = "views")]
impl From<TableV> for Value {
#[inline]
fn from(v: TableV) -> Self {
Value::TableView(Arc::new(v))
}
}
impl From<FieldArray> for Value {
#[inline]
fn from(v: FieldArray) -> Self {
Value::FieldArray(Arc::new(v))
}
}
#[cfg(feature = "chunked")]
impl From<SuperTable> for Value {
#[inline]
fn from(v: SuperTable) -> Self {
Value::SuperTable(Arc::new(v))
}
}
#[cfg(feature = "cube")]
impl From<Cube> for Value {
#[inline]
fn from(v: Cube) -> Self {
Value::Cube(Arc::new(v))
}
}
#[cfg(feature = "chunked")]
impl From<SuperArray> for Value {
#[inline]
fn from(v: SuperArray) -> Self {
Value::SuperArray(Arc::new(v))
}
}
#[cfg(all(feature = "chunked", feature = "views"))]
impl From<SuperArrayV> for Value {
#[inline]
fn from(v: SuperArrayV) -> Self {
Value::SuperArrayView(Arc::new(v))
}
}
#[cfg(feature = "matrix")]
impl From<Matrix> for Value {
#[inline]
fn from(v: Matrix) -> Self {
Value::Matrix(Arc::new(v))
}
}
impl TryFrom<Value> for Array {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
let err = || MinarrowError::TypeError {
from: "Value",
to: "Array",
message: Some("Value type mismatch".to_owned()),
};
match v {
Value::Array(inner) => Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone())),
#[cfg(feature = "views")]
Value::ArrayView(inner) => {
let view = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
Ok(view.to_array())
}
Value::FieldArray(inner) => {
let fa = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
Ok(fa.array)
}
#[cfg(feature = "scalar_type")]
Value::Scalar(s) => Ok(Array::from(s)),
Value::Table(_) => Err(err()),
#[cfg(feature = "views")]
Value::TableView(_) => Err(err()),
#[cfg(feature = "chunked")]
Value::SuperArray(_) => Err(err()),
#[cfg(all(feature = "chunked", feature = "views"))]
Value::SuperArrayView(_) => Err(err()),
#[cfg(feature = "chunked")]
Value::SuperTable(_) => Err(err()),
#[cfg(all(feature = "chunked", feature = "views"))]
Value::SuperTableView(_) => Err(err()),
#[cfg(feature = "matrix")]
Value::Matrix(_) => Err(err()),
#[cfg(feature = "cube")]
Value::Cube(_) => Err(err()),
Value::VecValue(_) => Err(err()),
Value::BoxValue(_) => Err(err()),
Value::ArcValue(_) => Err(err()),
Value::Tuple2(_) => Err(err()),
Value::Tuple3(_) => Err(err()),
Value::Tuple4(_) => Err(err()),
Value::Tuple5(_) => Err(err()),
Value::Tuple6(_) => Err(err()),
Value::Custom(_) => Err(err()),
}
}
}
#[cfg(feature = "views")]
impl TryFrom<Value> for ArrayV {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::ArrayView(inner) => {
Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone()))
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "ArrayV",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
#[cfg(feature = "views")]
impl TryFrom<Value> for NumericArrayV {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::ArrayView(inner) => {
let view = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
let (array, offset, len) = view.as_tuple();
match array {
Array::NumericArray(num_arr) => Ok(NumericArrayV::new(num_arr, offset, len)),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "NumericArrayV",
message: Some("ArrayV is not a NumericArray".to_owned()),
}),
}
}
Value::Array(inner) => {
let arr = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
match arr {
Array::NumericArray(num_arr) => {
let len = num_arr.len();
Ok(NumericArrayV::new(num_arr, 0, len))
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "NumericArrayV",
message: Some("Array is not a NumericArray".to_owned()),
}),
}
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "NumericArrayV",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
#[cfg(feature = "views")]
impl TryFrom<Value> for TextArrayV {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::ArrayView(inner) => {
let view = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
let (array, offset, len) = view.as_tuple();
match array {
Array::TextArray(text_arr) => Ok(TextArrayV::new(text_arr, offset, len)),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "TextArrayV",
message: Some("ArrayV is not a TextArray".to_owned()),
}),
}
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "TextArrayV",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
#[cfg(all(feature = "views", feature = "datetime"))]
impl TryFrom<Value> for TemporalArrayV {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::ArrayView(inner) => {
let view = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
let (array, offset, len) = view.as_tuple();
match array {
Array::TemporalArray(temp_arr) => {
Ok(TemporalArrayV::new(temp_arr, offset, len))
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "TemporalArrayV",
message: Some("ArrayV is not a TemporalArray".to_owned()),
}),
}
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "TemporalArrayV",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
impl TryFrom<Value> for Table {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
let err = || MinarrowError::TypeError {
from: "Value",
to: "Table",
message: Some("Value type mismatch".to_owned()),
};
match v {
Value::Table(inner) => Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone())),
#[cfg(feature = "views")]
Value::TableView(inner) => {
let view = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
Ok(view.to_table())
}
Value::Array(inner) => {
let array = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
Ok(Table::new(
"array".to_string(),
Some(vec![FieldArray::from_arr("column_0", array)]),
))
}
#[cfg(feature = "views")]
Value::ArrayView(inner) => {
let view = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
Ok(Table::new(
"array".to_string(),
Some(vec![FieldArray::from_arr("column_0", view.to_array())]),
))
}
Value::FieldArray(inner) => {
let fa = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
Ok(Table::new("array".to_string(), Some(vec![fa])))
}
#[cfg(feature = "scalar_type")]
Value::Scalar(s) => {
let array = Array::from(s);
Ok(Table::new(
"scalar".to_string(),
Some(vec![FieldArray::from_arr("column_0", array)]),
))
}
#[cfg(feature = "chunked")]
Value::SuperArray(_) => Err(err()),
#[cfg(all(feature = "chunked", feature = "views"))]
Value::SuperArrayView(_) => Err(err()),
#[cfg(feature = "chunked")]
Value::SuperTable(_) => Err(err()),
#[cfg(all(feature = "chunked", feature = "views"))]
Value::SuperTableView(_) => Err(err()),
#[cfg(feature = "matrix")]
Value::Matrix(_) => Err(err()),
#[cfg(feature = "cube")]
Value::Cube(_) => Err(err()),
Value::VecValue(_) => Err(err()),
Value::BoxValue(_) => Err(err()),
Value::ArcValue(_) => Err(err()),
Value::Tuple2(_) => Err(err()),
Value::Tuple3(_) => Err(err()),
Value::Tuple4(_) => Err(err()),
Value::Tuple5(_) => Err(err()),
Value::Tuple6(_) => Err(err()),
Value::Custom(_) => Err(err()),
}
}
}
#[cfg(feature = "views")]
impl TryFrom<Value> for TableV {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::TableView(inner) => {
Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone()))
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "TableV",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
impl TryFrom<Value> for FieldArray {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::FieldArray(inner) => {
Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone()))
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "FieldArray",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
#[cfg(feature = "chunked")]
impl TryFrom<Value> for SuperTable {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::SuperTable(inner) => {
Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone()))
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "SuperTable",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
#[cfg(feature = "cube")]
impl TryFrom<Value> for Cube {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::Cube(inner) => Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone())),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "Cube",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
#[cfg(feature = "chunked")]
impl TryFrom<Value> for SuperArray {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::SuperArray(inner) => {
Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone()))
}
Value::VecValue(inner) => {
let values = Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone());
let chunks: Result<Vec<Array>, _> =
values.into_iter().map(Array::try_from).collect();
Ok(SuperArray::from_arrays(chunks?))
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "SuperArray",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
#[cfg(all(feature = "chunked", feature = "views"))]
impl TryFrom<Value> for SuperArrayV {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::SuperArrayView(inner) => {
Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone()))
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "SuperArrayV",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
#[cfg(feature = "matrix")]
impl TryFrom<Value> for Matrix {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::Matrix(inner) => Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone())),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "Matrix",
message: Some("Value type mismatch".to_owned()),
}),
}
}
}
impl From<Vec<Value>> for Value {
fn from(v: Vec<Value>) -> Self {
Value::VecValue(Arc::new(v))
}
}
impl From<(Value, Value)> for Value {
fn from(v: (Value, Value)) -> Self {
Value::Tuple2(Arc::new(v))
}
}
impl From<(Value, Value, Value)> for Value {
fn from(v: (Value, Value, Value)) -> Self {
Value::Tuple3(Arc::new(v))
}
}
impl From<(Value, Value, Value, Value)> for Value {
fn from(v: (Value, Value, Value, Value)) -> Self {
Value::Tuple4(Arc::new(v))
}
}
impl From<(Value, Value, Value, Value, Value)> for Value {
fn from(v: (Value, Value, Value, Value, Value)) -> Self {
Value::Tuple5(Arc::new(v))
}
}
impl From<(Value, Value, Value, Value, Value, Value)> for Value {
fn from(v: (Value, Value, Value, Value, Value, Value)) -> Self {
Value::Tuple6(Arc::new(v))
}
}
impl TryFrom<Value> for Vec<Value> {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::VecValue(inner) => {
Ok(Arc::try_unwrap(inner).unwrap_or_else(|arc| (*arc).clone()))
}
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "Vec<Value>",
message: Some("Expected VecValue variant".to_owned()),
}),
}
}
}
impl TryFrom<Value> for (Value, Value) {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::Tuple2(tuple) => Ok(Arc::try_unwrap(tuple).unwrap_or_else(|arc| (*arc).clone())),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "(Value, Value)",
message: Some("Expected Tuple2 variant".to_owned()),
}),
}
}
}
impl TryFrom<Value> for (Value, Value, Value) {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::Tuple3(tuple) => Ok(Arc::try_unwrap(tuple).unwrap_or_else(|arc| (*arc).clone())),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "(Value, Value, Value)",
message: Some("Expected Tuple3 variant".to_owned()),
}),
}
}
}
impl TryFrom<Value> for (Value, Value, Value, Value) {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::Tuple4(tuple) => Ok(Arc::try_unwrap(tuple).unwrap_or_else(|arc| (*arc).clone())),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "(Value, Value, Value, Value)",
message: Some("Expected Tuple4 variant".to_owned()),
}),
}
}
}
impl TryFrom<Value> for (Value, Value, Value, Value, Value) {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::Tuple5(tuple) => Ok(Arc::try_unwrap(tuple).unwrap_or_else(|arc| (*arc).clone())),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "(Value, Value, Value, Value, Value)",
message: Some("Expected Tuple5 variant".to_owned()),
}),
}
}
}
impl TryFrom<Value> for (Value, Value, Value, Value, Value, Value) {
type Error = MinarrowError;
fn try_from(v: Value) -> Result<Self, Self::Error> {
match v {
Value::Tuple6(tuple) => Ok(Arc::try_unwrap(tuple).unwrap_or_else(|arc| (*arc).clone())),
_ => Err(MinarrowError::TypeError {
from: "Value",
to: "(Value, Value, Value, Value, Value, Value)",
message: Some("Expected Tuple6 variant".to_owned()),
}),
}
}
}
#[cfg(feature = "extended_numeric_types")]
#[macro_export]
macro_rules! val_i8 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_i8![$($x)*])
};
}
#[cfg(feature = "extended_numeric_types")]
#[macro_export]
macro_rules! val_i16 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_i16![$($x)*])
};
}
#[macro_export]
macro_rules! val_i32 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_i32![$($x)*])
};
}
#[macro_export]
macro_rules! val_i64 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_i64![$($x)*])
};
}
#[cfg(feature = "extended_numeric_types")]
#[macro_export]
macro_rules! val_u8 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_u8![$($x)*])
};
}
#[cfg(feature = "extended_numeric_types")]
#[macro_export]
macro_rules! val_u16 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_u16![$($x)*])
};
}
#[macro_export]
macro_rules! val_u32 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_u32![$($x)*])
};
}
#[macro_export]
macro_rules! val_u64 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_u64![$($x)*])
};
}
#[macro_export]
macro_rules! val_f32 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_f32![$($x)*])
};
}
#[macro_export]
macro_rules! val_f64 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_f64![$($x)*])
};
}
#[macro_export]
macro_rules! val_bool {
($($x:tt)*) => {
$crate::Value::from($crate::arr_bool![$($x)*])
};
}
#[macro_export]
macro_rules! val_str32 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_str32![$($x)*])
};
}
#[cfg(feature = "large_string")]
#[macro_export]
macro_rules! val_str64 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_str64![$($x)*])
};
}
#[cfg(feature = "default_categorical_8")]
#[macro_export]
macro_rules! val_cat8 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_cat8![$($x)*])
};
}
#[cfg(feature = "extended_categorical")]
#[macro_export]
macro_rules! val_cat16 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_cat16![$($x)*])
};
}
#[cfg(any(not(feature = "default_categorical_8"), feature = "extended_categorical"))]
#[macro_export]
macro_rules! val_cat32 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_cat32![$($x)*])
};
}
#[cfg(feature = "extended_categorical")]
#[macro_export]
macro_rules! val_cat64 {
($($x:tt)*) => {
$crate::Value::from($crate::arr_cat64![$($x)*])
};
}
#[cfg(feature = "scalar_type")]
#[cfg(feature = "extended_numeric_types")]
#[macro_export]
macro_rules! val_scalar_i8 {
($v:expr) => {
$crate::Value::from($crate::Scalar::Int8($v))
};
}
#[cfg(feature = "scalar_type")]
#[cfg(feature = "extended_numeric_types")]
#[macro_export]
macro_rules! val_scalar_i16 {
($v:expr) => {
$crate::Value::from($crate::Scalar::Int16($v))
};
}
#[cfg(feature = "scalar_type")]
#[macro_export]
macro_rules! val_scalar_i32 {
($v:expr) => {
$crate::Value::from($crate::Scalar::Int32($v))
};
}
#[cfg(feature = "scalar_type")]
#[macro_export]
macro_rules! val_scalar_i64 {
($v:expr) => {
$crate::Value::from($crate::Scalar::Int64($v))
};
}
#[cfg(feature = "scalar_type")]
#[cfg(feature = "extended_numeric_types")]
#[macro_export]
macro_rules! val_scalar_u8 {
($v:expr) => {
$crate::Value::from($crate::Scalar::UInt8($v))
};
}
#[cfg(feature = "scalar_type")]
#[cfg(feature = "extended_numeric_types")]
#[macro_export]
macro_rules! val_scalar_u16 {
($v:expr) => {
$crate::Value::from($crate::Scalar::UInt16($v))
};
}
#[cfg(feature = "scalar_type")]
#[macro_export]
macro_rules! val_scalar_u32 {
($v:expr) => {
$crate::Value::from($crate::Scalar::UInt32($v))
};
}
#[cfg(feature = "scalar_type")]
#[macro_export]
macro_rules! val_scalar_u64 {
($v:expr) => {
$crate::Value::from($crate::Scalar::UInt64($v))
};
}
#[cfg(feature = "scalar_type")]
#[macro_export]
macro_rules! val_scalar_f32 {
($v:expr) => {
$crate::Value::from($crate::Scalar::Float32($v))
};
}
#[cfg(feature = "scalar_type")]
#[macro_export]
macro_rules! val_scalar_f64 {
($v:expr) => {
$crate::Value::from($crate::Scalar::Float64($v))
};
}
#[cfg(feature = "scalar_type")]
#[macro_export]
macro_rules! val_scalar_bool {
($v:expr) => {
$crate::Value::from($crate::Scalar::Boolean($v))
};
}
#[cfg(feature = "scalar_type")]
#[macro_export]
macro_rules! val_scalar_str32 {
($v:expr) => {
$crate::Value::from($crate::Scalar::String32($v.to_string()))
};
}
#[cfg(feature = "scalar_type")]
#[cfg(feature = "large_string")]
#[macro_export]
macro_rules! val_scalar_str64 {
($v:expr) => {
$crate::Value::from($crate::Scalar::String64($v.to_string()))
};
}
#[cfg(feature = "scalar_type")]
#[macro_export]
macro_rules! val_scalar_null {
() => {
$crate::Value::from($crate::Scalar::Null)
};
}
#[cfg(test)]
mod accessor_tests {
use super::*;
use crate::structs::field_array::field_array;
use crate::structs::variants::integer::IntegerArray;
#[test]
fn test_arr_accessor() {
let arr = IntegerArray::<i32>::from_slice(&[1, 2, 3]);
let val = Value::Array(Arc::new(Array::from_int32(arr)));
let array_ref = val.arr();
assert_eq!(array_ref.len(), 3);
let result = val.try_arr();
assert!(result.is_ok());
assert_eq!(result.unwrap().len(), 3);
}
#[test]
#[should_panic(expected = "Expected Value::Array")]
fn test_arr_accessor_wrong_type_panics() {
let val = Value::Table(Arc::new(Table::new_empty()));
let _ = val.arr();
}
#[test]
fn test_try_arr_accessor_wrong_type_returns_error() {
let val = Value::Table(Arc::new(Table::new_empty()));
let result = val.try_arr();
assert!(result.is_err());
}
#[test]
fn test_table_accessor() {
let table = Table::new_empty();
let val = Value::Table(Arc::new(table));
let table_ref = val.table();
assert_eq!(table_ref.n_rows, 0);
let result = val.try_table();
assert!(result.is_ok());
}
#[test]
fn test_fa_accessor() {
let arr = IntegerArray::<i32>::from_slice(&[1, 2, 3]);
let fa = field_array("test", Array::from_int32(arr));
let val = Value::FieldArray(Arc::new(fa));
let fa_ref = val.fa();
assert_eq!(fa_ref.field.name, "test");
assert_eq!(fa_ref.len(), 3);
let result = val.try_fa();
assert!(result.is_ok());
}
#[test]
fn test_vec_val_accessor() {
let arr = IntegerArray::<i32>::from_slice(&[1, 2]);
let inner = Value::Array(Arc::new(Array::from_int32(arr)));
let val = Value::VecValue(Arc::new(vec![inner.clone(), inner]));
let vec_ref = val.vec_val();
assert_eq!(vec_ref.len(), 2);
let result = val.try_vec_val();
assert!(result.is_ok());
}
#[test]
fn test_box_val_accessor() {
let arr = IntegerArray::<i32>::from_slice(&[1, 2, 3]);
let inner = Value::Array(Arc::new(Array::from_int32(arr)));
let val = Value::BoxValue(Box::new(inner));
let inner_ref = val.box_val();
assert_eq!(inner_ref.len(), 3);
let result = val.try_box_val();
assert!(result.is_ok());
}
#[test]
fn test_arc_val_accessor() {
let arr = IntegerArray::<i32>::from_slice(&[1, 2, 3]);
let inner = Value::Array(Arc::new(Array::from_int32(arr)));
let val = Value::ArcValue(Arc::new(inner));
let inner_ref = val.arc_val();
assert_eq!(inner_ref.len(), 3);
let result = val.try_arc_val();
assert!(result.is_ok());
}
#[test]
fn test_tuple_accessors() {
let arr1 = Value::Array(Arc::new(Array::from_int32(IntegerArray::from_slice(&[1]))));
let arr2 = Value::Array(Arc::new(Array::from_int32(IntegerArray::from_slice(&[2]))));
let arr3 = Value::Array(Arc::new(Array::from_int32(IntegerArray::from_slice(&[3]))));
let t2 = Value::Tuple2(Arc::new((arr1.clone(), arr2.clone())));
let (a, b) = t2.t2();
assert_eq!(a.len(), 1);
assert_eq!(b.len(), 1);
assert!(t2.try_t2().is_ok());
let t3 = Value::Tuple3(Arc::new((arr1.clone(), arr2.clone(), arr3.clone())));
let (a, b, c) = t3.t3();
assert_eq!(a.len(), 1);
assert_eq!(b.len(), 1);
assert_eq!(c.len(), 1);
assert!(t3.try_t3().is_ok());
}
#[cfg(feature = "scalar_type")]
#[test]
fn test_scalar_accessor() {
let val = Value::Scalar(Scalar::Int32(42));
let scalar_ref = val.scalar();
assert_eq!(scalar_ref.i32(), 42);
let result = val.try_scalar();
assert!(result.is_ok());
}
#[cfg(feature = "views")]
#[test]
fn test_av_accessor() {
let arr = IntegerArray::<i32>::from_slice(&[1, 2, 3]);
let array = Array::from_int32(arr);
let view = ArrayV::new(array, 0, 3);
let val = Value::ArrayView(Arc::new(view));
let av_ref = val.av();
assert_eq!(av_ref.len(), 3);
let result = val.try_av();
assert!(result.is_ok());
}
#[cfg(feature = "views")]
#[test]
fn test_tv_accessor() {
let table = Table::new_empty();
let view = TableV::from_table(table, 0, 0);
let val = Value::TableView(Arc::new(view));
let tv_ref = val.tv();
assert_eq!(tv_ref.len(), 0);
let result = val.try_tv();
assert!(result.is_ok());
}
#[cfg(feature = "chunked")]
#[test]
fn test_sa_accessor() {
let sa = SuperArray::new();
let val = Value::SuperArray(Arc::new(sa));
let sa_ref = val.sa();
assert_eq!(sa_ref.len(), 0);
let result = val.try_sa();
assert!(result.is_ok());
}
#[cfg(feature = "chunked")]
#[test]
fn test_st_accessor() {
let st = SuperTable::new("test".to_string());
let val = Value::SuperTable(Arc::new(st));
let st_ref = val.st();
assert_eq!(st_ref.len(), 0);
let result = val.try_st();
assert!(result.is_ok());
}
}