pbsl/lib.rs
1mod bindings;
2
3use chrono::{
4 Datelike,
5 naive::{NaiveDate, NaiveDateTime, NaiveTime}, Timelike,
6};
7use num_enum::TryFromPrimitive;
8use rust_decimal::Decimal;
9use std::ptr::NonNull;
10use widestring::WideCString;
11use crate::bindings::array::*;
12use crate::bindings::packet::*;
13
14pub type PBString = WideCString;
15
16// pub type Pob_Data = Option<*mut ObData>;
17//
18// #[repr(C)]
19// pub struct _POBVM([u8; 0]);
20// pub type pobvm = NonNull<_POBVM>;
21//
22// #[repr(transparent)]
23// pub struct ObVm {
24// ptr: pobvm,
25// }
26// impl ObVm {
27// pub(crate) fn as_ptr(&self) -> pobvm {
28// self.ptr
29// }
30// ///
31// /// 获取value指类型参数
32// ///
33// pub fn get_next_arg(&self) -> Option<&ObData> {
34// unsafe { OT_GET_NEXT_EVALED_ARG_NO_CONVERT(self.as_ptr()).as_ref() }
35// }
36// ///
37// /// 获取ref引用类型参数
38// ///
39// pub fn get_next_lvalue_arg(&self, hnd: &mut u32) -> Option<&mut ObData> {
40// unsafe { OT_GET_NEXT_LVALUE_ARG(self.as_ptr(), hnd).as_mut() }
41// }
42// ///
43// /// 获取当前调用实例
44// ///
45// pub fn get_curr_obinst(&self,pobinst:&ObClass,nullval:&mut bool){
46// unsafe { OT_GET_CURR_OBINST_EXPR(self.as_ptr(), pobinst,nullval) };
47// }
48// ///
49// /// 获取当前调用实例字段
50// ///
51// pub fn get_obinst_field(&self,pobinst:&ObClass,field_id:u32,data:&mut ObData){
52// unsafe {OB_GET_FIELD(self.as_ptr(),pobinst.as_ptr(),field_id,data)};
53// }
54// ///
55// /// 设置当前调用实例字段
56// ///
57// pub fn set_obinst_field(&self,pobinst:&ObClass,field_id:u32,data:&mut ObData){
58// unsafe { OB_SET_FIELD(self.as_ptr(),pobinst.as_ptr(),field_id,data)}
59// }
60// }
61// impl ObVm {
62// pub fn no_return_val(&self) {
63// unsafe { OT_NO_RETURN_VAL(self.as_ptr()) };
64// }
65// pub fn set_return_val(&self, data: &ObData) {
66// unsafe { OT_SET_RETURN_VAL(self.as_ptr(), data) };
67// }
68// pub fn set_return_long(&self, val: i32) {
69// let data = ObData::new(val, ValueType::Long);
70// unsafe { OT_SET_RETURN_VAL(self.as_ptr(), &data) };
71// }
72// }
73// impl ObVm {
74// pub fn dup_string<'a>(&self, val: &'a [u16]) -> &'a [u16] {
75// unsafe { OB_DUP_STRING(self.as_ptr(), val) }
76// }
77// pub fn dup_blob<'a>(&self, val: &'a Psh_Binary) -> &'a Psh_Binary {
78// unsafe { OB_DUP_BLOB(self.as_ptr(), val) }
79// }
80// pub fn dup_dec<'a>(&self, val: &'a Psh_Dec) -> &'a Psh_Dec {
81// unsafe { OB_DUP_DEC(self.as_ptr(), val) }
82// }
83// pub fn dup_double<'a>(&self, val: &'a f64) -> &'a f64 {
84// unsafe { OB_DUP_DOUBLE(self.as_ptr(), val) }
85// }
86// pub fn dup_longlong<'a>(&self, val: &'a i64) -> &'a i64 {
87// unsafe { OB_DUP_LONGLONG(self.as_ptr(), val) }
88// }
89// pub fn dup_time<'a>(&self, val: &'a Psh_Time) -> &'a Psh_Time {
90// unsafe { OB_DUP_TIME(self.as_ptr(), val) }
91// }
92// }
93// impl ObVm {
94// ///
95// /// 获取数组长度
96// ///
97// pub fn get_array_num_items(&self, ptr: Parray) -> u32 {
98// unsafe { OT_ARRAY_NUM_ITEMS(self.as_ptr(), ptr) }
99// }
100// ///
101// /// 获取数组数据引用
102// ///
103// pub fn get_array_index(&self, ptr: Parray, index: u32) -> &ObData {
104// unsafe { &*(OT_ARRAY_INDEX(self.as_ptr(), ptr, index)) }
105// }
106// ///
107// /// 获取数组数据引用mut
108// ///
109// pub fn get_array_index_mut(&self, ptr: Parray, index: u32) -> &mut ObData {
110// unsafe { &mut *(OT_ARRAY_INDEX(self.as_ptr(), ptr, index)) }
111// }
112// ///
113// /// create boundarray
114// ///
115// pub fn array_create_bounded(
116// &self,
117// num_items: u32,
118// elmttype: Ob_Class_Hndl,
119// varinfo: u16,
120// dim: u16,
121// boundsarray: PVOID,
122// ) -> Parray {
123// unsafe {
124// OT_ARRAY_CREATE_BOUNDED(
125// self.as_ptr(),
126// num_items,
127// elmttype,
128// varinfo,
129// dim,
130// boundsarray,
131// )
132// }
133// }
134// ///
135// /// create unboundarray
136// ///
137// pub fn array_create_unbounded(&self, elmttype: Ob_Class_Hndl, varinfo: u16) -> Parray {
138// unsafe { OT_ARRAY_CREATE_UNBOUNDED(self.as_ptr(), elmttype, varinfo) }
139// }
140// }
141//
142// ///
143// /// refpak method
144// ///
145// impl ObVm {
146// pub fn assign_ref_int(&self, refpak: &OT_REF_PAK, val: i32, nullval: bool) {
147// unsafe { OT_ASSIGN_REF_INT(self.as_ptr(), refpak, val, nullval) };
148// }
149// pub fn assign_ref_uint(&self, refpak: &OT_REF_PAK, val: u32, nullval: bool) {
150// unsafe { OT_ASSIGN_REF_UINT(self.as_ptr(), refpak, val, nullval) };
151// }
152// pub fn assign_ref_byte(&self, refpak: &OT_REF_PAK, val: u8, nullval: bool) {
153// unsafe { OT_ASSIGN_REF_BYTE(self.as_ptr(), refpak, val, nullval) };
154// }
155// pub fn assign_ref_long(&self, refpak: &OT_REF_PAK, val: i32, nullval: bool) {
156// unsafe { OT_ASSIGN_REF_LONG(self.as_ptr(), refpak, val, nullval) };
157// }
158// pub fn assign_ref_ulong(&self, refpak: &OT_REF_PAK, val: u32, nullval: bool) {
159// unsafe { OT_ASSIGN_REF_ULONG(self.as_ptr(), refpak, val, nullval) };
160// }
161// pub fn assign_ref_dec(&self, refpak: &OT_REF_PAK, val: &Psh_Dec, nullval: bool) {
162// unsafe { OT_ASSIGN_REF_DEC(self.as_ptr(), refpak, val, nullval) };
163// }
164// pub fn assign_ref_float(&self, refpak: &OT_REF_PAK, val: f32, nullval: bool) {
165// unsafe { OT_ASSIGN_REF_FLOAT(self.as_ptr(), refpak, val, nullval) };
166// }
167// pub fn assign_ref_double(&self, refpak: &OT_REF_PAK, val: &f64, nullval: bool) {
168// unsafe { OT_ASSIGN_REF_DOUBLE(self.as_ptr(), refpak, val, nullval) };
169// }
170// pub fn assign_ref_longlong(&self, refpak: &OT_REF_PAK, val: &i64, nullval: bool) {
171// unsafe { OT_ASSIGN_REF_LONGLONG(self.as_ptr(), refpak, val, nullval) };
172// }
173// pub fn assign_ref_string(&self, refpak: &OT_REF_PAK, val: &[u16], nullval: bool) {
174// unsafe { OT_ASSIGN_REF_STRING(self.as_ptr(), refpak, val, nullval) };
175// }
176// pub fn assign_ref_bool(&self, refpak: &OT_REF_PAK, val: bool, nullval: bool) {
177// unsafe { OT_ASSIGN_REF_BOOL(self.as_ptr(), refpak, val, nullval) };
178// }
179// pub fn assign_ref_char(&self, refpak: &OT_REF_PAK, val: u16, nullval: bool) {
180// unsafe { OT_ASSIGN_REF_CHAR(self.as_ptr(), refpak, val, nullval) };
181// }
182// pub fn assign_ref_blob(&self, refpak: &OT_REF_PAK, val: &Psh_Binary, nullval: bool) {
183// unsafe { OT_ASSIGN_REF_BLOB(self.as_ptr(), refpak, val, nullval) };
184// }
185// pub fn assign_ref_datetime(&self, refpak: &OT_REF_PAK, val: &Psh_Time, nullval: bool) {
186// unsafe { OT_ASSIGN_REF_DATETIME(self.as_ptr(), refpak, val, nullval) };
187// }
188// pub fn assign_ref_obinst(&self, refpak: &OT_REF_PAK, val: PVOID, nullval: bool, ty: u16) {
189// unsafe { OT_ASSIGN_REF_OBINST(self.as_ptr(), refpak, val, nullval, ty) };
190// }
191// pub fn assign_ref_enum(&self, refpak: &OT_REF_PAK, val: i32, nullval: bool, ty: u16) {
192// unsafe { OT_ASSIGN_REF_ENUM(self.as_ptr(), refpak, val, nullval, ty) };
193// }
194// pub fn assign_ref_array(&self, refpak: &OT_REF_PAK, val: Parray, nullval: bool, ty: u16) {
195// unsafe { OT_ASSIGN_REF_ARRAY(self.as_ptr(), refpak, val, nullval, ty) };
196// }
197// pub fn assign_ref_any(&self, refpak: &OT_REF_PAK, val: &ObData, rhs_class_id: u16) {
198// unsafe { OT_ASSIGN_REF_ANY(self.as_ptr(), refpak, val, rhs_class_id) };
199// }
200// }
201
202pub type ObInfo = u16;
203#[repr(C, packed(1))]
204pub struct ObData {
205 val: UnionValue,
206 info: ObInfo,
207 r#type: ValueType,
208}
209
210#[derive(Default)]
211#[repr(C)]
212pub struct UnionValue {
213 data: [u8; 4],
214}
215
216#[repr(C)]
217pub struct _POBDATAVALUE([u8;0]);
218pub type pobdatavalue = NonNull<_POBDATAVALUE>;
219///
220/// 对象实例
221/// Ob_INST_ID
222///
223///
224#[repr(C)]
225pub struct _POBINSTID([u8; 0]);
226pub type pobinstid = NonNull<_POBINSTID>;
227
228#[repr(transparent)]
229pub struct ObInstId {
230 ptr: pobinstid,
231}
232
233pub trait AsValue {
234 fn asvalue(&self) -> UnionValue;
235}
236
237impl AsValue for u8 {
238 fn asvalue(&self) -> UnionValue {
239 UnionValue {
240 data: u32::to_le_bytes((*self).into()),
241 }
242 }
243}
244impl AsValue for i32 {
245 fn asvalue(&self) -> UnionValue {
246 UnionValue {
247 data: i32::to_le_bytes(*self),
248 }
249 }
250}
251impl AsValue for u32 {
252 fn asvalue(&self) -> UnionValue {
253 UnionValue {
254 data: u32::to_le_bytes(*self),
255 }
256 }
257}
258impl AsValue for i16 {
259 fn asvalue(&self) -> UnionValue {
260 UnionValue {
261 data: i32::to_le_bytes((*self).into()),
262 }
263 }
264}
265impl AsValue for u16 {
266 fn asvalue(&self) -> UnionValue {
267 UnionValue {
268 data: u32::to_le_bytes((*self).into()),
269 }
270 }
271}
272impl AsValue for f32 {
273 fn asvalue(&self) -> UnionValue {
274 UnionValue {
275 data: f32::to_le_bytes(*self),
276 }
277 }
278}
279impl AsValue for bool {
280 fn asvalue(&self) -> UnionValue {
281 UnionValue {
282 data: [*self as u8, 0, 0, 0],
283 }
284 }
285}
286
287// pub trait AsPtrValue {
288// fn asptrvalue(&self, obThis: &POB_THIS) -> UnionValue;
289// }
290// impl AsPtrValue for &f64 {
291// fn asptrvalue(&self, obThis: &POB_THIS) -> UnionValue {
292// UnionValue {
293// data: (obThis.dup_double(self) as *const _ as usize).to_le_bytes(),
294// }
295// }
296// }
297// impl AsPtrValue for &str {
298// fn asptrvalue(&self, obThis: &POB_THIS) -> UnionValue {
299// let stru16 = self.encode_utf16().collect::<Vec<u16>>();
300// UnionValue {
301// data: (obThis.dup_string(&stru16[0..]).as_ptr() as usize).to_le_bytes(),
302// }
303// }
304// }
305// impl AsPtrValue for &[u16] {
306// fn asptrvalue(&self, obThis: &POB_THIS) -> UnionValue {
307// UnionValue {
308// data: (obThis.dup_string(self).as_ptr() as usize).to_le_bytes(),
309// }
310// }
311// }
312// impl AsPtrValue for &i64 {
313// fn asptrvalue(&self, obThis: &POB_THIS) -> UnionValue {
314// UnionValue {
315// data: (obThis.dup_longlong(self) as *const _ as usize).to_le_bytes(),
316// }
317// }
318// }
319// impl AsPtrValue for &[u8] {
320// fn asptrvalue(&self, obThis: &POB_THIS) -> UnionValue {
321// let ulen = self.len() as u32;
322// let pblob = Psh_Binary {
323// len: ulen,
324// data: [0],
325// };
326// let dp = obThis.dup_blob(&pblob);
327// let src = self.as_ptr();
328// let dst = &dp.data as *const _ as *mut u8;
329// let _ = unsafe { std::ptr::copy_nonoverlapping(src, dst, ulen as usize) };
330// UnionValue {
331// data: (dp as *const _ as usize).to_le_bytes(),
332// }
333// }
334// }
335// impl AsPtrValue for &Psh_Dec {
336// fn asptrvalue(&self, obThis: &POB_THIS) -> UnionValue {
337// UnionValue {
338// data: (obThis.dup_dec(self) as *const _ as usize).to_le_bytes(),
339// }
340// }
341// }
342// impl AsPtrValue for &Psh_Time {
343// fn asptrvalue(&self, obThis: &POB_THIS) -> UnionValue {
344// UnionValue {
345// data: (obThis.dup_time(self) as *const _ as usize).to_le_bytes(),
346// }
347// }
348// }
349// impl AsPtrValue for Parray {
350// fn asptrvalue(&self, obThis: &POB_THIS) -> UnionValue {
351// let p = *self as *const _ as usize;
352// let p2 = p;
353// UnionValue {
354// data: (*self as *const _ as usize).to_le_bytes(),
355// }
356// }
357// }
358
359impl Default for ObData {
360 fn default() -> Self {
361 Self {
362 val: UnionValue::default(),
363 info: Default::default(),
364 r#type: ValueType::NoType,
365 }
366 }
367}
368
369impl ObData {
370 pub fn new(value: impl AsValue, valtype: ValueType) -> Self {
371 ObData {
372 val: value.asvalue(),
373 info: valtype.into_obinfo_value(),
374 r#type: valtype,
375 }
376 }
377 // pub fn new_ptrvalue(obThis: &POB_THIS, ptrvalue: impl AsPtrValue, valtype: ValueType) -> Self {
378 // ObData {
379 // val: ptrvalue.asptrvalue(obThis),
380 // info: valtype.into_obinfo_value(),
381 // r#type: valtype,
382 // }
383 // }
384}
385
386impl ObData {
387 pub fn as_ptr(&self) -> PVOID {
388 self as *const _ as PVOID
389 }
390}
391
392impl ObData{
393 ///
394 /// 是否引用类型的值
395 ///
396 pub fn val_isptr(&self)->bool{
397 todo!()
398 }
399 ///
400 /// 返回引用类型值地址
401 ///
402 pub fn val_as_ptr(&self)->pobdatavalue{
403 let d = unsafe{&*(usize::from_le_bytes(self.val.data) as *mut _POBDATAVALUE)};
404 pobdatavalue::from(d)
405 }
406}
407
408impl ObData {
409 pub fn get_valptr<T>(&self) -> *const T {
410 usize::from_le_bytes(self.val.data) as *const T
411 }
412 pub fn get_type(&self) -> ValueType {
413 self.r#type
414 }
415 pub fn get_type_unchecked(&self) -> u16 {
416 self.r#type.into()
417 }
418 pub fn get_byte_unchecked(&self) -> u8 {
419 self.val.data[0]
420 }
421 pub fn get_char_unchecked(&self) -> u16 {
422 u16::from_le_bytes([self.val.data[0], self.val.data[1]])
423 }
424 pub fn get_long_unchecked(&self) -> i32 {
425 i32::from_le_bytes(self.val.data)
426 }
427 pub fn get_ulong_unchecked(&self) -> u32 {
428 u32::from_le_bytes(self.val.data)
429 }
430 pub fn get_int_unchecked(&self) -> i16 {
431 i16::from_le_bytes([self.val.data[0], self.val.data[1]])
432 }
433 pub fn get_uint_unchecked(&self) -> u16 {
434 u16::from_le_bytes([self.val.data[0], self.val.data[1]])
435 }
436 pub fn get_real_unchecked(&self) -> f32 {
437 f32::from_le_bytes(self.val.data)
438 }
439 pub fn get_bool_unchecked(&self) -> bool {
440 self.val.data[0] == 1
441 }
442 pub fn get_string_unchecked(&self) -> PBString {
443 unsafe { PBString::from_ptr_str(self.get_valptr::<u16>()) }
444 }
445 pub fn get_double_unchecked(&self) -> f64 {
446 unsafe { *(self.get_valptr::<f64>()) }
447 }
448 pub fn get_decimal_unchecked(&self) -> Decimal {
449 let psh_dec = unsafe { &*self.get_valptr::<Psh_Dec>() };
450 psh_dec.into()
451 }
452 pub fn get_pbdec_unchecked(&self) -> &Psh_Dec {
453 unsafe { &*self.get_valptr::<Psh_Dec>() }
454 }
455 pub fn get_pbblob_unchecked(&self) -> &Psh_Binary {
456 unsafe { &*(self.get_valptr::<Psh_Binary>()) }
457 }
458 pub fn get_blob_unchecked(&self) -> Vec<u8> {
459 self.get_pbblob_unchecked().into()
460 }
461 pub fn get_pbdate_unchecked(&self) -> &Psh_Time {
462 unsafe { &*(self.get_valptr::<Psh_Time>()) }
463 }
464 pub fn get_longlong_unchecked(&self) -> i64 {
465 unsafe { *(self.get_valptr::<i64>()) }
466 }
467 pub fn get_object_unchecked(&self) -> &ObInstId {
468 unsafe { &*(self.get_valptr::<ObInstId>()) }
469 }
470 pub fn get_arrayid_unchecked(&self) -> Parray {
471 usize::from_le_bytes(self.val.data) as Parray
472 }
473}
474
475///
476/// array
477///
478impl ObData {
479 pub fn get_data_array(&self) {
480 todo!()
481 }
482 pub fn get_data_arrayinst(&self) {
483 todo!()
484 }
485}
486
487impl ObData {
488 pub fn get_valptr_ref<T>(&mut self) -> *mut T {
489 usize::from_le_bytes(self.val.data) as *mut T
490 }
491 pub fn get_refpak_unchecked(&mut self) -> &mut OT_REF_PAK {
492 unsafe { &mut *(usize::from_le_bytes(self.val.data) as *mut OT_REF_PAK) }
493 }
494}
495
496impl ObData {
497 pub fn set_data_value<T>(&mut self, val: &T)
498 where
499 T: AsValue,
500 {
501 self.val = val.asvalue();
502 }
503 // pub fn set_data_ptrvalue<T>(&mut self, obThis: &POB_THIS, val: &T)
504 // where
505 // T: AsPtrValue,
506 // {
507 // self.val = val.asptrvalue(obThis)
508 // }
509 pub fn set_data_ptr(&mut self,val:usize){
510 self.val.data = val.to_le_bytes()
511 }
512}
513
514impl ValueType {
515 pub fn into_obinfo_value(self) -> ObInfo {
516 match self {
517 ValueType::NoType => {
518 todo!()
519 }
520 ValueType::Int
521 | ValueType::Uint
522 | ValueType::Boolean
523 | ValueType::Char
524 | ValueType::Byte => 0x05C0,
525 ValueType::Long | ValueType::Ulong => 0x1DC0,
526 ValueType::Real => 0x0900,
527 ValueType::Double => 0x0D00,
528 ValueType::Decimal => 0x0D00,
529 ValueType::String => 0x0D00,
530 ValueType::Any => todo!(),
531 ValueType::Blob => 0x0D00,
532 ValueType::Date => 0x0D00,
533 ValueType::Time => 0x0D00,
534 ValueType::DateTime => 0x0D00,
535 ValueType::Dummy1 => todo!(),
536 ValueType::Dummy2 => todo!(),
537 ValueType::Dummy3 => todo!(),
538 ValueType::Dummy4 => todo!(),
539 ValueType::LongLong => 0x0D00,
540 }
541 }
542 pub fn into_obinfo_readonly(self) -> ObInfo {
543 todo!()
544 }
545 pub fn into_obinfo_ref(self) -> ObInfo {
546 match self {
547 ValueType::NoType => {
548 todo!()
549 }
550 ValueType::Int
551 | ValueType::Uint
552 | ValueType::Boolean
553 | ValueType::Char
554 | ValueType::Byte => 0x05C0,
555 ValueType::Long | ValueType::Ulong => 0x1DC0,
556 ValueType::Real => 0x0980,
557 ValueType::Double => 0x0D00,
558 ValueType::Decimal => 0x0D00,
559 ValueType::String => 0x0D00,
560 ValueType::Any => todo!(),
561 ValueType::Blob => 0x0D00,
562 ValueType::Date => 0x0D00,
563 ValueType::Time => 0x0D00,
564 ValueType::DateTime => 0x0D00,
565 ValueType::Dummy1 => todo!(),
566 ValueType::Dummy2 => todo!(),
567 ValueType::Dummy3 => todo!(),
568 ValueType::Dummy4 => todo!(),
569 ValueType::LongLong => 0x0D00,
570 }
571 }
572 pub fn into_obinfo_null(self) -> ObInfo {
573 todo!()
574 }
575}
576
577#[repr(u16)]
578#[non_exhaustive]
579#[derive(Debug, Copy, Clone, PartialEq, Eq, TryFromPrimitive)]
580pub enum ValueType {
581 NoType = 0,
582 Int,
583 Long,
584 Real,
585 Double,
586 Decimal,
587 String,
588 Boolean,
589 Any,
590 Uint,
591 Ulong,
592 Blob,
593 Date,
594 Time,
595 DateTime,
596 Dummy1,
597 Dummy2,
598 Dummy3,
599 Char,
600 Dummy4,
601 LongLong,
602 Byte,
603}
604impl Into<u16> for ValueType {
605 fn into(self) -> u16 {
606 self as u16
607 }
608}
609
610#[repr(u16)]
611#[non_exhaustive]
612#[derive(Debug, Copy, Clone, PartialEq, Eq)]
613pub enum Ob_Data_Style {
614 UNDECLARED_STYLE = 0,
615 INT_STYLE = 1,
616 FLOAT_STYLE = 2,
617 PTR_STYLE = 3,
618 CONST_STYLE = 4,
619 ID_STYLE = 5,
620 OBINST_STYLE = 6,
621 LONG_STYLE = 7,
622}
623
624#[repr(u16)]
625#[non_exhaustive]
626#[derive(Debug, Copy, Clone, PartialEq, Eq)]
627pub enum FieldType {
628 OB_TYPEDEF_FIELD = 0, // For instance variables
629 OB_INSTVAR_FIELD = 0x0500,
630}
631#[repr(u16)]
632#[non_exhaustive]
633#[derive(Debug, Copy, Clone, PartialEq, Eq)]
634pub enum ValAccess {
635 OB_GLOBAL_VAR = 0, // For globally scoped variables
636 OB_SHARED_VAR = 1,
637}
638
639#[repr(u16)]
640#[non_exhaustive]
641#[derive(Debug, Copy, Clone, PartialEq, Eq)]
642pub enum Ob_Group_Types {
643 OB_SIMPLE = 0,
644 OB_ARRAY,
645}
646
647#[repr(C)]
648pub struct Ob_Class_Hndl {
649 pub(crate) group_hndl: u16,
650 pub(crate) class_id: u16,
651}
652
653#[repr(C)]
654pub struct Psh_Binary {
655 len: u32,
656 data: [u8; 1],
657}
658
659impl Into<Vec<u8>> for &Psh_Binary {
660 fn into(self) -> Vec<u8> {
661 unsafe {
662 Vec::from_raw_parts(
663 self.data.as_ptr() as *mut u8,
664 self.len as usize,
665 self.len as usize,
666 )
667 }
668 //unsafe{Vec::from_raw_parts(self.data as *const u8 as *mut u8, self.len as usize, self.len as usize)}
669 }
670}
671
672impl Psh_Binary {
673 fn len(&self) -> u32 {
674 self.len
675 }
676}
677
678#[repr(C)]
679pub struct Psh_Dec {
680 v: [u16; 7],
681 flags: [u8; 2],
682}
683/* flags 1:小数位,0:正负 */
684/* v:u128 */
685impl Into<Decimal> for &Psh_Dec {
686 fn into(self) -> Decimal {
687 let num: u128 = u128::from(self.v[0])
688 | (u128::from(self.v[1]) << 16)
689 | (u128::from(self.v[2]) << 32)
690 | (u128::from(self.v[3]) << 48)
691 | (u128::from(self.v[4]) << 64)
692 | (u128::from(self.v[5]) << 80)
693 | (u128::from(self.v[6]) << 96);
694 let scale = self.flags[1];
695 let isnag = self.flags[0];
696
697 let rt: i128 = match isnag {
698 1 => (!num + 1) as i128,
699 _ => num as i128,
700 };
701 Decimal::from_i128_with_scale(rt, scale.into())
702 }
703}
704impl From<Decimal> for Psh_Dec {
705 fn from(dec: Decimal) -> Self {
706 let isnag = dec.is_sign_negative();
707 let scale = dec.scale();
708 let mants = match isnag {
709 true => (!dec.mantissa() + 1) as u128,
710 false => dec.mantissa() as u128,
711 };
712 let b = mants.to_le_bytes();
713 Psh_Dec {
714 v: [
715 u16::from_le_bytes([b[0], b[1]]),
716 u16::from_le_bytes([b[2], b[3]]),
717 u16::from_le_bytes([b[4], b[5]]),
718 u16::from_le_bytes([b[6], b[7]]),
719 u16::from_le_bytes([b[8], b[9]]),
720 u16::from_le_bytes([b[10], b[11]]),
721 u16::from_le_bytes([b[12], b[13]]),
722 ],
723 flags: [isnag.into(), scale.to_le_bytes()[0]],
724 }
725 }
726}
727
728#[derive(Default)]
729#[repr(C)]
730pub struct Psh_Time {
731 tm_msec: u32,
732 tm_year: i16,
733 tm_mon: u8,
734 tm_day: u8,
735 tm_hour: u8,
736 tm_min: u8,
737 tm_sec: u8,
738 tm_filter: u8,
739}
740impl Into<NaiveDate> for &Psh_Time {
741 fn into(self) ->NaiveDate{
742 NaiveDate::from_ymd_opt(
743 (self.tm_year + 1900).into(),
744 (self.tm_mon + 1).into(),
745 self.tm_day.into()
746 ).unwrap()
747 }
748}
749impl Into<NaiveTime> for &Psh_Time {
750 fn into(self) -> NaiveTime {
751 NaiveTime::from_hms_milli_opt(
752 self.tm_hour.into(),
753 self.tm_min.into(),
754 self.tm_sec.into(),
755 self.tm_msec,
756 ).unwrap()
757 }
758}
759impl Into<NaiveDateTime> for &Psh_Time {
760 fn into(self) -> NaiveDateTime {
761 NaiveDateTime::new(self.into(), self.into())
762 }
763}
764
765impl From<NaiveDate> for Psh_Time {
766 fn from(nd: NaiveDate) -> Self {
767 Psh_Time {
768 tm_year: (nd.year() - 1900) as i16,
769 tm_mon: (nd.month() - 1) as u8,
770 tm_day: nd.day() as u8,
771 ..Default::default()
772 }
773 }
774}
775impl From<NaiveTime> for Psh_Time {
776 fn from(nt: NaiveTime) -> Self {
777 Psh_Time {
778 tm_hour: nt.hour() as u8,
779 tm_min: nt.minute() as u8,
780 tm_sec: nt.second() as u8,
781 tm_msec: nt.nanosecond() / 1_000_000,
782 ..Default::default()
783 }
784 }
785}
786impl From<NaiveDateTime> for Psh_Time {
787 fn from(ndt: NaiveDateTime) -> Self {
788 Psh_Time {
789 tm_year: (ndt.year() - 1900) as i16,
790 tm_mon: (ndt.month() - 1) as u8,
791 tm_day: ndt.day() as u8,
792 tm_hour: ndt.hour() as u8,
793 tm_min: ndt.minute() as u8,
794 tm_sec: ndt.second() as u8,
795 tm_msec: ndt.nanosecond() / 1_000_000,
796 ..Default::default()
797 }
798 }
799}
800
801#[test]
802fn test_dec() {
803 /* pb -0.00123451 */
804 let psh_dec = Psh_Dec {
805 v: [57915, 1, 0, 0, 0, 0, 0],
806 flags: [1, 8],
807 };
808 let mydec: Decimal = (&psh_dec).into();
809 assert_eq!(Decimal::from_i128_with_scale(-123451, 8), mydec);
810
811 let dec = Decimal::from_i128_with_scale(-123451, 8);
812 let pdec = Psh_Dec::from(dec);
813 assert_eq!([57915, 1, 0, 0, 0, 0, 0], pdec.v);
814 assert_eq!([1, 8], pdec.flags);
815}