1use ::{_API};
161
162use capi::sctypes::*;
163use capi::scvalue::{VALUE_UNIT_TYPE_STRING, VALUE_UNIT_TYPE_OBJECT, VALUE_UNIT_UNDEFINED};
164pub use capi::scvalue::{VALUE_RESULT, VALUE_STRING_CVT_TYPE, VALUE_TYPE};
165use capi::scvalue::VALUE;
166use ::om::IAsset;
167
168
169pub struct Value
175{
176 data: VALUE,
177 tmp: * mut Value,
178}
179
180unsafe impl Send for Value {}
182
183impl Value {
184
185 pub const fn new() -> Value {
187 Value { data: VALUE::new(), tmp: ::std::ptr::null_mut() }
188 }
189
190 pub fn array(length: usize) -> Value {
192 let mut me = Value::new();
193 (_API.ValueIntDataSet)(me.as_ptr(), length as i32, VALUE_TYPE::T_ARRAY as UINT, 0);
194 return me;
195 }
196
197 pub fn map() -> Value {
199 let mut me = Value::new();
200 (_API.ValueIntDataSet)(me.as_ptr(), 0i32, VALUE_TYPE::T_MAP as UINT, 0);
201 return me;
202 }
203
204 pub const fn null() -> Value {
206 let mut me = Value::new();
207 me.data.t = VALUE_TYPE::T_NULL;
208 return me;
209 }
210 pub const fn nothing() -> Value {
212 let mut me = Value::new();
213 me.data.t = VALUE_TYPE::T_UNDEFINED;
214 me.data.u = VALUE_UNIT_UNDEFINED::UT_NOTHING as UINT;
215 return me;
216 }
217
218 pub fn symbol(val: &str) -> Value {
220 let mut me = Value::new();
221 me.assign_str(val, VALUE_UNIT_TYPE_STRING::SYMBOL);
222 return me;
223 }
224
225 pub fn error(val: &str) -> Value {
227 let mut me = Value::new();
228 me.assign_str(val, VALUE_UNIT_TYPE_STRING::ERROR);
229 return me;
230 }
231
232 pub fn color(val: u32) -> Value {
234 let mut me = Value::new();
235 (_API.ValueIntDataSet)(me.as_ptr(), val as i32, VALUE_TYPE::T_COLOR as u32, 0);
236 return me;
237 }
238
239 pub fn duration(val: f64) -> Value {
241 let mut me = Value::new();
242 (_API.ValueFloatDataSet)(me.as_ptr(), val, VALUE_TYPE::T_DURATION as u32, 0);
243 return me;
244 }
245
246 pub fn angle(val: f64) -> Value {
248 let mut me = Value::new();
249 (_API.ValueFloatDataSet)(me.as_ptr(), val, VALUE_TYPE::T_ANGLE as u32, 0);
250 return me;
251 }
252
253 pub fn parse(val: &str) -> Result<Value, usize> {
255 return Value::parse_as(val, VALUE_STRING_CVT_TYPE::JSON_LITERAL);
256 }
257
258 pub fn parse_as(val: &str, how: VALUE_STRING_CVT_TYPE) -> Result<Value, usize> {
260 let mut me = Value::new();
261 let (s,n) = s2wn!(val);
262 let ok: u32 = (_API.ValueFromString)(me.as_ptr(), s.as_ptr(), n, how);
263 if ok == 0 {
264 Ok(me)
265 } else {
266 Err(ok as usize)
267 }
268 }
269
270 pub fn to_asset<T>(&self) -> Option<&mut IAsset<T>> {
272 if self.is_asset() {
273 let mut val = 0_i64;
274 if (_API.ValueInt64Data)(self.as_cptr(), &mut val) == VALUE_RESULT::OK {
275 let ptr = val as usize as *mut IAsset<T>;
276 let asset = unsafe { &mut *ptr };
277 return Some(asset);
278 }
279 }
280 return None;
281 }
282
283 pub fn as_mut_ptr(&mut self) -> *mut VALUE {
285 &mut self.data as *mut VALUE
286 }
287
288 #[doc(hidden)]
289 pub fn as_ptr(&mut self) -> *mut VALUE {
290 &mut self.data as *mut VALUE
291 }
292
293 #[doc(hidden)]
294 pub fn as_cptr(&self) -> *const VALUE {
295 &self.data as *const VALUE
296 }
297
298 pub const fn get_type(&self) -> VALUE_TYPE {
300 return self.data.t;
301 }
302
303 pub const fn full_type(&self) -> (VALUE_TYPE, UINT) {
305 return (self.data.t, self.data.u);
306 }
307
308 pub fn isolate(&mut self) {
312 (_API.ValueIsolate)(self.as_ptr());
313 }
314
315 pub fn clear(&mut self) -> &mut Value {
317 (_API.ValueClear)(self.as_ptr());
318 self
319 }
320
321 pub fn len(&self) -> usize {
323 let mut n: INT = 0;
324 (_API.ValueElementsCount)(self.as_cptr(), &mut n);
325 return n as usize;
326 }
327
328 pub fn push<T: Into<Value>>(&mut self, src: T) {
330 (_API.ValueNthElementValueSet)(self.as_ptr(), self.len() as INT, src.into().as_cptr());
331 }
332
333 pub fn set<T: Into<Value>>(&mut self, index: usize, src: T) {
335 (_API.ValueNthElementValueSet)(self.as_ptr(), index as INT, src.into().as_cptr());
336 }
337
338 pub fn get(&self, index: usize) -> Value {
345 let mut v = Value::new();
346 (_API.ValueNthElementValue)(self.as_cptr(), index as INT, v.as_ptr());
347 return v;
348 }
349
350 pub fn set_item<TKey: Into<Value>, TValue: Into<Value>>(&mut self, key: TKey, value: TValue) {
358 (_API.ValueSetValueToKey)(self.as_ptr(), key.into().as_cptr(), value.into().as_cptr());
359 }
360
361 pub fn get_item<T: Into<Value>>(&self, key: T) -> Value {
363 let mut v = Value::new();
364 (_API.ValueGetValueOfKey)(self.as_cptr(), key.into().as_cptr(), v.as_ptr());
365 return v;
366 }
367
368 pub fn key_at(&self, index: usize) -> Value {
370 let mut v = Value::new();
371 (_API.ValueNthElementKey)(self.as_cptr(), index as INT, v.as_ptr());
372 return v;
373 }
374
375 pub fn keys(&self) -> KeyIterator {
383 KeyIterator {
384 base: self,
385 index: 0,
386 count: self.len(),
387 }
388 }
389
390 pub fn values(&self) -> SeqIterator {
399 SeqIterator {
400 base: self,
401 index: 0,
402 count: self.len(),
403 }
404 }
405
406 pub fn items(&self) -> Vec<(Value, Value)> {
412 type VecType = Vec<(Value, Value)>;
413 let mut result = Vec::with_capacity(self.len());
414
415 extern "system" fn on_pair(param: LPVOID, pkey: *const VALUE, pval: *const VALUE) -> BOOL {
416 assert!(!param.is_null());
417 unsafe {
418 let result = param as *mut VecType;
419 let result = &mut *result;
420 let item = (Value::copy_from(pkey), Value::copy_from(pval));
421 result.push(item);
422 }
423 return true as BOOL;
424 }
425
426 let ptr = &mut result as *mut VecType;
427 (_API.ValueEnumElements)(self.as_cptr(), on_pair, ptr as LPVOID);
428
429 return result;
430 }
431
432 pub fn to_int(&self) -> Option<i32> {
434 let mut val = 0i32;
435 match (_API.ValueIntData)(self.as_cptr(), &mut val) {
436 VALUE_RESULT::OK => Some(val),
437 _ => None
438 }
439 }
440
441 pub fn to_bool(&self) -> Option<bool> {
443 let mut val = 0i32;
444 match (_API.ValueIntData)(self.as_cptr(), &mut val) {
445 VALUE_RESULT::OK => Some(val != 0),
446 _ => None
447 }
448 }
449
450 pub fn to_float(&self) -> Option<f64> {
452 let mut val = 0f64;
453 match (_API.ValueFloatData)(self.as_cptr(), &mut val) {
454 VALUE_RESULT::OK => Some(val),
455 _ => None
456 }
457 }
458
459 pub fn to_color(&self) -> Option<u32> {
461 let mut val = 0i32;
462 match (_API.ValueIntData)(self.as_cptr(), &mut val) {
463 VALUE_RESULT::OK => Some(val as u32),
464 _ => None
465 }
466 }
467
468 pub fn to_duration(&self) -> Option<f64> {
470 let mut val = 0f64;
471 match (_API.ValueFloatData)(self.as_cptr(), &mut val) {
472 VALUE_RESULT::OK => Some(val),
473 _ => None
474 }
475 }
476
477 pub fn to_angle(&self) -> Option<f64> {
479 let mut val = 0f64;
480 match (_API.ValueFloatData)(self.as_cptr(), &mut val) {
481 VALUE_RESULT::OK => Some(val),
482 _ => None
483 }
484 }
485
486 pub fn as_string(&self) -> Option<String> {
488 let mut s = 0 as LPCWSTR;
489 let mut n = 0_u32;
490 match (_API.ValueStringData)(self.as_cptr(), &mut s, &mut n) {
491 VALUE_RESULT::OK => Some(::utf::w2sn(s, n as usize)),
492 _ => None
493 }
494 }
495
496 pub fn into_string(mut self) -> String {
498 (_API.ValueToString)(self.as_ptr(), VALUE_STRING_CVT_TYPE::JSON_LITERAL);
499 return self.as_string().unwrap();
500 }
501
502 pub fn as_bytes(&self) -> Option<&[u8]> {
504 let mut s = 0 as LPCBYTE;
505 let mut n = 0_u32;
506 match (_API.ValueBinaryData)(self.as_cptr(), &mut s, &mut n) {
507 VALUE_RESULT::OK => Some(unsafe { ::std::slice::from_raw_parts(s, n as usize) }),
508 _ => None
509 }
510 }
511
512 pub fn to_bytes(&self) -> Option<Vec<u8>> {
514 self.as_bytes().map(ToOwned::to_owned)
515 }
516
517 pub fn call(&self, this: Option<Value>, args: &[Value], name: Option<&str>) -> Result<Value, VALUE_RESULT> {
527 let mut rv = Value::new();
528 let argv = Value::pack_args(args);
529 let name = s2w!(name.unwrap_or(""));
530 let ok = (_API.ValueInvoke)(self.as_cptr(), this.unwrap_or_default().as_ptr(),
531 argv.len() as UINT, argv.as_ptr(), rv.as_ptr(), name.as_ptr());
532 match ok {
533 VALUE_RESULT::OK => Ok(rv),
534 _ => Err(ok)
535 }
536 }
537
538 #[doc(hidden)]
539 pub fn pack_to(&self, dst: &mut VALUE) {
540 (_API.ValueCopy)(dst, self.as_cptr());
541 }
542
543 #[doc(hidden)]
544 pub fn pack_args(args: &[Value]) -> Vec<VALUE> {
545 let argc = args.len();
546 let mut argv: Vec<VALUE> = Vec::with_capacity(argc);
547 argv.resize(argc, VALUE::default());
548 for i in 0..argc {
549 args[i].pack_to(&mut argv[i]);
550 }
551 return argv;
552 }
553
554 #[doc(hidden)]
555 pub unsafe fn unpack_from(args: * const VALUE, count: UINT) -> Vec<Value> {
556 let argc = count as usize;
557 let mut argv: Vec<Value> = Vec::with_capacity(argc);
558 assert!(argc == 0 || !args.is_null());
559 let args = ::std::slice::from_raw_parts(args, argc);
560 for arg in args {
561 argv.push(Value::copy_from(arg));
562 }
563 return argv;
564 }
565
566 #[doc(hidden)]
567 unsafe fn copy_from(ptr: *const VALUE) -> Value {
568 assert!(!ptr.is_null());
569 let mut v = Value::new();
570 (_API.ValueCopy)(v.as_ptr(), ptr);
571 return v;
572 }
573
574 #[allow(clippy::mut_from_ref)]
575 fn ensure_tmp_mut(&self) -> &mut Value {
576 let cp = self as *const Value;
577 let mp = cp as *mut Value;
578 let me = unsafe { &mut *mp };
579 return me.ensure_tmp();
580 }
581
582 fn ensure_tmp(&mut self) -> &mut Value {
583 if self.tmp.is_null() {
584 let tmp = Box::new(Value::new());
585 self.tmp = Box::into_raw(tmp);
586 }
587 return unsafe { &mut *self.tmp };
588 }
589
590 pub fn is_empty(&self) -> bool {
592 self.is_undefined() || self.len() == 0
593 }
594
595 #[allow(missing_docs)]
596 pub const fn is_undefined(&self) -> bool {
597 self.data.t as u32 == VALUE_TYPE::T_UNDEFINED as u32 && self.data.u == 0
598 }
599 #[allow(missing_docs)]
600 pub const fn is_null(&self) -> bool {
601 self.data.t as u32 == VALUE_TYPE::T_NULL as u32
602 }
603 #[allow(missing_docs)]
604 pub const fn is_nothing(&self) -> bool {
605 self.data.t as u32 == VALUE_TYPE::T_UNDEFINED as u32 && self.data.u == VALUE_UNIT_UNDEFINED::UT_NOTHING as UINT
606 }
607 #[allow(missing_docs)]
608 pub const fn is_bool(&self) -> bool {
609 self.data.t as u32 == VALUE_TYPE::T_BOOL as u32
610 }
611 #[allow(missing_docs)]
612 pub const fn is_int(&self) -> bool {
613 self.data.t as u32 == VALUE_TYPE::T_INT as u32
614 }
615 #[allow(missing_docs)]
616 pub const fn is_float(&self) -> bool {
617 self.data.t as u32 == VALUE_TYPE::T_FLOAT as u32
618 }
619 #[allow(missing_docs)]
620 pub const fn is_bytes(&self) -> bool {
621 self.data.t as u32 == VALUE_TYPE::T_BYTES as u32
622 }
623 #[allow(missing_docs)]
624 pub const fn is_string(&self) -> bool {
625 self.data.t as u32 == VALUE_TYPE::T_STRING as u32
626 }
627 #[allow(missing_docs)]
628 pub const fn is_symbol(&self) -> bool {
629 self.data.t as u32 == VALUE_TYPE::T_STRING as u32 && self.data.u == VALUE_UNIT_TYPE_STRING::SYMBOL as UINT
630 }
631 #[allow(missing_docs)]
632 pub const fn is_error_string(&self) -> bool {
633 self.data.t as u32 == VALUE_TYPE::T_STRING as u32 && self.data.u == VALUE_UNIT_TYPE_STRING::ERROR as UINT
634 }
635 #[allow(missing_docs)]
636 pub const fn is_date(&self) -> bool {
637 self.data.t as u32 == VALUE_TYPE::T_DATE as u32
638 }
639 #[allow(missing_docs)]
640 pub const fn is_currency(&self) -> bool {
641 self.data.t as u32 == VALUE_TYPE::T_CURRENCY as u32
642 }
643 #[allow(missing_docs)]
644 pub const fn is_color(&self) -> bool {
645 self.data.t as u32 == VALUE_TYPE::T_COLOR as u32
646 }
647 #[allow(missing_docs)]
648 pub const fn is_duration(&self) -> bool {
649 self.data.t as u32 == VALUE_TYPE::T_DURATION as u32
650 }
651 #[allow(missing_docs)]
652 pub const fn is_angle(&self) -> bool {
653 self.data.t as u32 == VALUE_TYPE::T_ANGLE as u32
654 }
655 #[allow(missing_docs)]
656 pub const fn is_map(&self) -> bool {
657 self.data.t as u32 == VALUE_TYPE::T_MAP as u32
658 }
659 #[allow(missing_docs)]
660 pub const fn is_array(&self) -> bool {
661 self.data.t as u32 == VALUE_TYPE::T_ARRAY as u32
662 }
663 #[allow(missing_docs)]
664 pub const fn is_function(&self) -> bool {
665 self.data.t as u32 == VALUE_TYPE::T_FUNCTION as u32
666 }
667 #[allow(missing_docs)]
668 pub fn is_native_function(&self) -> bool {
669 (_API.ValueIsNativeFunctor)(self.as_cptr()) != 0
670 }
671 #[allow(missing_docs)]
672 pub const fn is_object(&self) -> bool {
673 self.data.t as u32 == VALUE_TYPE::T_OBJECT as u32
674 }
675 #[allow(missing_docs)]
676 pub const fn is_asset(&self) -> bool {
677 self.data.t as u32 == VALUE_TYPE::T_ASSET as u32
678 }
679
680 pub const fn is_primitive(&self) -> bool {
682 use capi::scvalue::VALUE_TYPE::*;
683 matches!(self.data.t,
684 T_UNDEFINED
685 | T_NULL
686 | T_BOOL
687 | T_INT
688 | T_FLOAT
689 | T_DATE
690 )
691 }
692
693 #[allow(missing_docs)]
695 pub const fn is_object_array(&self) -> bool {
696 self.data.t as u32 == VALUE_TYPE::T_OBJECT as u32 && self.data.u == VALUE_UNIT_TYPE_OBJECT::ARRAY as UINT
697 }
698 #[allow(missing_docs)]
699 pub const fn is_object_map(&self) -> bool {
700 self.data.t as u32 == VALUE_TYPE::T_OBJECT as u32 && self.data.u == VALUE_UNIT_TYPE_OBJECT::OBJECT as UINT
701 }
702 #[allow(missing_docs)]
703 pub const fn is_object_class(&self) -> bool {
704 self.data.t as u32 == VALUE_TYPE::T_OBJECT as u32 && self.data.u == VALUE_UNIT_TYPE_OBJECT::OBJECT as UINT
705 }
706 #[allow(missing_docs)]
707 pub const fn is_object_native(&self) -> bool {
708 self.data.t as u32 == VALUE_TYPE::T_OBJECT as u32 && self.data.u == VALUE_UNIT_TYPE_OBJECT::NATIVE as UINT
709 }
710 #[allow(missing_docs)]
711 pub const fn is_object_function(&self) -> bool {
712 self.data.t as u32 == VALUE_TYPE::T_OBJECT as u32 && self.data.u == VALUE_UNIT_TYPE_OBJECT::FUNCTION as UINT
713 }
714 #[allow(missing_docs)]
715 pub const fn is_object_error(&self) -> bool {
716 self.data.t as u32 == VALUE_TYPE::T_OBJECT as u32 && self.data.u == VALUE_UNIT_TYPE_OBJECT::ERROR as UINT
717 }
718 #[allow(missing_docs)]
719 pub const fn is_dom_element(&self) -> bool {
720 self.data.t as u32 == VALUE_TYPE::T_DOM_OBJECT as u32
721 }
722
723 #[allow(missing_docs)]
725 pub const fn is_varray(&self) -> bool {
726 self.is_array() || self.is_object()
727 }
728 #[allow(missing_docs)]
729 pub const fn is_vmap(&self) -> bool {
730 self.is_map() || self.is_object_map()
731 }
732 #[allow(missing_docs)]
733 pub fn is_vfunction(&self) -> bool {
734 self.is_function() || self.is_object_function() || self.is_native_function()
735 }
736 #[allow(missing_docs)]
737 pub const fn is_verror(&self) -> bool {
738 self.is_error_string() || self.is_object_error()
739 }
740
741 fn assign_str(&mut self, val: &str, unit: VALUE_UNIT_TYPE_STRING) -> VALUE_RESULT {
742 let (s,n) = s2wn!(val);
743 return (_API.ValueStringDataSet)(self.as_ptr(), s.as_ptr(), n, unit as UINT);
744 }
745}
746
747
748impl ::std::fmt::Display for Value {
750 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
751 let copy = self.clone();
752 let re = copy.into_string();
753 f.write_str(&re)
754 }
755}
756
757impl ::std::fmt::Debug for Value {
759 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
760
761 let mut tname = format!("{:?}", self.data.t);
762
763 if self.is_undefined() || self.is_null() {
764 return f.write_str(&tname[2..].to_lowercase());
765
766 } else if self.is_nothing() {
767 return f.write_str("nothing");
768
769 } else if self.is_string() && self.data.u != 0 {
770 let units = [
772 ("file", 0xfffe), ("symbol", 0xffff),
773 ("string", 0), ("error", 1), ("secure", 2),
774 ("url", 3), ("selector", 4),
775 ];
776
777 tname.push(':');
778 if let Some(name) = units.iter().find(|&&x| x.1 == self.data.u) {
779 tname.push_str(name.0);
780 } else {
781 tname.push_str(&self.data.u.to_string());
782 }
783
784 } else if self.is_object() {
785 let units = ["array", "object", "class", "native", "function", "error"];
787 let u = self.data.u as usize;
788 tname.push(':');
789 if u < units.len() {
790 tname.push_str(units[u]);
791 } else {
792 tname.push_str(&u.to_string());
793 }
794
795 } else if self.data.u != 0 {
796 }
802 f.write_str(&tname[2..].to_lowercase())?;
803 f.write_str(":")?;
804 write!(f, "{}", &self)
805 }
806}
807
808impl Drop for Value {
810 fn drop(&mut self) {
811 if !self.tmp.is_null() {
813 let _drop_tmp = unsafe { Box::from_raw(self.tmp) };
814 }
815 if std::thread::panicking() {
816 if self.is_primitive() {
818 return;
819 }
820 }
822 (_API.ValueClear)(self.as_ptr());
823 }
824}
825
826impl Default for Value {
828 fn default() -> Self {
829 return Value::new();
830 }
831}
832
833impl Clone for Value {
837 fn clone(&self) -> Self {
838 let mut dst = Value::new();
839 (_API.ValueCopy)(dst.as_ptr(), self.as_cptr());
840 return dst;
841 }
842}
843
844impl ::std::cmp::PartialEq for Value {
846 fn eq(&self, other: &Self) -> bool {
847 let eq = (_API.ValueCompare)(self.as_cptr(), other.as_cptr());
848 matches!(eq, VALUE_RESULT::OK_TRUE)
849 }
850}
851
852impl ::std::ops::Index<usize> for Value {
854 type Output = Value;
855 fn index(&self, index: usize) -> &Self::Output {
856 let tmp = self.ensure_tmp_mut();
857 (_API.ValueNthElementValue)(self.as_cptr(), index as INT, tmp.as_ptr());
858 return tmp;
859 }
860}
861
862#[cfg(notworking)]
864impl ::std::ops::IndexMut<usize> for Value {
865 fn index_mut(&mut self, index: usize) -> &mut Value {
866 let tmp = self.ensure_tmp_mut();
867 (_API.ValueNthElementValue)(self.as_cptr(), index as INT, tmp.as_ptr());
868 return tmp;
869 }
870}
871
872impl ::std::ops::Index<Value> for Value {
874 type Output = Value;
875 fn index(&self, key: Value) -> &Self::Output {
876 let tmp = self.ensure_tmp_mut();
877 (_API.ValueGetValueOfKey)(self.as_cptr(), key.as_cptr(), tmp.as_ptr());
878 return tmp;
879 }
880}
881
882impl ::std::ops::Index<&'static str> for Value {
884 type Output = Value;
885 fn index(&self, key: &'static str) -> &Self::Output {
886 let tmp = self.ensure_tmp_mut();
887 (_API.ValueGetValueOfKey)(self.as_cptr(), Value::from(key).as_cptr(), tmp.as_ptr());
888 return tmp;
889 }
890}
891
892#[cfg(notworking)]
894impl ::std::ops::IndexMut<Value> for Value {
895 fn index_mut<'a>(&'a mut self, key: Value) -> &'a mut Value {
896 let ptr = self.as_ptr();
897 let tmp = self.ensure_tmp();
898 (_API.ValueSetValueToKey)(ptr, key.as_cptr(), tmp.as_ptr());
899 return tmp;
900 }
901}
902
903impl From<()> for Value {
907 fn from(_: ()) -> Self {
908 Value::new()
909 }
910}
911
912impl<'a> From<&'a VALUE> for Value {
914 fn from(val: &'a VALUE) -> Self {
915 unsafe { Value::copy_from(val as *const _) }
916 }
917}
918
919impl From<i32> for Value {
921 fn from(val: i32) -> Self {
924 let mut me = Value::new();
925 (_API.ValueIntDataSet)(me.as_ptr(), val, VALUE_TYPE::T_INT as UINT, 0);
926 return me;
927 }
928}
929
930impl From<&i32> for Value {
932 fn from(val: &i32) -> Self {
935 let mut me = Value::new();
936 (_API.ValueIntDataSet)(me.as_ptr(), *val, VALUE_TYPE::T_INT as UINT, 0);
937 return me;
938 }
939}
940
941impl From<f64> for Value {
943 fn from(val: f64) -> Self {
944 let mut me = Value::new();
945 (_API.ValueFloatDataSet)(me.as_ptr(), val, VALUE_TYPE::T_FLOAT as UINT, 0);
946 return me;
947 }
948}
949
950impl From<&f64> for Value {
952 fn from(val: &f64) -> Self {
953 let mut me = Value::new();
954 (_API.ValueFloatDataSet)(me.as_ptr(), *val, VALUE_TYPE::T_FLOAT as UINT, 0);
955 return me;
956 }
957}
958
959impl From<bool> for Value {
961 fn from(val: bool) -> Self {
962 let mut me = Value::new();
963 (_API.ValueIntDataSet)(me.as_ptr(), val as INT, VALUE_TYPE::T_BOOL as UINT, 0);
964 return me;
965 }
966}
967
968impl From<&bool> for Value {
970 fn from(val: &bool) -> Self {
971 let mut me = Value::new();
972 (_API.ValueIntDataSet)(me.as_ptr(), *val as INT, VALUE_TYPE::T_BOOL as UINT, 0);
973 return me;
974 }
975}
976
977impl<'a> From<&'a str> for Value {
979 fn from(val: &'a str) -> Self {
980 let mut me = Value::new();
981 me.assign_str(val, VALUE_UNIT_TYPE_STRING::STRING);
982 return me;
983 }
984}
985
986impl From<String> for Value {
988 fn from(val: String) -> Self {
989 let mut me = Value::new();
990 me.assign_str(&val, VALUE_UNIT_TYPE_STRING::STRING);
991 return me;
992 }
993}
994
995impl<'a> From<&'a String> for Value {
996 fn from(val: &'a String) -> Self {
997 let mut me = Value::new();
998 me.assign_str(val, VALUE_UNIT_TYPE_STRING::STRING);
999 return me;
1000 }
1001}
1002
1003impl ::std::str::FromStr for Value {
1005 type Err = VALUE_RESULT;
1006 fn from_str(val: &str) -> Result<Self, Self::Err> {
1007 Value::parse(val).or(Err(VALUE_RESULT::BAD_PARAMETER))
1008 }
1009}
1010
1011impl<'a> From<&'a [u8]> for Value {
1013 fn from(val: &'a [u8]) -> Self {
1014 let mut me = Value::new();
1015 (_API.ValueBinaryDataSet)(me.as_ptr(), val.as_ptr(), val.len() as UINT, VALUE_TYPE::T_BYTES as UINT, 0);
1016 return me;
1017 }
1018}
1019
1020impl From<std::time::SystemTime> for Value {
1022 fn from(val: std::time::SystemTime) -> Self {
1024 let mut me = Value::new();
1025 if let Ok(epoch) = val.duration_since(std::time::UNIX_EPOCH) {
1026 let ns = epoch.as_nanos() / 100;
1028
1029 (_API.ValueInt64DataSet)(me.as_ptr(), ns as i64, VALUE_TYPE::T_DATE as u32, 0);
1032 }
1033 me
1034 }
1035}
1036
1037impl<T, E> From<Result<T, E>> for Value where T: Into<Value>, E: std::fmt::Display {
1039 fn from(val: Result<T, E>) -> Self {
1040 match val {
1041 Ok(v) => v.into(),
1042 Err(e) => Value::error(&e.to_string()),
1043 }
1044 }
1045}
1046
1047impl ::std::iter::FromIterator<Value> for Value {
1062 fn from_iter<I: IntoIterator<Item=Value>>(iterator: I) -> Self {
1063 let iterator = iterator.into_iter();
1064 let capacity = iterator.size_hint().0;
1065 let mut v = Value::array(capacity);
1066 for (i, m) in iterator.enumerate() {
1067 v.set(i, m);
1068 }
1069 return v;
1070 }
1071}
1072
1073impl ::std::iter::FromIterator<i32> for Value {
1075 fn from_iter<I: IntoIterator<Item=i32>>(iterator: I) -> Self {
1076 let iterator = iterator.into_iter();
1077 let capacity = iterator.size_hint().0;
1078 let mut v = Value::array(capacity);
1079 for (i, m) in iterator.enumerate() {
1080 v.set(i, Value::from(m));
1081 }
1082 return v;
1083 }
1084}
1085
1086impl ::std::iter::FromIterator<f64> for Value {
1088 fn from_iter<I: IntoIterator<Item=f64>>(iterator: I) -> Self {
1089 let iterator = iterator.into_iter();
1090 let capacity = iterator.size_hint().0;
1091 let mut v = Value::array(capacity);
1092 for (i, m) in iterator.enumerate() {
1093 v.set(i, Value::from(m));
1094 }
1095 return v;
1096 }
1097}
1098
1099impl<'a> ::std::iter::FromIterator<&'a str> for Value {
1101 fn from_iter<I: IntoIterator<Item=&'a str>>(iterator: I) -> Self {
1102 let iterator = iterator.into_iter();
1103 let capacity = iterator.size_hint().0;
1104 let mut v = Value::array(capacity);
1105 for (i, m) in iterator.enumerate() {
1106 v.set(i, Value::from(m));
1107 }
1108 return v;
1109 }
1110}
1111
1112impl ::std::iter::FromIterator<String> for Value {
1114 fn from_iter<I: IntoIterator<Item=String>>(iterator: I) -> Self {
1115 let iterator = iterator.into_iter();
1116 let capacity = iterator.size_hint().0;
1117 let mut v = Value::array(capacity);
1118 for (i, m) in iterator.enumerate() {
1119 v.set(i, Value::from(m));
1120 }
1121 return v;
1122 }
1123}
1124
1125impl<F, R> From<F> for Value
1127where
1128 F: Fn(&[Value]) -> R,
1129 R: Into<Value>,
1130{
1131 fn from(f: F) -> Value {
1132 let mut v = Value::new();
1133 let boxed = Box::new(f);
1134 let ptr = Box::into_raw(boxed); (_API.ValueNativeFunctorSet)(v.as_ptr(), _functor_invoke::<F,R>, _functor_release::<F>, ptr as LPVOID);
1136 return v;
1137 }
1138}
1139
1140impl<T> From<Box<IAsset<T>>> for Value {
1142 fn from(ptr: Box<IAsset<T>>) -> Value {
1143 let ptr = Box::into_raw(ptr);
1144 let mut me = Value::new();
1145 (_API.ValueInt64DataSet)(me.as_ptr(), ptr as usize as i64, VALUE_TYPE::T_ASSET as u32, 0);
1146 return me;
1147 }
1148}
1149
1150extern "C" fn _functor_release<F>(tag: LPVOID)
1151{
1152 let ptr = tag as *mut F;
1154 let boxed = unsafe { Box::from_raw(ptr) };
1155 drop(boxed);
1157}
1158
1159extern "C" fn _functor_invoke<F, R>(tag: LPVOID, argc: UINT, argv: *const VALUE, retval: *mut VALUE)
1160where
1161 F: Fn(&[Value]) -> R,
1162 R: Into<Value>,
1163{
1164 let ptr = tag as *mut F;
1166 let me = unsafe { &mut *ptr };
1167 let retval = unsafe { &mut *retval };
1168 let args = unsafe { Value::unpack_from(argv, argc) };
1169 let rv = me(&args);
1170 let rv: Value = rv.into();
1171 rv.pack_to(retval)
1172}
1173
1174
1175pub trait FromValue {
1177 fn from_value(v: &Value) -> Option<Self> where Self: Sized;
1179}
1180
1181impl FromValue for Value {
1182 fn from_value(v: &Value) -> Option<Self> {
1183 Some(v.clone())
1184 }
1185}
1186
1187impl FromValue for bool {
1188 fn from_value(v: &Value) -> Option<Self> {
1189 v.to_bool()
1190 }
1191}
1192
1193impl FromValue for i32 {
1194 fn from_value(v: &Value) -> Option<Self> {
1195 v.to_int()
1196 }
1197}
1198
1199impl FromValue for f64 {
1200 fn from_value(v: &Value) -> Option<Self> {
1201 v.to_float()
1202 }
1203}
1204
1205impl FromValue for Vec<u8> {
1206 fn from_value(v: &Value) -> Option<Self> {
1207 v.to_bytes()
1208 }
1209}
1210
1211impl FromValue for String {
1212 fn from_value(v: &Value) -> Option<Self> {
1213 v.as_string()
1214 }
1215}
1216
1217
1218#[doc(hidden)]
1220pub struct KeyIterator<'a> {
1221 base: &'a Value,
1222 index: usize,
1223 count: usize,
1224}
1225
1226impl<'a> ::std::iter::Iterator for KeyIterator<'a> {
1227 type Item = Value;
1228
1229 fn next(&mut self) -> Option<Self::Item> {
1230 if self.index < self.count {
1231 self.index += 1;
1232 Some(self.base.key_at(self.index - 1))
1233 } else {
1234 None
1235 }
1236 }
1237
1238 fn size_hint(&self) -> (usize, Option<usize>) {
1239 let remain = self.count - self.index;
1240 (remain, Some(remain))
1241 }
1242
1243 fn count(self) -> usize {
1244 self.count
1245 }
1246}
1247
1248impl<'a> ::std::iter::DoubleEndedIterator for KeyIterator<'a> {
1250 fn next_back(&mut self) -> Option<Self::Item> {
1251 if self.index == self.count || self.count == 0 {
1252 None
1253 } else {
1254 self.count -= 1;
1255 Some(self.base.key_at(self.count))
1256 }
1257 }
1258}
1259
1260
1261#[doc(hidden)]
1263pub struct SeqIterator<'a> {
1264 base: &'a Value,
1265 index: usize,
1266 count: usize,
1267}
1268
1269impl<'a> ::std::iter::Iterator for SeqIterator<'a> {
1270 type Item = Value;
1271
1272 fn next(&mut self) -> Option<Self::Item> {
1273 if self.index < self.count {
1274 self.index += 1;
1275 Some(self.base.get(self.index - 1))
1276 } else {
1277 None
1278 }
1279 }
1280
1281 fn size_hint(&self) -> (usize, Option<usize>) {
1282 let remain = self.count - self.index;
1283 (remain, Some(remain))
1284 }
1285
1286 fn count(self) -> usize {
1287 self.count
1288 }
1289}
1290
1291impl<'a> ::std::iter::DoubleEndedIterator for SeqIterator<'a> {
1293 fn next_back(&mut self) -> Option<Self::Item> {
1294 if self.index == self.count || self.count == 0 {
1295 None
1296 } else {
1297 self.count -= 1;
1298 Some(self.base.get(self.count))
1299 }
1300 }
1301}
1302
1303impl<'a> ::std::iter::IntoIterator for &'a Value {
1307 type Item = Value;
1308 type IntoIter = SeqIterator<'a>;
1309
1310 fn into_iter(self) -> Self::IntoIter {
1311 self.values()
1312 }
1313}
1314
1315
1316#[cfg(test)]
1317mod tests {
1318 #![allow(unused_imports)]
1319
1320 use super::{Value, FromValue};
1321 use capi::scvalue::{VALUE, VALUE_TYPE};
1322 use std::mem;
1323 use ::{_API};
1324
1325 fn check1(a: i32) {
1326 assert_eq!(a, 12);
1327 }
1328
1329 #[test]
1330 fn test_from_value() {
1331 let v = Value::from(12);
1332 check1(
1333 match FromValue::from_value(&v) {
1334 Some(x) => { x },
1335 None => { return; }
1336 }
1337 );
1338 }
1339
1340 #[test]
1341 fn test_value_layout() {
1342 assert_eq!(mem::size_of::<VALUE_TYPE>(), 4);
1343 assert_eq!(mem::size_of::<VALUE>(), 16);
1344 }
1345
1346 #[test]
1347 fn test_abi() {
1348
1349 let mut data = VALUE { t: VALUE_TYPE::T_UNDEFINED, u: 0, d: 0 };
1350 assert_eq!(data.t, VALUE_TYPE::T_UNDEFINED);
1351
1352 (_API.ValueInit)(&mut data);
1353 assert_eq!(data.t, VALUE_TYPE::T_UNDEFINED);
1354
1355 (_API.ValueClear)(&mut data);
1356 assert_eq!(data.t, VALUE_TYPE::T_UNDEFINED);
1357 }
1358}