#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
pub mod error;
use super::{qattribute, qinf_base, qninf_base, qnull_base, qtype};
use chrono::prelude::*;
use chrono::Duration;
use error::Error;
use std::any::Any;
use std::fmt;
use std::result::Result as StdResult;
pub type Result<T> = StdResult<T, Error>;
pub const ONE_DAY_NANOS: i64 = 86400000000000;
pub const ONE_DAY_MILLIS: i64 = 86400000;
pub const KDB_MONTH_OFFSET: i32 = 360;
pub const KDB_DAY_OFFSET: i32 = 10957;
pub const KDB_TIMESTAMP_OFFSET: i64 = 946684800000000000;
pub mod qnull {
use super::qnull_base;
use chrono::prelude::*;
use chrono::Duration;
use once_cell::sync::Lazy;
pub const GUID: [u8; 16] = [0_u8; 16];
pub const SHORT: i16 = qnull_base::H;
pub const INT: i32 = qnull_base::I;
pub const LONG: i64 = qnull_base::J;
pub const REAL: f32 = qnull_base::E;
pub const FLOAT: f64 = qnull_base::F;
pub const CHAR: char = qnull_base::C;
pub const SYMBOL: String = String::new();
pub const TIMESTAMP: Lazy<DateTime<Utc>> = Lazy::new(|| {
NaiveDate::from_ymd_opt(1707, 9, 22)
.unwrap()
.and_hms_nano_opt(0, 12, 43, 145224192)
.unwrap()
.and_local_timezone(Utc)
.unwrap()
});
pub const MONTH: NaiveDate = NaiveDate::MIN;
pub const DATE: NaiveDate = NaiveDate::MIN;
pub const DATETIME: DateTime<Utc> = DateTime::<Utc>::MIN_UTC;
pub const TIMESPAN: Lazy<Duration> = Lazy::new(|| Duration::nanoseconds(qnull_base::J));
pub const MINUTE: Lazy<Duration> = Lazy::new(|| Duration::minutes(qnull_base::I as i64));
pub const SECOND: Lazy<Duration> = Lazy::new(|| Duration::seconds(qnull_base::I as i64));
pub const TIME: Lazy<Duration> = Lazy::new(|| Duration::milliseconds(qnull_base::I as i64));
}
pub mod qinf {
use super::qinf_base;
use chrono::prelude::*;
use chrono::Duration;
use once_cell::sync::Lazy;
pub const SHORT: i16 = qinf_base::H;
pub const INT: i32 = qinf_base::I;
pub const LONG: i64 = qinf_base::J;
pub const REAL: f32 = qinf_base::E;
pub const FLOAT: f64 = qinf_base::F;
pub const TIMESTAMP: Lazy<DateTime<Utc>> = Lazy::new(|| {
NaiveDate::from_ymd_opt(2292, 4, 10)
.unwrap()
.and_hms_nano_opt(23, 47, 16, 854775807)
.unwrap()
.and_local_timezone(Utc)
.unwrap()
});
pub const MONTH: Lazy<NaiveDate> = Lazy::new(|| NaiveDate::MAX - Duration::days(30));
pub const DATE: NaiveDate = NaiveDate::MAX;
pub const DATETIME: Lazy<DateTime<Utc>> =
Lazy::new(|| DateTime::<Utc>::MAX_UTC - Duration::nanoseconds(999999));
pub const TIMESPAN: Lazy<Duration> = Lazy::new(|| Duration::nanoseconds(qinf_base::J));
pub const MINUTE: Lazy<Duration> = Lazy::new(|| Duration::minutes(qinf_base::I as i64));
pub const SECOND: Lazy<Duration> = Lazy::new(|| Duration::seconds(qinf_base::I as i64));
pub const TIME: Lazy<Duration> = Lazy::new(|| Duration::milliseconds(qinf_base::I as i64));
}
pub mod qninf {
use super::qninf_base;
use chrono::prelude::*;
use chrono::Duration;
use once_cell::sync::Lazy;
pub const SHORT: i16 = qninf_base::H;
pub const INT: i32 = qninf_base::I;
pub const LONG: i64 = qninf_base::J;
pub const REAL: f32 = qninf_base::E;
pub const FLOAT: f64 = qninf_base::F;
pub const TIMESTAMP: Lazy<DateTime<Utc>> = Lazy::new(|| {
NaiveDate::from_ymd_opt(1707, 9, 22)
.unwrap()
.and_hms_nano_opt(0, 12, 43, 145224193)
.unwrap()
.and_local_timezone(Utc)
.unwrap()
});
pub const MONTH: Lazy<NaiveDate> = Lazy::new(|| NaiveDate::MIN + Duration::days(31));
pub const DATE: Lazy<NaiveDate> = Lazy::new(|| NaiveDate::MIN + Duration::days(1));
pub const DATETIME: Lazy<DateTime<Utc>> =
Lazy::new(|| DateTime::<Utc>::MIN_UTC + Duration::nanoseconds(1000000));
pub const TIMESPAN: Lazy<Duration> = Lazy::new(|| Duration::nanoseconds(qninf_base::J));
pub const MINUTE: Lazy<Duration> = Lazy::new(|| Duration::minutes(qninf_base::I as i64));
pub const SECOND: Lazy<Duration> = Lazy::new(|| Duration::seconds(qninf_base::I as i64));
pub const TIME: Lazy<Duration> = Lazy::new(|| Duration::milliseconds(qninf_base::I as i64));
}
pub type S = String;
pub type C = i8;
pub type G = u8;
pub type H = i16;
pub type I = i32;
pub type J = i64;
pub type E = f32;
pub type F = f64;
pub type U = [G; 16];
pub(crate) trait AsAny {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
pub(crate) trait Klone {
fn clone_box(&self) -> Box<dyn k0_list_inner>;
}
pub(crate) trait k0_list_inner: Klone + fmt::Debug + AsAny + Send + Sync + 'static {
fn len(&self) -> usize;
}
#[derive(Debug)]
pub(crate) struct k0_list {
n: J,
G0: Box<dyn k0_list_inner>,
}
#[derive(Clone, Debug)]
pub(crate) enum k0_inner {
byte(G),
guid(U),
short(H),
int(I),
long(J),
real(E),
float(F),
symbol(S),
table(K),
list(k0_list),
null(()),
}
#[derive(Clone, Debug)]
pub(crate) struct k0 {
qtype: i8,
attribute: i8,
value: k0_inner,
}
#[derive(Clone, Debug)]
pub struct K(pub(crate) Box<k0>);
impl AsAny for Vec<G> {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl AsAny for Vec<U> {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl AsAny for Vec<H> {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl AsAny for Vec<I> {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl AsAny for Vec<J> {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl AsAny for Vec<E> {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl AsAny for Vec<F> {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl AsAny for String {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl AsAny for Vec<S> {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl AsAny for Vec<K> {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
impl<T> Klone for T
where
T: k0_list_inner + Clone,
{
fn clone_box(&self) -> Box<dyn k0_list_inner> {
Box::new(self.clone())
}
}
impl k0_list_inner for Vec<G> {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list_inner for Vec<U> {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list_inner for Vec<H> {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list_inner for Vec<I> {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list_inner for Vec<J> {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list_inner for Vec<E> {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list_inner for Vec<F> {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list_inner for String {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list_inner for Vec<S> {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list_inner for Vec<K> {
fn len(&self) -> usize {
self.len()
}
}
impl k0_list {
pub(crate) fn new<T>(array: T) -> Self
where
T: k0_list_inner,
{
k0_list {
n: array.len() as J,
G0: Box::new(array),
}
}
}
impl Clone for k0_list {
fn clone(&self) -> Self {
k0_list {
n: self.n,
G0: self.G0.clone_box(),
}
}
}
impl K {
pub(crate) fn new(qtype: i8, attribute: i8, inner: k0_inner) -> Self {
K(Box::new(k0 {
qtype: qtype,
attribute: attribute,
value: inner,
}))
}
pub fn new_bool(boolean: bool) -> Self {
K::new(
qtype::BOOL_ATOM,
qattribute::NONE,
k0_inner::byte(boolean as u8),
)
}
pub fn new_guid(guid: [G; 16]) -> Self {
K::new(qtype::GUID_ATOM, qattribute::NONE, k0_inner::guid(guid))
}
pub fn new_byte(byte: u8) -> Self {
K::new(qtype::BYTE_ATOM, qattribute::NONE, k0_inner::byte(byte))
}
pub fn new_short(short: i16) -> Self {
K::new(qtype::SHORT_ATOM, qattribute::NONE, k0_inner::short(short))
}
pub fn new_int(int: i32) -> Self {
K::new(qtype::INT_ATOM, qattribute::NONE, k0_inner::int(int))
}
pub fn new_long(long: i64) -> Self {
K::new(qtype::LONG_ATOM, qattribute::NONE, k0_inner::long(long))
}
pub fn new_real(real: f32) -> Self {
K::new(qtype::REAL_ATOM, qattribute::NONE, k0_inner::real(real))
}
pub fn new_float(float: f64) -> Self {
K::new(qtype::FLOAT_ATOM, qattribute::NONE, k0_inner::float(float))
}
pub fn new_char(character: char) -> Self {
K::new(
qtype::CHAR,
qattribute::NONE,
k0_inner::byte(character as G),
)
}
pub fn new_symbol(symbol: String) -> Self {
K::new(
qtype::SYMBOL_ATOM,
qattribute::NONE,
k0_inner::symbol(symbol),
)
}
pub fn new_timestamp(timestamp: DateTime<Utc>) -> Self {
K::new(
qtype::TIMESTAMP_ATOM,
qattribute::NONE,
k0_inner::long(datetime_to_q_timestamp(timestamp)),
)
}
pub fn new_month(month: NaiveDate) -> Self {
K::new(
qtype::MONTH_ATOM,
qattribute::NONE,
k0_inner::int(date_to_q_month(month)),
)
}
pub fn new_date(date: NaiveDate) -> Self {
K::new(
qtype::DATE_ATOM,
qattribute::NONE,
k0_inner::int(date_to_q_date(date)),
)
}
pub fn new_datetime(datetime: DateTime<Utc>) -> Self {
K::new(
qtype::DATETIME_ATOM,
qattribute::NONE,
k0_inner::float(datetime_to_q_datetime(datetime)),
)
}
pub fn new_timespan(duration: Duration) -> Self {
K::new(
qtype::TIMESPAN_ATOM,
qattribute::NONE,
k0_inner::long(duration.num_nanoseconds().expect("duration overflow")),
)
}
pub fn new_minute(minute: Duration) -> Self {
K::new(
qtype::MINUTE_ATOM,
qattribute::NONE,
k0_inner::int(minute.num_minutes() as i32),
)
}
pub fn new_second(second: Duration) -> Self {
K::new(
qtype::SECOND_ATOM,
qattribute::NONE,
k0_inner::int(second.num_seconds() as i32),
)
}
pub fn new_time(time: Duration) -> Self {
K::new(
qtype::TIME_ATOM,
qattribute::NONE,
k0_inner::int(time.num_milliseconds() as i32),
)
}
pub fn new_bool_list(list: Vec<bool>, attribute: i8) -> Self {
let array = list
.into_iter()
.map(|element| match element {
true => 1,
false => 0,
})
.collect::<Vec<G>>();
K::new(
qtype::BOOL_LIST,
attribute,
k0_inner::list(k0_list::new(array)),
)
}
pub fn new_guid_list(list: Vec<[u8; 16]>, attribute: i8) -> Self {
K::new(
qtype::GUID_LIST,
attribute,
k0_inner::list(k0_list::new(list)),
)
}
pub fn new_byte_list(list: Vec<u8>, attribute: i8) -> Self {
K::new(
qtype::BYTE_LIST,
attribute,
k0_inner::list(k0_list::new(list)),
)
}
pub fn new_short_list(list: Vec<i16>, attribute: i8) -> Self {
K::new(
qtype::SHORT_LIST,
attribute,
k0_inner::list(k0_list::new(list)),
)
}
pub fn new_int_list(list: Vec<i32>, attribute: i8) -> Self {
K::new(
qtype::INT_LIST,
attribute,
k0_inner::list(k0_list::new(list)),
)
}
pub fn new_long_list(list: Vec<i64>, attribute: i8) -> Self {
K::new(
qtype::LONG_LIST,
attribute,
k0_inner::list(k0_list::new(list)),
)
}
pub fn new_real_list(list: Vec<f32>, attribute: i8) -> Self {
K::new(
qtype::REAL_LIST,
attribute,
k0_inner::list(k0_list::new(list)),
)
}
pub fn new_float_list(list: Vec<f64>, attribute: i8) -> Self {
K::new(
qtype::FLOAT_LIST,
attribute,
k0_inner::list(k0_list::new(list)),
)
}
pub fn new_string(string: String, attribute: i8) -> Self {
K::new(qtype::STRING, attribute, k0_inner::symbol(string))
}
pub fn new_symbol_list(list: Vec<String>, attribute: i8) -> Self {
K::new(
qtype::SYMBOL_LIST,
attribute,
k0_inner::list(k0_list::new(list)),
)
}
pub fn new_timestamp_list(list: Vec<DateTime<Utc>>, attribute: i8) -> Self {
let array = list
.into_iter()
.map(|datetime| datetime_to_q_timestamp(datetime))
.collect::<Vec<J>>();
K::new(
qtype::TIMESTAMP_LIST,
attribute,
k0_inner::list(k0_list::new(array)),
)
}
pub fn new_month_list(list: Vec<NaiveDate>, attribute: i8) -> Self {
let array = list
.into_iter()
.map(|date| date_to_q_month(date))
.collect::<Vec<I>>();
K::new(
qtype::MONTH_LIST,
attribute,
k0_inner::list(k0_list::new(array)),
)
}
pub fn new_date_list(list: Vec<NaiveDate>, attribute: i8) -> Self {
let array = list
.into_iter()
.map(|date| date_to_q_date(date))
.collect::<Vec<I>>();
K::new(
qtype::DATE_LIST,
attribute,
k0_inner::list(k0_list::new(array)),
)
}
pub fn new_datetime_list(list: Vec<DateTime<Utc>>, attribute: i8) -> Self {
let array = list
.into_iter()
.map(|datetime| datetime_to_q_datetime(datetime))
.collect::<Vec<F>>();
K::new(
qtype::DATETIME_LIST,
attribute,
k0_inner::list(k0_list::new(array)),
)
}
pub fn new_timespan_list(list: Vec<Duration>, attribute: i8) -> Self {
let array = list
.into_iter()
.map(|duration| duration.num_nanoseconds().expect("duration overflow"))
.collect::<Vec<J>>();
K::new(
qtype::TIMESPAN_LIST,
attribute,
k0_inner::list(k0_list::new(array)),
)
}
pub fn new_minute_list(list: Vec<Duration>, attribute: i8) -> Self {
let array = list
.into_iter()
.map(|duration| duration.num_minutes() as i32)
.collect::<Vec<I>>();
K::new(
qtype::MINUTE_LIST,
attribute,
k0_inner::list(k0_list::new(array)),
)
}
pub fn new_second_list(list: Vec<Duration>, attribute: i8) -> Self {
let array = list
.into_iter()
.map(|duration| duration.num_seconds() as i32)
.collect::<Vec<I>>();
K::new(
qtype::SECOND_LIST,
attribute,
k0_inner::list(k0_list::new(array)),
)
}
pub fn new_time_list(list: Vec<Duration>, attribute: i8) -> Self {
let array = list
.into_iter()
.map(|duration| duration.num_milliseconds() as i32)
.collect::<Vec<I>>();
K::new(
qtype::TIME_LIST,
attribute,
k0_inner::list(k0_list::new(array)),
)
}
pub fn new_compound_list(list: Vec<K>) -> Self {
K::new(
qtype::COMPOUND_LIST,
qattribute::NONE,
k0_inner::list(k0_list::new(list)),
)
}
pub fn new_dictionary(keys: K, values: K) -> Result<Self> {
if keys.len() != values.len() {
Err(Error::length_mismatch(keys.len(), values.len()))
} else {
let qtype = if keys.0.attribute == qattribute::SORTED {
qtype::SORTED_DICTIONARY
} else {
qtype::DICTIONARY
};
Ok(K::new(
qtype,
qattribute::NONE,
k0_inner::list(k0_list::new(vec![keys, values])),
))
}
}
pub fn new_null() -> Self {
K::new(qtype::NULL, qattribute::NONE, k0_inner::null(()))
}
pub fn new_error(error: String) -> Self {
K::new(qtype::ERROR, qattribute::NONE, k0_inner::symbol(error))
}
pub fn get_bool(&self) -> Result<bool> {
match self.0.qtype {
qtype::BOOL_ATOM => match self.0.value {
k0_inner::byte(boolean) => Ok(boolean != 0),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::BOOL_ATOM)),
}
}
pub fn get_guid(&self) -> Result<[u8; 16]> {
match self.0.qtype {
qtype::GUID_ATOM => match self.0.value {
k0_inner::guid(guid) => Ok(guid),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::GUID_ATOM)),
}
}
pub fn get_byte(&self) -> Result<u8> {
match self.0.qtype {
qtype::BOOL_ATOM | qtype::BYTE_ATOM | qtype::CHAR => match self.0.value {
k0_inner::byte(byte) => Ok(byte),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::BYTE_ATOM)),
}
}
pub fn get_short(&self) -> Result<i16> {
match self.0.qtype {
qtype::SHORT_ATOM => match self.0.value {
k0_inner::short(short) => Ok(short),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::SHORT_ATOM)),
}
}
pub fn get_int(&self) -> Result<i32> {
match self.0.qtype {
qtype::INT_ATOM
| qtype::MONTH_ATOM
| qtype::DATE_ATOM
| qtype::MINUTE_ATOM
| qtype::SECOND_ATOM
| qtype::TIME_ATOM => match self.0.value {
k0_inner::int(int) => Ok(int),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::INT_ATOM)),
}
}
pub fn get_long(&self) -> Result<i64> {
match self.0.qtype {
qtype::LONG_ATOM | qtype::TIMESTAMP_ATOM | qtype::TIMESPAN_ATOM => match self.0.value {
k0_inner::long(long) => Ok(long),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::LONG_ATOM)),
}
}
pub fn get_real(&self) -> Result<f32> {
match self.0.qtype {
qtype::REAL_ATOM => match self.0.value {
k0_inner::real(real) => Ok(real),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::REAL_ATOM)),
}
}
pub fn get_float(&self) -> Result<f64> {
match self.0.qtype {
qtype::FLOAT_ATOM | qtype::DATETIME_ATOM => match self.0.value {
k0_inner::float(float) => Ok(float),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::FLOAT_ATOM)),
}
}
pub fn get_char(&self) -> Result<char> {
match self.0.qtype {
qtype::CHAR => match self.0.value {
k0_inner::byte(ch) => Ok(ch as char),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::CHAR)),
}
}
pub fn get_symbol(&self) -> Result<&str> {
match self.0.qtype {
qtype::SYMBOL_ATOM => match &self.0.value {
k0_inner::symbol(symbol) => Ok(symbol),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::SYMBOL_ATOM)),
}
}
pub fn get_timestamp(&self) -> Result<DateTime<Utc>> {
match self.0.qtype {
qtype::TIMESTAMP_ATOM => match self.0.value {
k0_inner::long(nanos) => Ok(q_timestamp_to_datetime(nanos)),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::TIMESTAMP_ATOM)),
}
}
pub fn get_month(&self) -> Result<NaiveDate> {
match self.0.qtype {
qtype::MONTH_ATOM => match self.0.value {
k0_inner::int(months) => Ok(q_month_to_date(months)),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::MONTH_ATOM)),
}
}
pub fn get_date(&self) -> Result<NaiveDate> {
match self.0.qtype {
qtype::DATE_ATOM => match self.0.value {
k0_inner::int(days) => Ok(q_date_to_date(days)?),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::DATE_ATOM)),
}
}
pub fn get_datetime(&self) -> Result<DateTime<Utc>> {
match self.0.qtype {
qtype::DATETIME_ATOM => match self.0.value {
k0_inner::float(days) => Ok(q_datetime_to_datetime(days)),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::DATETIME_ATOM)),
}
}
pub fn get_timespan(&self) -> Result<Duration> {
match self.0.qtype {
qtype::TIMESPAN_ATOM => match self.0.value {
k0_inner::long(nanos) => Ok(q_timespan_to_duration(nanos)),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::TIMESPAN_ATOM)),
}
}
pub fn get_minute(&self) -> Result<Duration> {
match self.0.qtype {
qtype::MINUTE_ATOM => match self.0.value {
k0_inner::int(minutes) => Ok(q_minute_to_duration(minutes)),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::MINUTE_ATOM)),
}
}
pub fn get_second(&self) -> Result<Duration> {
match self.0.qtype {
qtype::SECOND_ATOM => match self.0.value {
k0_inner::int(seconds) => Ok(q_second_to_duration(seconds)),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::SECOND_ATOM)),
}
}
pub fn get_time(&self) -> Result<Duration> {
match self.0.qtype {
qtype::TIME_ATOM => match self.0.value {
k0_inner::int(millis) => Ok(q_time_to_duration(millis)),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::TIME_ATOM)),
}
}
pub fn get_dictionary(&self) -> Result<&K> {
match self.0.qtype {
qtype::TABLE => match &self.0.value {
k0_inner::table(dictionary) => Ok(dictionary),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::TABLE)),
}
}
pub fn get_mut_dictionary(&mut self) -> Result<&mut K> {
match self.0.qtype {
qtype::TABLE => match &mut self.0.value {
k0_inner::table(dictionary) => Ok(dictionary),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::TABLE)),
}
}
pub fn get_error_string(&self) -> Result<&str> {
match self.0.qtype {
qtype::ERROR => match &self.0.value {
k0_inner::symbol(error) => Ok(error),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::ERROR)),
}
}
pub fn as_string(&self) -> Result<&str> {
match self.0.qtype {
qtype::STRING => match &self.0.value {
k0_inner::symbol(string) => Ok(string),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::STRING)),
}
}
pub fn as_mut_string(&mut self) -> Result<&mut String> {
match self.0.qtype {
qtype::STRING => match &mut self.0.value {
k0_inner::symbol(string) => Ok(string),
_ => unreachable!(),
},
_ => Err(Error::invalid_cast(self.0.qtype, qtype::STRING)),
}
}
pub fn as_mut_vec<T>(&mut self) -> Result<&mut Vec<T>>
where
T: 'static,
{
match self.0.qtype {
qtype::COMPOUND_LIST
| qtype::BOOL_LIST
| qtype::GUID_LIST
| qtype::BYTE_LIST
| qtype::SHORT_LIST
| qtype::INT_LIST
| qtype::LONG_LIST
| qtype::REAL_LIST
| qtype::FLOAT_LIST
| qtype::SYMBOL_LIST
| qtype::TIMESTAMP_LIST
| qtype::MONTH_LIST
| qtype::DATE_LIST
| qtype::DATETIME_LIST
| qtype::TIMESPAN_LIST
| qtype::MINUTE_LIST
| qtype::SECOND_LIST
| qtype::TIME_LIST
| qtype::DICTIONARY
| qtype::SORTED_DICTIONARY => match &mut self.0.value {
k0_inner::list(list) => match list.G0.as_any_mut().downcast_mut::<Vec<T>>() {
Some(vector) => Ok(vector),
_ => Err(Error::invalid_cast_list(self.0.qtype)),
},
_ => unreachable!(),
},
_ => Err(Error::invalid_cast_list(self.0.qtype)),
}
}
pub fn as_vec<T>(&self) -> Result<&Vec<T>>
where
T: 'static,
{
match self.0.qtype {
qtype::COMPOUND_LIST
| qtype::BOOL_LIST
| qtype::GUID_LIST
| qtype::BYTE_LIST
| qtype::SHORT_LIST
| qtype::INT_LIST
| qtype::LONG_LIST
| qtype::REAL_LIST
| qtype::FLOAT_LIST
| qtype::SYMBOL_LIST
| qtype::TIMESTAMP_LIST
| qtype::MONTH_LIST
| qtype::DATE_LIST
| qtype::DATETIME_LIST
| qtype::TIMESPAN_LIST
| qtype::MINUTE_LIST
| qtype::SECOND_LIST
| qtype::TIME_LIST
| qtype::DICTIONARY
| qtype::SORTED_DICTIONARY => match &self.0.value {
k0_inner::list(list) => match list.G0.as_any().downcast_ref::<Vec<T>>() {
Some(vector) => Ok(vector),
_ => Err(Error::invalid_cast_list(self.0.qtype)),
},
_ => unreachable!(),
},
_ => Err(Error::invalid_cast_list(self.0.qtype)),
}
}
pub fn get_column<T>(&self, column: T) -> Result<&K>
where
T: ToString,
{
match self.0.qtype {
qtype::TABLE => {
let dictionary = self.get_dictionary().unwrap().as_vec::<K>().unwrap();
match dictionary[0]
.as_vec::<S>()
.unwrap()
.iter()
.position(|name| *name == column.to_string())
{
Some(index) => Ok(&dictionary[1].as_vec::<K>().unwrap()[index]),
_ => Err(Error::no_such_column(column.to_string())),
}
}
qtype::DICTIONARY => {
let key_value = self.as_vec::<K>().unwrap();
if key_value[0].0.qtype == qtype::TABLE {
if let Ok(found_column) = key_value[0].get_column(column.to_string()) {
Ok(found_column)
} else if let Ok(found_column) = key_value[1].get_column(column.to_string()) {
Ok(found_column)
} else {
Err(Error::no_such_column(column.to_string()))
}
} else {
Err(Error::invalid_operation("get_column", self.0.qtype, None))
}
}
_ => Err(Error::invalid_operation("get_column", self.0.qtype, None)),
}
}
pub fn get_mut_column<T>(&mut self, column: T) -> Result<&mut K>
where
T: ToString,
{
match self.0.qtype {
qtype::TABLE => {
let dictionary = self
.get_mut_dictionary()
.unwrap()
.as_mut_vec::<K>()
.unwrap();
match dictionary[0]
.as_vec::<S>()
.unwrap()
.iter()
.position(|name| *name == column.to_string())
{
Some(index) => Ok(&mut dictionary[1].as_mut_vec::<K>().unwrap()[index]),
_ => Err(Error::no_such_column(column.to_string())),
}
}
qtype::DICTIONARY => {
let key_value = self.as_vec::<K>().unwrap();
if key_value[0].0.qtype == qtype::TABLE {
let mut dictionary = key_value[0]
.get_dictionary()
.unwrap()
.as_vec::<K>()
.unwrap();
if let Some(index) = dictionary[0]
.as_vec::<S>()
.unwrap()
.iter()
.position(|name| *name == column.to_string())
{
return Ok(&mut self.as_mut_vec::<K>().unwrap()[0]
.get_mut_dictionary()
.unwrap()
.as_mut_vec::<K>()
.unwrap()[1]
.as_mut_vec::<K>()
.unwrap()[index]);
}
dictionary = key_value[1]
.get_dictionary()
.unwrap()
.as_vec::<K>()
.unwrap();
if let Some(index) = dictionary[0]
.as_vec::<S>()
.unwrap()
.iter()
.position(|name| *name == column.to_string())
{
Ok(&mut self.as_mut_vec::<K>().unwrap()[1]
.get_mut_dictionary()
.unwrap()
.as_mut_vec::<K>()
.unwrap()[1]
.as_mut_vec::<K>()
.unwrap()[index])
} else {
Err(Error::no_such_column(column.to_string()))
}
} else {
Err(Error::invalid_operation(
"get_mut_column",
self.0.qtype,
None,
))
}
}
_ => Err(Error::invalid_operation(
"get_mut_column",
self.0.qtype,
None,
)),
}
}
pub fn get_type(&self) -> i8 {
self.0.qtype
}
pub fn get_attribute(&self) -> i8 {
self.0.attribute
}
pub fn set_attribute(&mut self, attribute: i8) {
self.0.attribute = attribute;
}
fn increment(&mut self) {
match &mut self.0.value {
k0_inner::list(list) => list.n += 1,
_ => unreachable!(),
}
}
fn decrement(&mut self) {
match &mut self.0.value {
k0_inner::list(list) => list.n -= 1,
_ => unreachable!(),
}
}
pub fn push(&mut self, element: &dyn Any) -> Result<()> {
match self.0.qtype {
qtype::BOOL_LIST => {
if let Some(boolean) = element.downcast_ref::<bool>() {
self.increment();
Ok(self.as_mut_vec::<G>().unwrap().push(*boolean as u8))
} else {
Err(Error::insert_wrong_element(false, qtype::BOOL_LIST, "bool"))
}
}
qtype::GUID_LIST => {
if let Some(guid) = element.downcast_ref::<U>() {
self.increment();
Ok(self.as_mut_vec::<U>().unwrap().push(*guid))
} else {
Err(Error::insert_wrong_element(
false,
qtype::GUID_LIST,
"[u8; 16]",
))
}
}
qtype::BYTE_LIST => {
if let Some(byte) = element.downcast_ref::<u8>() {
self.increment();
Ok(self.as_mut_vec::<G>().unwrap().push(*byte))
} else {
Err(Error::insert_wrong_element(false, qtype::BYTE_LIST, "u8"))
}
}
qtype::SHORT_LIST => {
if let Some(short) = element.downcast_ref::<i16>() {
self.increment();
Ok(self.as_mut_vec::<H>().unwrap().push(*short))
} else {
Err(Error::insert_wrong_element(false, qtype::SHORT_LIST, "i16"))
}
}
qtype::INT_LIST => {
if let Some(int) = element.downcast_ref::<i32>() {
self.increment();
Ok(self.as_mut_vec::<I>().unwrap().push(*int))
} else {
Err(Error::insert_wrong_element(
false,
qtype::INT_LIST,
"[u8; 16]",
))
}
}
qtype::LONG_LIST => {
if let Some(long) = element.downcast_ref::<i64>() {
self.increment();
Ok(self.as_mut_vec::<J>().unwrap().push(*long))
} else {
Err(Error::insert_wrong_element(false, qtype::LONG_LIST, "i64"))
}
}
qtype::REAL_LIST => {
if let Some(real) = element.downcast_ref::<f32>() {
self.increment();
Ok(self.as_mut_vec::<E>().unwrap().push(*real))
} else {
Err(Error::insert_wrong_element(false, qtype::FLOAT_LIST, "f32"))
}
}
qtype::FLOAT_LIST => {
if let Some(float) = element.downcast_ref::<f64>() {
self.increment();
Ok(self.as_mut_vec::<F>().unwrap().push(*float))
} else {
Err(Error::insert_wrong_element(false, qtype::FLOAT_LIST, "f64"))
}
}
qtype::STRING => {
if let Some(ch) = element.downcast_ref::<char>() {
Ok(self.as_mut_string().unwrap().push(*ch))
} else {
Err(Error::insert_wrong_element(false, qtype::STRING, "char"))
}
}
qtype::SYMBOL_LIST => {
if let Some(symbol) = element.downcast_ref::<String>() {
self.increment();
Ok(self.as_mut_vec::<S>().unwrap().push(symbol.clone()))
} else {
Err(Error::insert_wrong_element(
false,
qtype::SYMBOL_LIST,
"String",
))
}
}
qtype::TIMESTAMP_LIST => {
if let Some(timestamp) = element.downcast_ref::<DateTime<Utc>>() {
self.increment();
Ok(self
.as_mut_vec::<J>()
.unwrap()
.push(datetime_to_q_timestamp(*timestamp)))
} else {
Err(Error::insert_wrong_element(
false,
qtype::TIMESTAMP_LIST,
"DateTime<Utc>",
))
}
}
qtype::MONTH_LIST => {
if let Some(month) = element.downcast_ref::<NaiveDate>() {
self.increment();
Ok(self
.as_mut_vec::<I>()
.unwrap()
.push(date_to_q_month(*month)))
} else {
Err(Error::insert_wrong_element(
false,
qtype::MONTH_LIST,
"Date<Utc>",
))
}
}
qtype::DATE_LIST => {
if let Some(date) = element.downcast_ref::<NaiveDate>() {
self.increment();
Ok(self.as_mut_vec::<I>().unwrap().push(date_to_q_date(*date)))
} else {
Err(Error::insert_wrong_element(
false,
qtype::DATE_LIST,
"Date<Utc>",
))
}
}
qtype::DATETIME_LIST => {
if let Some(datetime) = element.downcast_ref::<DateTime<Utc>>() {
self.increment();
Ok(self
.as_mut_vec::<F>()
.unwrap()
.push(datetime_to_q_datetime(*datetime)))
} else {
Err(Error::insert_wrong_element(
false,
qtype::DATETIME_LIST,
"DateTime<Utc>",
))
}
}
qtype::TIMESPAN_LIST => {
if let Some(timespan) = element.downcast_ref::<Duration>() {
self.increment();
Ok(self
.as_mut_vec::<J>()
.unwrap()
.push(timespan.num_nanoseconds().expect("duration overflow")))
} else {
Err(Error::insert_wrong_element(
false,
qtype::TIMESPAN_LIST,
"Duration",
))
}
}
qtype::MINUTE_LIST => {
if let Some(minute) = element.downcast_ref::<Duration>() {
self.increment();
Ok(self
.as_mut_vec::<I>()
.unwrap()
.push(minute.num_minutes() as i32))
} else {
Err(Error::insert_wrong_element(
false,
qtype::MINUTE_LIST,
"Duration",
))
}
}
qtype::SECOND_LIST => {
if let Some(second) = element.downcast_ref::<Duration>() {
self.increment();
Ok(self
.as_mut_vec::<I>()
.unwrap()
.push(second.num_seconds() as i32))
} else {
Err(Error::insert_wrong_element(
false,
qtype::SECOND_LIST,
"Duration",
))
}
}
qtype::TIME_LIST => {
if let Some(time) = element.downcast_ref::<Duration>() {
self.increment();
Ok(self
.as_mut_vec::<I>()
.unwrap()
.push(time.num_milliseconds() as i32))
} else {
Err(Error::insert_wrong_element(
false,
qtype::TIME_LIST,
"Duration",
))
}
}
qtype::COMPOUND_LIST => {
if let Some(k) = element.downcast_ref::<K>() {
self.increment();
Ok(self.as_mut_vec::<K>().unwrap().push(k.clone()))
} else {
Err(Error::insert_wrong_element(
false,
qtype::COMPOUND_LIST,
"K",
))
}
}
_ => Err(Error::invalid_operation("push", self.0.qtype, None)),
}
}
pub fn insert(&mut self, index: usize, element: &dyn Any) -> Result<()> {
if index > self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::BOOL_LIST => {
if let Some(boolean) = element.downcast_ref::<bool>() {
self.increment();
Ok(self
.as_mut_vec::<G>()
.unwrap()
.insert(index, *boolean as u8))
} else {
Err(Error::insert_wrong_element(true, qtype::BOOL_LIST, "bool"))
}
}
qtype::GUID_LIST => {
if let Some(guid) = element.downcast_ref::<U>() {
self.increment();
Ok(self.as_mut_vec::<U>().unwrap().insert(index, *guid))
} else {
Err(Error::insert_wrong_element(
true,
qtype::GUID_LIST,
"[u8; 16]",
))
}
}
qtype::BYTE_LIST => {
if let Some(byte) = element.downcast_ref::<u8>() {
self.increment();
Ok(self.as_mut_vec::<G>().unwrap().insert(index, *byte))
} else {
Err(Error::insert_wrong_element(true, qtype::BYTE_LIST, "u8"))
}
}
qtype::SHORT_LIST => {
if let Some(short) = element.downcast_ref::<i16>() {
self.increment();
Ok(self.as_mut_vec::<H>().unwrap().insert(index, *short))
} else {
Err(Error::insert_wrong_element(false, qtype::SHORT_LIST, "i16"))
}
}
qtype::INT_LIST => {
if let Some(int) = element.downcast_ref::<i32>() {
self.increment();
Ok(self.as_mut_vec::<I>().unwrap().insert(index, *int))
} else {
Err(Error::insert_wrong_element(false, qtype::INT_LIST, "i32"))
}
}
qtype::LONG_LIST => {
if let Some(long) = element.downcast_ref::<i64>() {
self.increment();
Ok(self.as_mut_vec::<J>().unwrap().insert(index, *long))
} else {
Err(Error::insert_wrong_element(false, qtype::LONG_LIST, "i64"))
}
}
qtype::REAL_LIST => {
if let Some(real) = element.downcast_ref::<f32>() {
self.increment();
Ok(self.as_mut_vec::<E>().unwrap().insert(index, *real))
} else {
Err(Error::insert_wrong_element(false, qtype::REAL_LIST, "f32"))
}
}
qtype::FLOAT_LIST => {
if let Some(float) = element.downcast_ref::<f64>() {
self.increment();
Ok(self.as_mut_vec::<F>().unwrap().insert(index, *float))
} else {
Err(Error::insert_wrong_element(false, qtype::FLOAT_LIST, "f64"))
}
}
qtype::STRING => {
if let Some(ch) = element.downcast_ref::<char>() {
Ok(self.as_mut_string().unwrap().insert(index, *ch))
} else {
Err(Error::insert_wrong_element(false, qtype::STRING, "char"))
}
}
qtype::SYMBOL_LIST => {
if let Some(symbol) = element.downcast_ref::<String>() {
self.increment();
Ok(self
.as_mut_vec::<S>()
.unwrap()
.insert(index, symbol.clone()))
} else {
Err(Error::insert_wrong_element(
false,
qtype::SYMBOL_LIST,
"String",
))
}
}
qtype::TIMESTAMP_LIST => {
if let Some(timestamp) = element.downcast_ref::<DateTime<Utc>>() {
self.increment();
Ok(self
.as_mut_vec::<J>()
.unwrap()
.insert(index, datetime_to_q_timestamp(*timestamp)))
} else {
Err(Error::insert_wrong_element(
false,
qtype::TIMESTAMP_LIST,
"DateTime<Utc>",
))
}
}
qtype::MONTH_LIST => {
if let Some(month) = element.downcast_ref::<NaiveDate>() {
self.increment();
Ok(self
.as_mut_vec::<I>()
.unwrap()
.insert(index, date_to_q_month(*month)))
} else {
Err(Error::insert_wrong_element(
false,
qtype::MONTH_LIST,
"Date<Utc>",
))
}
}
qtype::DATE_LIST => {
if let Some(date) = element.downcast_ref::<NaiveDate>() {
self.increment();
Ok(self
.as_mut_vec::<I>()
.unwrap()
.insert(index, date_to_q_date(*date)))
} else {
Err(Error::insert_wrong_element(
false,
qtype::DATE_LIST,
"Date<Utc>",
))
}
}
qtype::DATETIME_LIST => {
if let Some(datetime) = element.downcast_ref::<DateTime<Utc>>() {
self.increment();
Ok(self
.as_mut_vec::<F>()
.unwrap()
.insert(index, datetime_to_q_datetime(*datetime)))
} else {
Err(Error::insert_wrong_element(
false,
qtype::DATETIME_LIST,
"DateTime<Utc>",
))
}
}
qtype::TIMESPAN_LIST => {
if let Some(timespan) = element.downcast_ref::<Duration>() {
self.increment();
Ok(self.as_mut_vec::<J>().unwrap().insert(
index,
timespan.num_nanoseconds().expect("duration overflow"),
))
} else {
Err(Error::insert_wrong_element(
false,
qtype::TIMESPAN_LIST,
"Duration",
))
}
}
qtype::MINUTE_LIST => {
if let Some(minute) = element.downcast_ref::<Duration>() {
self.increment();
Ok(self
.as_mut_vec::<I>()
.unwrap()
.insert(index, minute.num_minutes() as i32))
} else {
Err(Error::insert_wrong_element(
false,
qtype::MINUTE_LIST,
"Duration",
))
}
}
qtype::SECOND_LIST => {
if let Some(second) = element.downcast_ref::<Duration>() {
self.increment();
Ok(self
.as_mut_vec::<I>()
.unwrap()
.insert(index, second.num_seconds() as i32))
} else {
Err(Error::insert_wrong_element(
false,
qtype::SECOND_LIST,
"Duration",
))
}
}
qtype::TIME_LIST => {
if let Some(time) = element.downcast_ref::<Duration>() {
self.increment();
Ok(self
.as_mut_vec::<I>()
.unwrap()
.insert(index, time.num_milliseconds() as i32))
} else {
Err(Error::insert_wrong_element(
false,
qtype::TIME_LIST,
"Duration",
))
}
}
qtype::COMPOUND_LIST => {
if let Some(k) = element.downcast_ref::<K>() {
self.increment();
Ok(self.as_mut_vec::<K>().unwrap().insert(index, k.clone()))
} else {
Err(Error::insert_wrong_element(
false,
qtype::COMPOUND_LIST,
"K",
))
}
}
_ => Err(Error::invalid_operation("insert", self.0.qtype, None)),
}
}
}
pub fn pop_bool(&mut self) -> Result<bool> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::BOOL_LIST => {
self.decrement();
Ok(self.as_mut_vec::<G>().unwrap().pop().unwrap() != 0)
}
_ => Err(Error::invalid_operation(
"pop_bool",
self.0.qtype,
Some(qtype::BOOL_LIST),
)),
}
}
}
pub fn pop_guid(&mut self) -> Result<[u8; 16]> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::GUID_LIST => {
self.decrement();
Ok(self.as_mut_vec::<U>().unwrap().pop().unwrap())
}
_ => Err(Error::invalid_operation(
"pop_guid",
self.0.qtype,
Some(qtype::GUID_LIST),
)),
}
}
}
pub fn pop_byte(&mut self) -> Result<u8> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::BYTE_LIST => {
self.decrement();
Ok(self.as_mut_vec::<G>().unwrap().pop().unwrap())
}
_ => Err(Error::invalid_operation(
"pop_byte",
self.0.qtype,
Some(qtype::BYTE_LIST),
)),
}
}
}
pub fn pop_short(&mut self) -> Result<i16> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::SHORT_LIST => {
self.decrement();
Ok(self.as_mut_vec::<H>().unwrap().pop().unwrap())
}
_ => Err(Error::invalid_operation(
"pop_short",
self.0.qtype,
Some(qtype::SHORT_LIST),
)),
}
}
}
pub fn pop_int(&mut self) -> Result<i32> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::INT_LIST => {
self.decrement();
Ok(self.as_mut_vec::<I>().unwrap().pop().unwrap())
}
_ => Err(Error::invalid_operation(
"pop_int",
self.0.qtype,
Some(qtype::INT_LIST),
)),
}
}
}
pub fn pop_long(&mut self) -> Result<i64> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::LONG_LIST => {
self.decrement();
Ok(self.as_mut_vec::<J>().unwrap().pop().unwrap())
}
_ => Err(Error::invalid_operation(
"pop_long",
self.0.qtype,
Some(qtype::LONG_LIST),
)),
}
}
}
pub fn pop_real(&mut self) -> Result<f32> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::REAL_LIST => {
self.decrement();
Ok(self.as_mut_vec::<E>().unwrap().pop().unwrap())
}
_ => Err(Error::invalid_operation(
"pop_real",
self.0.qtype,
Some(qtype::REAL_LIST),
)),
}
}
}
pub fn pop_float(&mut self) -> Result<f64> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::FLOAT_LIST => {
self.decrement();
Ok(self.as_mut_vec::<F>().unwrap().pop().unwrap())
}
_ => Err(Error::invalid_operation(
"pop_float",
self.0.qtype,
Some(qtype::FLOAT_LIST),
)),
}
}
}
pub fn pop_char(&mut self) -> Result<char> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::STRING => Ok(self.as_mut_string().unwrap().pop().unwrap()),
_ => Err(Error::invalid_operation(
"pop_char",
self.0.qtype,
Some(qtype::STRING),
)),
}
}
}
pub fn pop_symbol(&mut self) -> Result<String> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::SYMBOL_LIST => {
self.decrement();
Ok(self.as_mut_vec::<S>().unwrap().pop().unwrap())
}
_ => Err(Error::invalid_operation(
"pop_symbol",
self.0.qtype,
Some(qtype::SYMBOL_LIST),
)),
}
}
}
pub fn pop_timestamp(&mut self) -> Result<DateTime<Utc>> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::TIMESTAMP_LIST => {
self.decrement();
Ok(q_timestamp_to_datetime(
self.as_mut_vec::<J>().unwrap().pop().unwrap(),
))
}
_ => Err(Error::invalid_operation(
"pop_timestamp",
self.0.qtype,
Some(qtype::TIMESTAMP_LIST),
)),
}
}
}
pub fn pop_month(&mut self) -> Result<NaiveDate> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::MONTH_LIST => {
self.decrement();
Ok(q_month_to_date(
self.as_mut_vec::<I>().unwrap().pop().unwrap(),
))
}
_ => Err(Error::invalid_operation(
"pop_month",
self.0.qtype,
Some(qtype::MONTH_LIST),
)),
}
}
}
pub fn pop_date(&mut self) -> Result<NaiveDate> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::DATE_LIST => {
self.decrement();
Ok(q_date_to_date(
self.as_mut_vec::<I>().unwrap().pop().unwrap(),
)?)
}
_ => Err(Error::invalid_operation(
"pop_date",
self.0.qtype,
Some(qtype::DATE_LIST),
)),
}
}
}
pub fn pop_datetime(&mut self) -> Result<DateTime<Utc>> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::DATETIME_LIST => {
self.decrement();
Ok(q_datetime_to_datetime(
self.as_mut_vec::<F>().unwrap().pop().unwrap(),
))
}
_ => Err(Error::invalid_operation(
"pop_datetime",
self.0.qtype,
Some(qtype::DATETIME_LIST),
)),
}
}
}
pub fn pop_timespan(&mut self) -> Result<Duration> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::TIMESPAN_LIST => {
self.decrement();
Ok(Duration::nanoseconds(
self.as_mut_vec::<J>().unwrap().pop().unwrap(),
))
}
_ => Err(Error::invalid_operation(
"pop_timespan",
self.0.qtype,
Some(qtype::TIMESPAN_LIST),
)),
}
}
}
pub fn pop_minute(&mut self) -> Result<Duration> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::MINUTE_LIST => {
self.decrement();
Ok(Duration::minutes(
self.as_mut_vec::<I>().unwrap().pop().unwrap() as i64,
))
}
_ => Err(Error::invalid_operation(
"pop_minute",
self.0.qtype,
Some(qtype::MINUTE_LIST),
)),
}
}
}
pub fn pop_second(&mut self) -> Result<Duration> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::SECOND_LIST => {
self.decrement();
Ok(Duration::seconds(
self.as_mut_vec::<I>().unwrap().pop().unwrap() as i64,
))
}
_ => Err(Error::invalid_operation(
"pop_second",
self.0.qtype,
Some(qtype::SECOND_LIST),
)),
}
}
}
pub fn pop_time(&mut self) -> Result<Duration> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::TIME_LIST => {
self.decrement();
Ok(Duration::milliseconds(
self.as_mut_vec::<I>().unwrap().pop().unwrap() as i64,
))
}
_ => Err(Error::invalid_operation(
"pop_time",
self.0.qtype,
Some(qtype::TIME_LIST),
)),
}
}
}
pub fn pop(&mut self) -> Result<K> {
if self.len() == 0 {
Err(Error::pop_from_empty_list())
} else {
match self.0.qtype {
qtype::BOOL_LIST => {
self.decrement();
Ok(K::new_bool(
self.as_mut_vec::<G>().unwrap().pop().unwrap() != 0,
))
}
qtype::GUID_LIST => {
self.decrement();
Ok(K::new_guid(self.as_mut_vec::<U>().unwrap().pop().unwrap()))
}
qtype::BYTE_LIST => {
self.decrement();
Ok(K::new_byte(self.as_mut_vec::<G>().unwrap().pop().unwrap()))
}
qtype::SHORT_LIST => {
self.decrement();
Ok(K::new_short(self.as_mut_vec::<H>().unwrap().pop().unwrap()))
}
qtype::INT_LIST => {
self.decrement();
Ok(K::new_int(self.as_mut_vec::<I>().unwrap().pop().unwrap()))
}
qtype::LONG_LIST => {
self.decrement();
Ok(K::new_long(self.as_mut_vec::<J>().unwrap().pop().unwrap()))
}
qtype::REAL_LIST => {
self.decrement();
Ok(K::new_real(self.as_mut_vec::<E>().unwrap().pop().unwrap()))
}
qtype::FLOAT_LIST => {
self.decrement();
Ok(K::new_float(self.as_mut_vec::<F>().unwrap().pop().unwrap()))
}
qtype::STRING => Ok(K::new_char(self.as_mut_string().unwrap().pop().unwrap())),
qtype::SYMBOL_LIST => {
self.decrement();
Ok(K::new_symbol(
self.as_mut_vec::<S>().unwrap().pop().unwrap(),
))
}
qtype::TIMESTAMP_LIST => {
self.decrement();
Ok(K::new(
qtype::TIMESTAMP_ATOM,
qattribute::NONE,
k0_inner::long(self.as_mut_vec::<J>().unwrap().pop().unwrap()),
))
}
qtype::MONTH_LIST => {
self.decrement();
Ok(K::new(
qtype::MONTH_ATOM,
qattribute::NONE,
k0_inner::int(self.as_mut_vec::<I>().unwrap().pop().unwrap()),
))
}
qtype::DATE_LIST => {
self.decrement();
Ok(K::new(
qtype::DATE_ATOM,
qattribute::NONE,
k0_inner::int(self.as_mut_vec::<I>().unwrap().pop().unwrap()),
))
}
qtype::DATETIME_LIST => {
self.decrement();
Ok(K::new(
qtype::DATETIME_ATOM,
qattribute::NONE,
k0_inner::float(self.as_mut_vec::<F>().unwrap().pop().unwrap()),
))
}
qtype::TIMESPAN_LIST => {
self.decrement();
Ok(K::new(
qtype::TIMESPAN_ATOM,
qattribute::NONE,
k0_inner::long(self.as_mut_vec::<J>().unwrap().pop().unwrap()),
))
}
qtype::MINUTE_LIST => {
self.decrement();
Ok(K::new(
qtype::MINUTE_ATOM,
qattribute::NONE,
k0_inner::int(self.as_mut_vec::<I>().unwrap().pop().unwrap()),
))
}
qtype::SECOND_LIST => {
self.decrement();
Ok(K::new(
qtype::SECOND_ATOM,
qattribute::NONE,
k0_inner::int(self.as_mut_vec::<I>().unwrap().pop().unwrap()),
))
}
qtype::TIME_LIST => {
self.decrement();
Ok(K::new(
qtype::TIME_ATOM,
qattribute::NONE,
k0_inner::int(self.as_mut_vec::<I>().unwrap().pop().unwrap()),
))
}
qtype::COMPOUND_LIST => {
self.decrement();
Ok(self.as_mut_vec::<K>().unwrap().pop().unwrap())
}
_ => Err(Error::invalid_operation("pop", self.0.qtype, None)),
}
}
}
pub fn remove_bool(&mut self, index: usize) -> Result<bool> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::BOOL_LIST => Ok(self.as_mut_vec::<G>().unwrap().remove(index) != 0),
_ => Err(Error::invalid_operation(
"remove_bool",
self.0.qtype,
Some(qtype::BOOL_LIST),
)),
}
}
}
pub fn remove_guid(&mut self, index: usize) -> Result<[u8; 16]> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::GUID_LIST => Ok(self.as_mut_vec::<U>().unwrap().remove(index)),
_ => Err(Error::invalid_operation(
"remove_guid",
self.0.qtype,
Some(qtype::GUID_LIST),
)),
}
}
}
pub fn remove_byte(&mut self, index: usize) -> Result<u8> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::BYTE_LIST => Ok(self.as_mut_vec::<G>().unwrap().remove(index)),
_ => Err(Error::invalid_operation(
"remove_byte",
self.0.qtype,
Some(qtype::BYTE_LIST),
)),
}
}
}
pub fn remove_short(&mut self, index: usize) -> Result<i16> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::SHORT_LIST => Ok(self.as_mut_vec::<H>().unwrap().remove(index)),
_ => Err(Error::invalid_operation(
"remove_short",
self.0.qtype,
Some(qtype::SHORT_LIST),
)),
}
}
}
pub fn remove_int(&mut self, index: usize) -> Result<i32> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::INT_LIST => Ok(self.as_mut_vec::<I>().unwrap().remove(index)),
_ => Err(Error::invalid_operation(
"remove_int",
self.0.qtype,
Some(qtype::INT_LIST),
)),
}
}
}
pub fn remove_long(&mut self, index: usize) -> Result<i64> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::LONG_LIST => Ok(self.as_mut_vec::<J>().unwrap().remove(index)),
_ => Err(Error::invalid_operation(
"remove_long",
self.0.qtype,
Some(qtype::LONG_LIST),
)),
}
}
}
pub fn remove_real(&mut self, index: usize) -> Result<f32> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::REAL_LIST => Ok(self.as_mut_vec::<E>().unwrap().remove(index)),
_ => Err(Error::invalid_operation(
"remove_real",
self.0.qtype,
Some(qtype::REAL_LIST),
)),
}
}
}
pub fn remove_float(&mut self, index: usize) -> Result<f64> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::FLOAT_LIST => Ok(self.as_mut_vec::<F>().unwrap().remove(index)),
_ => Err(Error::invalid_operation(
"remove_float",
self.0.qtype,
Some(qtype::FLOAT_LIST),
)),
}
}
}
pub fn remove_char(&mut self, index: usize) -> Result<char> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::STRING => Ok(self.as_mut_string().unwrap().remove(index)),
_ => Err(Error::invalid_operation(
"remove_char",
self.0.qtype,
Some(qtype::STRING),
)),
}
}
}
pub fn remove_symbol(&mut self, index: usize) -> Result<String> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::SYMBOL_LIST => Ok(self.as_mut_vec::<S>().unwrap().remove(index)),
_ => Err(Error::invalid_operation(
"remove_symbol",
self.0.qtype,
Some(qtype::SYMBOL_LIST),
)),
}
}
}
pub fn remove_timestamp(&mut self, index: usize) -> Result<DateTime<Utc>> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::TIMESTAMP_LIST => Ok(q_timestamp_to_datetime(
self.as_mut_vec::<J>().unwrap().remove(index),
)),
_ => Err(Error::invalid_operation(
"remove_timestamp",
self.0.qtype,
Some(qtype::TIMESTAMP_LIST),
)),
}
}
}
pub fn remove_month(&mut self, index: usize) -> Result<NaiveDate> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::MONTH_LIST => Ok(q_month_to_date(
self.as_mut_vec::<I>().unwrap().remove(index),
)),
_ => Err(Error::invalid_operation(
"remove_month",
self.0.qtype,
Some(qtype::MONTH_LIST),
)),
}
}
}
pub fn remove_date(&mut self, index: usize) -> Result<NaiveDate> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::DATE_LIST => Ok(q_date_to_date(
self.as_mut_vec::<I>().unwrap().remove(index),
)?),
_ => Err(Error::invalid_operation(
"remove_date",
self.0.qtype,
Some(qtype::DATE_LIST),
)),
}
}
}
pub fn remove_datetime(&mut self, index: usize) -> Result<DateTime<Utc>> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::DATETIME_LIST => Ok(q_datetime_to_datetime(
self.as_mut_vec::<F>().unwrap().remove(index),
)),
_ => Err(Error::invalid_operation(
"remove_datetime",
self.0.qtype,
Some(qtype::DATETIME_LIST),
)),
}
}
}
pub fn remove_timespan(&mut self, index: usize) -> Result<Duration> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::TIMESPAN_LIST => Ok(Duration::nanoseconds(
self.as_mut_vec::<J>().unwrap().remove(index),
)),
_ => Err(Error::invalid_operation(
"remove_timespan",
self.0.qtype,
Some(qtype::TIMESPAN_LIST),
)),
}
}
}
pub fn remove_minute(&mut self, index: usize) -> Result<Duration> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::MINUTE_LIST => Ok(Duration::minutes(
self.as_mut_vec::<I>().unwrap().remove(index) as i64,
)),
_ => Err(Error::invalid_operation(
"remove_minute",
self.0.qtype,
Some(qtype::MINUTE_LIST),
)),
}
}
}
pub fn remove_second(&mut self, index: usize) -> Result<Duration> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::SECOND_LIST => Ok(Duration::seconds(
self.as_mut_vec::<I>().unwrap().remove(index) as i64,
)),
_ => Err(Error::invalid_operation(
"remove_second",
self.0.qtype,
Some(qtype::SECOND_LIST),
)),
}
}
}
pub fn remove_time(&mut self, index: usize) -> Result<Duration> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::TIME_LIST => Ok(Duration::milliseconds(
self.as_mut_vec::<I>().unwrap().remove(index) as i64,
)),
_ => Err(Error::invalid_operation(
"remove_time",
self.0.qtype,
Some(qtype::TIME_LIST),
)),
}
}
}
pub fn remove(&mut self, index: usize) -> Result<K> {
if index >= self.len() {
Err(Error::index_out_of_bounds(self.len(), index))
} else {
match self.0.qtype {
qtype::BOOL_LIST => Ok(K::new_bool(
self.as_mut_vec::<G>().unwrap().remove(index) != 0,
)),
qtype::GUID_LIST => Ok(K::new_guid(self.as_mut_vec::<U>().unwrap().remove(index))),
qtype::BYTE_LIST => Ok(K::new_byte(self.as_mut_vec::<G>().unwrap().remove(index))),
qtype::SHORT_LIST => {
Ok(K::new_short(self.as_mut_vec::<H>().unwrap().remove(index)))
}
qtype::INT_LIST => Ok(K::new_int(self.as_mut_vec::<I>().unwrap().remove(index))),
qtype::LONG_LIST => Ok(K::new_long(self.as_mut_vec::<J>().unwrap().remove(index))),
qtype::REAL_LIST => Ok(K::new_real(self.as_mut_vec::<E>().unwrap().remove(index))),
qtype::FLOAT_LIST => {
Ok(K::new_float(self.as_mut_vec::<F>().unwrap().remove(index)))
}
qtype::STRING => Ok(K::new_char(self.as_mut_string().unwrap().remove(index))),
qtype::SYMBOL_LIST => {
Ok(K::new_symbol(self.as_mut_vec::<S>().unwrap().remove(index)))
}
qtype::TIMESTAMP_LIST => Ok(K::new_timestamp(q_timestamp_to_datetime(
self.as_mut_vec::<J>().unwrap().remove(index),
))),
qtype::MONTH_LIST => Ok(K::new_month(q_month_to_date(
self.as_mut_vec::<I>().unwrap().remove(index),
))),
qtype::DATE_LIST => Ok(K::new_date(q_date_to_date(
self.as_mut_vec::<I>().unwrap().remove(index),
)?)),
qtype::DATETIME_LIST => Ok(K::new_datetime(q_datetime_to_datetime(
self.as_mut_vec::<F>().unwrap().remove(index),
))),
qtype::TIMESPAN_LIST => Ok(K::new_timespan(Duration::nanoseconds(
self.as_mut_vec::<J>().unwrap().remove(index),
))),
qtype::MINUTE_LIST => Ok(K::new_minute(Duration::minutes(
self.as_mut_vec::<I>().unwrap().remove(index) as i64,
))),
qtype::SECOND_LIST => Ok(K::new_second(Duration::seconds(
self.as_mut_vec::<I>().unwrap().remove(index) as i64,
))),
qtype::TIME_LIST => Ok(K::new_time(Duration::milliseconds(
self.as_mut_vec::<I>().unwrap().remove(index) as i64,
))),
qtype::COMPOUND_LIST => Ok(self.as_mut_vec::<K>().unwrap().remove(index)),
_ => Err(Error::invalid_operation("remove", self.0.qtype, None)),
}
}
}
pub fn push_pair(&mut self, key: &dyn Any, value: &dyn Any) -> Result<()> {
match self.0.qtype {
qtype::DICTIONARY => {
let dictionary = self.as_mut_vec::<K>().unwrap();
match dictionary[0].push(key) {
Ok(_) => match dictionary[1].push(value) {
Ok(_) => Ok(()),
Err(error) => {
dictionary[0].pop().unwrap();
Err(error)
}
},
Err(error) => Err(error),
}
}
_ => Err(Error::invalid_operation(
"push_pair",
self.0.qtype,
Some(qtype::DICTIONARY),
)),
}
}
pub fn pop_pair(&mut self) -> Result<(K, K)> {
match self.0.qtype {
qtype::DICTIONARY => {
let dictionary = self.as_mut_vec::<K>().unwrap();
if let (Ok(key), Ok(value)) = (dictionary[0].pop(), dictionary[1].pop()) {
Ok((key, value))
} else {
Err(Error::pop_from_empty_list())
}
}
_ => Err(Error::invalid_operation(
"pop_pair",
self.0.qtype,
Some(qtype::DICTIONARY),
)),
}
}
pub fn len(&self) -> usize {
match self.0.qtype {
_t @ qtype::COMPOUND_LIST..=qtype::TIME_LIST => {
match &self.0.value {
k0_inner::symbol(string) => string.len(),
k0_inner::list(list) => list.n as usize,
_ => unreachable!(),
}
}
qtype::TABLE => {
match &self.0.value {
k0_inner::table(dictionary) => {
match &dictionary.as_vec::<K>().unwrap()[1].as_vec::<K>().unwrap()[0]
.0
.value
{
k0_inner::list(column) => {
column.n as usize
}
k0_inner::symbol(column) => {
column.len()
}
_ => unreachable!(),
}
}
_ => unreachable!(),
}
}
qtype::DICTIONARY | qtype::SORTED_DICTIONARY => {
match &self.as_vec::<K>().unwrap()[0].0.value {
k0_inner::list(list) => list.n as usize,
k0_inner::table(_) => self.as_vec::<K>().unwrap()[0].len(),
_ => unreachable!(),
}
}
_ => 1,
}
}
pub fn flip(self) -> Result<Self> {
match self.0.qtype {
qtype::DICTIONARY | qtype::SORTED_DICTIONARY => {
let keys_values = (&self).as_vec::<K>().unwrap();
if keys_values[0].0.qtype == qtype::SYMBOL_LIST
&& keys_values[1].0.qtype == qtype::COMPOUND_LIST
{
Ok(K::new(
qtype::TABLE,
qattribute::NONE,
k0_inner::table(self),
))
} else {
Err(Error::object(self))
}
}
_ => Err(Error::object(self)),
}
}
pub fn enkey(self, mut n: usize) -> Result<Self> {
match self.0.value {
k0_inner::table(mut dictionary) => {
let headers_columns = dictionary.as_mut_vec::<K>().unwrap();
if headers_columns[0].len() <= n {
n = headers_columns[0].len() - 1;
}
let value_heders = K::new_symbol_list(
headers_columns[0].as_mut_vec::<S>().unwrap().split_off(n),
qattribute::NONE,
);
let value_columns = K::new_compound_list(
headers_columns[1].as_mut_vec::<K>().unwrap().split_off(n),
);
let value_table = K::new_dictionary(value_heders, value_columns)
.unwrap()
.flip()
.unwrap();
Ok(K::new_dictionary(dictionary.flip().unwrap(), value_table)
.expect("failed to build keyed table"))
}
_ => Err(Error::object(self)),
}
}
pub fn unkey(mut self) -> Result<Self> {
match self.0.qtype {
qtype::DICTIONARY => {
let value_table = self.as_mut_vec::<K>().unwrap().pop().unwrap();
let key_table = self.as_mut_vec::<K>().unwrap().pop().unwrap();
match (key_table.0.value, value_table.0.value) {
(
k0_inner::table(mut key_dictionary),
k0_inner::table(mut value_dictionary),
) => {
key_dictionary
.as_mut_vec::<K>()
.unwrap()
.into_iter()
.zip(value_dictionary.as_mut_vec::<K>().unwrap().into_iter())
.enumerate()
.for_each(|(i, (key, value))| {
if i == 0 {
key.as_mut_vec::<S>()
.unwrap()
.append(value.as_mut_vec::<S>().unwrap());
} else {
key.as_mut_vec::<K>()
.unwrap()
.append(value.as_mut_vec::<K>().unwrap());
}
});
key_dictionary.flip()
}
_ => unreachable!(),
}
}
_ => Err(Error::object(self)),
}
}
}
fn datetime_to_q_timestamp(timestamp: DateTime<Utc>) -> i64 {
if timestamp <= *qnull::TIMESTAMP {
qnull_base::J
} else if timestamp == *qninf::TIMESTAMP {
qninf_base::J
} else if timestamp >= *qinf::TIMESTAMP {
qinf_base::J
} else {
timestamp
.timestamp_nanos_opt()
.unwrap_or(i64::MIN)
.saturating_sub(KDB_TIMESTAMP_OFFSET)
}
}
fn date_to_q_month(month: NaiveDate) -> i32 {
if month == qnull::MONTH {
qnull_base::I
} else if month == *qninf::MONTH {
qninf_base::I
} else if month >= *qinf::MONTH {
qinf_base::I
} else {
let months = (month.year() - 1970) * 12 + month.month0() as i32;
months.saturating_sub(KDB_MONTH_OFFSET)
}
}
fn date_to_q_date(date: NaiveDate) -> i32 {
if date == qnull::DATE {
qnull_base::I
} else if date == *qninf::DATE {
qninf_base::I
} else if date == qinf::DATE {
qinf_base::I
} else {
let days =
NaiveDate::signed_duration_since(date, NaiveDate::from_ymd_opt(1970, 1, 1).unwrap())
.num_days() as i32;
days.saturating_sub(KDB_DAY_OFFSET)
}
}
fn datetime_to_q_datetime(datetime: DateTime<Utc>) -> f64 {
if datetime == qnull::DATETIME {
qnull_base::F
} else if datetime <= *qninf::DATETIME {
qninf_base::F
} else if datetime >= *qinf::DATETIME {
qinf_base::F
} else {
let millis = datetime.timestamp_millis() as f64 / ONE_DAY_MILLIS as f64;
millis - KDB_DAY_OFFSET as f64
}
}
pub fn q_timestamp_to_datetime(nanos: i64) -> DateTime<Utc> {
Utc.timestamp_nanos(nanos) + Duration::nanoseconds(KDB_TIMESTAMP_OFFSET)
}
pub fn q_month_to_date(months: i32) -> NaiveDate {
if months == qnull_base::I {
qnull::MONTH
} else if months <= -3171072 {
*qninf::MONTH
} else if months >= 3121728 {
*qinf::MONTH
} else {
NaiveDate::from_ymd_opt(2000 + months / 12, 1 + (months % 12) as u32, 1).unwrap()
}
}
pub fn q_date_to_date(days: i32) -> Result<NaiveDate> {
if days == qnull_base::I {
Ok(qnull::DATE)
} else if days <= -96476615 {
Ok(*qninf::DATE)
} else if days >= 95015644 {
Ok(qinf::DATE)
} else {
Ok((NaiveDate::from_ymd_opt(2000, 1, 1)
.ok_or_else(|| Error::InvalidDateTime)?
.and_hms_opt(0, 0, 0)
.ok_or_else(|| Error::InvalidDateTime)?
.and_local_timezone(Utc)
.unwrap()
+ Duration::days(days as i64))
.date_naive())
}
}
pub fn q_datetime_to_datetime(days: f64) -> DateTime<Utc> {
if days.is_nan() {
qnull::DATETIME
} else if days <= -96476615 as f64 {
*qninf::DATETIME
} else if days >= 95015644 as f64 {
*qinf::DATETIME
} else {
Utc.timestamp_millis_opt((ONE_DAY_MILLIS as f64 * (days + KDB_DAY_OFFSET as f64)) as i64)
.unwrap()
}
}
pub fn q_timespan_to_duration(nanos: i64) -> Duration {
Duration::nanoseconds(nanos)
}
pub fn q_minute_to_duration(minutes: i32) -> Duration {
Duration::minutes(minutes as i64)
}
pub fn q_second_to_duration(seconds: i32) -> Duration {
Duration::seconds(seconds as i64)
}
pub fn q_time_to_duration(millis: i32) -> Duration {
Duration::milliseconds(millis as i64)
}
mod connection;
mod deserialize;
mod format;
mod serialize;
pub use connection::*;