#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
use std::fmt;
use std::any::Any;
use chrono::prelude::*;
use chrono::Duration;
use super::{qtype, qattribute, qnull_base, qinf_base, qninf_base};
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(|| Utc.ymd(1707, 9, 22).and_hms_nano(0, 12, 43, 145224192));
pub const MONTH: Date<Utc>=chrono::MIN_DATE;
pub const DATE: Date<Utc>=chrono::MIN_DATE;
pub const DATETIME: DateTime<Utc>=chrono::MIN_DATETIME;
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(|| Utc.ymd(2292, 4, 10).and_hms_nano(23, 47, 16, 854775807));
pub const MONTH: Lazy<Date<Utc>>=Lazy::new(|| chrono::MAX_DATE - Duration::days(30));
pub const DATE: Date<Utc>=chrono::MAX_DATE;
pub const DATETIME: Lazy<DateTime<Utc>>=Lazy::new(|| chrono::MAX_DATETIME - 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(|| Utc.ymd(1707, 9, 22).and_hms_nano(0, 12, 43, 145224193));
pub const MONTH: Lazy<Date<Utc>>=Lazy::new(|| chrono::MIN_DATE + Duration::days(31));
pub const DATE: Lazy<Date<Utc>>=Lazy::new(|| chrono::MIN_DATE + Duration::days(1));
pub const DATETIME: Lazy<DateTime<Utc>>=Lazy::new(|| chrono::MIN_DATETIME + 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: Date<Utc>) -> Self{
K::new(qtype::MONTH_ATOM, qattribute::NONE, k0_inner::int(date_to_q_month(month)))
}
pub fn new_date(date: Date<Utc>) -> 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<Date<Utc>>, 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<Date<Utc>>, 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, &'static str>{
if keys.len() != values.len(){
Err("lengths of keys and values do not match")
}
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, &'static str>{
match self.0.qtype{
qtype::BOOL_ATOM => {
match self.0.value{
k0_inner::byte(boolean) => Ok(boolean!=0),
_ => unreachable!()
}
},
_ => Err("not a bool")
}
}
pub fn get_guid(&self) -> Result<[u8; 16], &'static str>{
match self.0.qtype{
qtype::GUID_ATOM => {
match self.0.value{
k0_inner::guid(guid) => Ok(guid),
_ => unreachable!()
}
},
_ => Err("not a GUID")
}
}
pub fn get_byte(&self) -> Result<u8, &'static str>{
match self.0.qtype{
qtype::BOOL_ATOM | qtype::BYTE_ATOM | qtype::CHAR => {
match self.0.value{
k0_inner::byte(byte) => Ok(byte),
_ => unreachable!()
}
},
_ => Err("not a byte compatible")
}
}
pub fn get_short(&self) -> Result<i16, &'static str>{
match self.0.qtype{
qtype::SHORT_ATOM => {
match self.0.value{
k0_inner::short(short) => Ok(short),
_ => unreachable!()
}
},
_ => Err("not a short")
}
}
pub fn get_int(&self) -> Result<i32, &'static str>{
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("not an int compatible")
}
}
pub fn get_long(&self) -> Result<i64, &'static str>{
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("not a long compatible")
}
}
pub fn get_real(&self) -> Result<f32, &'static str>{
match self.0.qtype{
qtype::REAL_ATOM => {
match self.0.value{
k0_inner::real(real) => Ok(real),
_ => unreachable!()
}
},
_ => Err("not a real")
}
}
pub fn get_float(&self) -> Result<f64, &'static str>{
match self.0.qtype{
qtype::FLOAT_ATOM | qtype::DATETIME_ATOM => {
match self.0.value{
k0_inner::float(float) => Ok(float),
_ => unreachable!()
}
},
_ => Err("not a float compatible")
}
}
pub fn get_char(&self) -> Result<char, &'static str>{
match self.0.qtype{
qtype::CHAR => {
match self.0.value{
k0_inner::byte(ch) => Ok(ch as char),
_ => unreachable!()
}
},
_ => Err("not a char")
}
}
pub fn get_symbol(&self) -> Result<&str, &'static str>{
match self.0.qtype{
qtype::SYMBOL_ATOM => {
match &self.0.value{
k0_inner::symbol(symbol) => Ok(symbol),
_ => unreachable!()
}
},
_ => Err("neither a symbol nor an error")
}
}
pub fn get_timestamp(&self) -> Result<DateTime<Utc>, &'static str>{
match self.0.qtype{
qtype::TIMESTAMP_ATOM => {
match self.0.value{
k0_inner::long(nanos) => Ok(q_timestamp_to_datetime(nanos)),
_ => unreachable!()
}
},
_ => Err("not a timestamp")
}
}
pub fn get_month(&self) -> Result<Date<Utc>, &'static str>{
match self.0.qtype{
qtype::MONTH_ATOM => {
match self.0.value{
k0_inner::int(months) => Ok(q_month_to_date(months)),
_ => unreachable!()
}
},
_ => Err("not a month")
}
}
pub fn get_date(&self) -> Result<Date<Utc>, &'static str>{
match self.0.qtype{
qtype::DATE_ATOM => {
match self.0.value{
k0_inner::int(days) => Ok(q_date_to_date(days)),
_ => unreachable!()
}
},
_ => Err("not a date")
}
}
pub fn get_datetime(&self) -> Result<DateTime<Utc>, &'static str>{
match self.0.qtype{
qtype::DATETIME_ATOM => {
match self.0.value{
k0_inner::float(days) => Ok(q_datetime_to_datetime(days)),
_ => unreachable!()
}
},
_ => Err("not a datetime")
}
}
pub fn get_timespan(&self) -> Result<Duration, &'static str>{
match self.0.qtype{
qtype::TIMESPAN_ATOM => {
match self.0.value{
k0_inner::long(nanos) => Ok(q_timespan_to_duration(nanos)),
_ => unreachable!()
}
},
_ => Err("not a timespan")
}
}
pub fn get_minute(&self) -> Result<Duration, &'static str>{
match self.0.qtype{
qtype::MINUTE_ATOM => {
match self.0.value{
k0_inner::int(minutes) => Ok(q_minute_to_duration(minutes)),
_ => unreachable!()
}
},
_ => Err("not a minute")
}
}
pub fn get_second(&self) -> Result<Duration, &'static str>{
match self.0.qtype{
qtype::SECOND_ATOM => {
match self.0.value{
k0_inner::int(seconds) => Ok(q_second_to_duration(seconds)),
_ => unreachable!()
}
},
_ => Err("not a second")
}
}
pub fn get_time(&self) -> Result<Duration, &'static str>{
match self.0.qtype{
qtype::TIME_ATOM => {
match self.0.value{
k0_inner::int(millis) => Ok(q_time_to_duration(millis)),
_ => unreachable!()
}
},
_ => Err("not a time")
}
}
pub fn get_dictionary(&self) -> Result<&K, &'static str>{
match self.0.qtype{
qtype::TABLE => {
match &self.0.value{
k0_inner::table(dictionary) => Ok(dictionary),
_ => unreachable!()
}
},
_ => Err("not a table")
}
}
pub fn get_mut_dictionary(&mut self) -> Result<&mut K, &'static str>{
match self.0.qtype{
qtype::TABLE => {
match &mut self.0.value{
k0_inner::table(dictionary) => Ok(dictionary),
_ => unreachable!()
}
},
_ => Err("not a table")
}
}
pub fn get_error_string(&self) -> Result<&str, &'static str>{
match self.0.qtype{
qtype::ERROR => {
match &self.0.value{
k0_inner::symbol(error) => Ok(error),
_ => unreachable!()
}
},
_ => Err("not an error")
}
}
pub fn as_string(&self) -> Result<&str, &'static str>{
match self.0.qtype{
qtype::STRING => {
match &self.0.value{
k0_inner::symbol(string) => Ok(string),
_ => unreachable!()
}
},
_ => Err("not a string")
}
}
pub fn as_mut_string(&mut self) -> Result<&mut String, &'static str>{
match self.0.qtype{
qtype::STRING => {
match &mut self.0.value{
k0_inner::symbol(string) => Ok(string),
_ => unreachable!()
}
},
_ => Err("not a string")
}
}
pub fn as_mut_vec<T>(&mut self) -> Result<&mut Vec<T>, &'static str> 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("failed to cast to specified type")
}
},
_ => unreachable!()
}
},
_ => Err("not a list type")
}
}
pub fn as_vec<T>(&self) -> Result<&Vec<T>, &'static str> 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("failed to cast to specified type")
}
},
_ => unreachable!()
}
},
_ => Err("not a list type")
}
}
pub fn get_column<T>(&self, column: T) -> Result<&K, &'static str> 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("no such column")
}
},
_ => Err("not a table")
}
}
pub fn get_mut_column<T>(&mut self, column: T) -> Result<&mut K, &'static str> 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("no such column")
}
},
_ => Err("not a table")
}
}
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<(), &'static str>{
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("failed to push an element. expected type: 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("failed to push an element. expected type: [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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: i32")
}
},
qtype::LONG_LIST => {
if let Some(long) = element.downcast_ref::<i64>(){
self.increment();
Ok(self.as_mut_vec::<J>().unwrap().push(*long))
}
else{
Err("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: f64")
}
},
qtype::STRING => {
if let Some(ch) = element.downcast_ref::<char>(){
Ok(self.as_mut_string().unwrap().push(*ch))
}
else{
Err("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: DateTime<Utc>")
}
},
qtype::MONTH_LIST => {
if let Some(month) = element.downcast_ref::<Date<Utc>>(){
self.increment();
Ok(self.as_mut_vec::<I>().unwrap().push(date_to_q_month(*month)))
}
else{
Err("failed to push an element. expected type: Date<Utc>")
}
},
qtype::DATE_LIST => {
if let Some(date) = element.downcast_ref::<Date<Utc>>(){
self.increment();
Ok(self.as_mut_vec::<I>().unwrap().push(date_to_q_date(*date)))
}
else{
Err("failed to push an element. expected type: Datte<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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: K")
}
},
_ => Err("this object cannot push an element")
}
}
pub fn insert(&mut self, index: usize, element: &dyn Any) -> Result<(), &'static str>{
if index > self.len(){
Err("index out of bounds")
}
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("failed to push an element. expected type: 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("failed to push an element. expected type: [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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: f64")
}
},
qtype::STRING => {
if let Some(ch) = element.downcast_ref::<char>(){
Ok(self.as_mut_string().unwrap().insert(index, *ch))
}
else{
Err("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: DateTime<Utc>")
}
},
qtype::MONTH_LIST => {
if let Some(month) = element.downcast_ref::<Date<Utc>>(){
self.increment();
Ok(self.as_mut_vec::<I>().unwrap().insert(index, date_to_q_month(*month)))
}
else{
Err("failed to push an element. expected type: Date<Utc>")
}
},
qtype::DATE_LIST => {
if let Some(date) = element.downcast_ref::<Date<Utc>>(){
self.increment();
Ok(self.as_mut_vec::<I>().unwrap().insert(index, date_to_q_date(*date)))
}
else{
Err("failed to push an element. expected type: Datte<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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: 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("failed to push an element. expected type: K")
}
},
_ => Err("this object cannot push an element")
}
}
}
pub fn pop_bool(&mut self) -> Result<bool, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::BOOL_LIST => {
self.decrement();
Ok(self.as_mut_vec::<G>().unwrap().pop().unwrap()!=0)
},
_ => Err("not a bool list")
}
}
}
pub fn pop_guid(&mut self) -> Result<[u8; 16], &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::GUID_LIST => {
self.decrement();
Ok(self.as_mut_vec::<U>().unwrap().pop().unwrap())
},
_ => Err("not a GUID list")
}
}
}
pub fn pop_byte(&mut self) -> Result<u8, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::BYTE_LIST => {
self.decrement();
Ok(self.as_mut_vec::<G>().unwrap().pop().unwrap())
},
_ => Err("not a byte list")
}
}
}
pub fn pop_short(&mut self) -> Result<i16, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::SHORT_LIST => {
self.decrement();
Ok(self.as_mut_vec::<H>().unwrap().pop().unwrap())
},
_ => Err("not a short list")
}
}
}
pub fn pop_int(&mut self) -> Result<i32, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::INT_LIST => {
self.decrement();
Ok(self.as_mut_vec::<I>().unwrap().pop().unwrap())
},
_ => Err("not an int list")
}
}
}
pub fn pop_long(&mut self) -> Result<i64, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::LONG_LIST => {
self.decrement();
Ok(self.as_mut_vec::<J>().unwrap().pop().unwrap())
},
_ => Err("not a long list")
}
}
}
pub fn pop_real(&mut self) -> Result<f32, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::REAL_LIST => {
self.decrement();
Ok(self.as_mut_vec::<E>().unwrap().pop().unwrap())
},
_ => Err("not a real list")
}
}
}
pub fn pop_float(&mut self) -> Result<f64, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::FLOAT_LIST => {
self.decrement();
Ok(self.as_mut_vec::<F>().unwrap().pop().unwrap())
},
_ => Err("not a float list")
}
}
}
pub fn pop_char(&mut self) -> Result<char, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::STRING => Ok(self.as_mut_string().unwrap().pop().unwrap()),
_ => Err("not a string")
}
}
}
pub fn pop_symbol(&mut self) -> Result<String, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::SYMBOL_LIST => {
self.decrement();
Ok(self.as_mut_vec::<S>().unwrap().pop().unwrap())
},
_ => Err("not a symbol list")
}
}
}
pub fn pop_timestamp(&mut self) -> Result<DateTime<Utc>, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::TIMESTAMP_LIST => {
self.decrement();
Ok(q_timestamp_to_datetime(self.as_mut_vec::<J>().unwrap().pop().unwrap()))
},
_ => Err("not a timestamp list")
}
}
}
pub fn pop_month(&mut self) -> Result<Date<Utc>, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::MONTH_LIST => {
self.decrement();
Ok(q_month_to_date(self.as_mut_vec::<I>().unwrap().pop().unwrap()))
},
_ => Err("not a month list")
}
}
}
pub fn pop_date(&mut self) -> Result<Date<Utc>, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::DATE_LIST => {
self.decrement();
Ok(q_date_to_date(self.as_mut_vec::<I>().unwrap().pop().unwrap()))
},
_ => Err("not a date list")
}
}
}
pub fn pop_datetime(&mut self) -> Result<DateTime<Utc>, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::DATETIME_LIST => {
self.decrement();
Ok(q_datetime_to_datetime(self.as_mut_vec::<F>().unwrap().pop().unwrap()))
},
_ => Err("not a datetime list")
}
}
}
pub fn pop_timespan(&mut self) -> Result<Duration, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::TIMESPAN_LIST => {
self.decrement();
Ok(Duration::nanoseconds(self.as_mut_vec::<J>().unwrap().pop().unwrap()))
},
_ => Err("not a timespan list")
}
}
}
pub fn pop_minute(&mut self) -> Result<Duration, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::MINUTE_LIST => {
self.decrement();
Ok(Duration::minutes(self.as_mut_vec::<I>().unwrap().pop().unwrap() as i64))
},
_ => Err("not a minute list")
}
}
}
pub fn pop_second(&mut self) -> Result<Duration, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::SECOND_LIST => {
self.decrement();
Ok(Duration::seconds(self.as_mut_vec::<I>().unwrap().pop().unwrap() as i64))
},
_ => Err("not a second list")
}
}
}
pub fn pop_time(&mut self) -> Result<Duration, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
else{
match self.0.qtype{
qtype::TIME_LIST => {
self.decrement();
Ok(Duration::milliseconds(self.as_mut_vec::<I>().unwrap().pop().unwrap() as i64))
},
_ => Err("not a time list")
}
}
}
pub fn pop(&mut self) -> Result<K, &'static str>{
if self.len() == 0{
Err("no element in this object")
}
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("this object cannot pop an element")
}
}
}
pub fn remove_bool(&mut self, index: usize) -> Result<bool, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::BOOL_LIST => Ok(self.as_mut_vec::<G>().unwrap().remove(index)!=0),
_ => Err("not a bool list")
}
}
}
pub fn remove_guid(&mut self, index: usize) -> Result<[u8;16], &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::GUID_LIST => Ok(self.as_mut_vec::<U>().unwrap().remove(index)),
_ => Err("not a GUID list")
}
}
}
pub fn remove_byte(&mut self, index: usize) -> Result<u8, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::BYTE_LIST => Ok(self.as_mut_vec::<G>().unwrap().remove(index)),
_ => Err("not a byte list")
}
}
}
pub fn remove_short(&mut self, index: usize) -> Result<i16, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::SHORT_LIST => Ok(self.as_mut_vec::<H>().unwrap().remove(index)),
_ => Err("not a short list")
}
}
}
pub fn remove_int(&mut self, index: usize) -> Result<i32, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::INT_LIST => Ok(self.as_mut_vec::<I>().unwrap().remove(index)),
_ => Err("not an int list")
}
}
}
pub fn remove_long(&mut self, index: usize) -> Result<i64, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::LONG_LIST => Ok(self.as_mut_vec::<J>().unwrap().remove(index)),
_ => Err("not a long list")
}
}
}
pub fn remove_real(&mut self, index: usize) -> Result<f32, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::REAL_LIST => Ok(self.as_mut_vec::<E>().unwrap().remove(index)),
_ => Err("not a real list")
}
}
}
pub fn remove_float(&mut self, index: usize) -> Result<f64, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::FLOAT_LIST => Ok(self.as_mut_vec::<F>().unwrap().remove(index)),
_ => Err("not a float list")
}
}
}
pub fn remove_char(&mut self, index: usize) -> Result<char, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::STRING => Ok(self.as_mut_string().unwrap().remove(index)),
_ => Err("not a string")
}
}
}
pub fn remove_symbol(&mut self, index: usize) -> Result<String, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::SYMBOL_LIST => Ok(self.as_mut_vec::<S>().unwrap().remove(index)),
_ => Err("not a symbol list")
}
}
}
pub fn remove_timestamp(&mut self, index: usize) -> Result<DateTime<Utc>, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::TIMESTAMP_LIST => Ok(q_timestamp_to_datetime(self.as_mut_vec::<J>().unwrap().remove(index))),
_ => Err("not a timestamp list")
}
}
}
pub fn remove_month(&mut self, index: usize) -> Result<Date<Utc>, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::MONTH_LIST => Ok(q_month_to_date(self.as_mut_vec::<I>().unwrap().remove(index))),
_ => Err("not a month list")
}
}
}
pub fn remove_date(&mut self, index: usize) -> Result<Date<Utc>, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::DATE_LIST => Ok(q_date_to_date(self.as_mut_vec::<I>().unwrap().remove(index))),
_ => Err("not a date list")
}
}
}
pub fn remove_datetime(&mut self, index: usize) -> Result<DateTime<Utc>, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::DATETIME_LIST => Ok(q_datetime_to_datetime(self.as_mut_vec::<F>().unwrap().remove(index))),
_ => Err("not a datetime list")
}
}
}
pub fn remove_timespan(&mut self, index: usize) -> Result<Duration, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::TIMESPAN_LIST => Ok(Duration::nanoseconds(self.as_mut_vec::<J>().unwrap().remove(index))),
_ => Err("not a timespan list")
}
}
}
pub fn remove_minute(&mut self, index: usize) -> Result<Duration, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::MINUTE_LIST => Ok(Duration::minutes(self.as_mut_vec::<I>().unwrap().remove(index) as i64)),
_ => Err("not a minute list")
}
}
}
pub fn remove_second(&mut self, index: usize) -> Result<Duration, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::SECOND_LIST => Ok(Duration::seconds(self.as_mut_vec::<I>().unwrap().remove(index) as i64)),
_ => Err("not a second list")
}
}
}
pub fn remove_time(&mut self, index: usize) -> Result<Duration, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
else{
match self.0.qtype{
qtype::TIME_LIST => Ok(Duration::milliseconds(self.as_mut_vec::<I>().unwrap().remove(index) as i64)),
_ => Err("not a time list")
}
}
}
pub fn remove(&mut self, index: usize) -> Result<K, &'static str>{
if index >= self.len(){
Err("index out of bounds")
}
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("this object cannot remove an element")
}
}
}
pub fn push_pair(&mut self, key: &dyn Any, value: &dyn Any) -> Result<(), &'static str>{
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("not a dictionary")
}
}
pub fn pop_pair(&mut self) -> Result<(K, K), &'static str>{
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("no element in this object")
}
},
_ => Err("not a 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
},
_ => unreachable!()
}
},
_ => unreachable!()
}
},
qtype::DICTIONARY | qtype::SORTED_DICTIONARY => {
match &self.as_vec::<K>().unwrap()[0].0.value{
k0_inner::list(list) => list.n as usize,
_ => unreachable!()
}
},
_ => 1
}
}
pub fn flip(self) -> Result<Self, 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(self)
}
},
_ => Err(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().saturating_sub(KDB_TIMESTAMP_OFFSET)
}
}
fn date_to_q_month(month: Date<Utc>) -> 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: Date<Utc>) -> 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= Date::signed_duration_since(date, Utc.ymd(1970, 1, 1)).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(crate) fn q_timestamp_to_datetime(nanos: i64) -> DateTime<Utc>{
Utc.timestamp_nanos(nanos) + Duration::nanoseconds(KDB_TIMESTAMP_OFFSET)
}
pub(crate) fn q_month_to_date(months: i32) -> Date<Utc>{
if months == qnull_base::I{
qnull::MONTH
}
else if months <= -3171072{
*qninf::MONTH
}
else if months >= 3121728{
*qinf::MONTH
}
else{
Utc.ymd(2000 + months / 12, 1 + (months % 12) as u32, 1)
}
}
pub(crate) fn q_date_to_date(days: i32) -> Date<Utc>{
if days == qnull_base::I{
qnull::DATE
}
else if days <= -96476615{
*qninf::DATE
}
else if days >= 95015644{
qinf::DATE
}
else{
(Utc.ymd(2000, 1, 1).and_hms(0, 0, 0) + Duration::days(days as i64)).date()
}
}
pub(crate) 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((ONE_DAY_MILLIS as f64 * (days + KDB_DAY_OFFSET as f64)) as i64)
}
}
pub(crate) fn q_timespan_to_duration(nanos: i64) -> Duration{
Duration::nanoseconds(nanos)
}
pub(crate) fn q_minute_to_duration(minutes: i32) -> Duration{
Duration::minutes(minutes as i64)
}
pub(crate) fn q_second_to_duration(seconds: i32) -> Duration{
Duration::seconds(seconds as i64)
}
pub(crate) fn q_time_to_duration(millis: i32) -> Duration{
Duration::milliseconds(millis as i64)
}
mod format;
mod serialize;
mod deserialize;
mod connection;
pub use connection::*;