2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-10-19 18:00:45 -04:00
|
|
|
|
|
|
|
// TODO: maybe add a Payload type that holds scope & v8::Value
|
|
|
|
// so it can implement Deserialize by itself
|
|
|
|
|
|
|
|
// Classifies v8::Values into sub-types
|
2021-10-20 09:40:20 -04:00
|
|
|
#[derive(Debug)]
|
2021-10-19 18:00:45 -04:00
|
|
|
pub enum ValueType {
|
|
|
|
Null,
|
|
|
|
Bool,
|
|
|
|
Number,
|
2023-03-22 00:13:24 -04:00
|
|
|
BigInt,
|
2021-10-19 18:00:45 -04:00
|
|
|
String,
|
|
|
|
Array,
|
2022-01-20 09:11:09 -05:00
|
|
|
ArrayBuffer,
|
2021-10-20 09:40:20 -04:00
|
|
|
ArrayBufferView,
|
2021-10-19 18:00:45 -04:00
|
|
|
Object,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ValueType {
|
|
|
|
pub fn from_v8(v: v8::Local<v8::Value>) -> ValueType {
|
|
|
|
if v.is_boolean() {
|
|
|
|
return Self::Bool;
|
|
|
|
} else if v.is_number() {
|
|
|
|
return Self::Number;
|
|
|
|
} else if v.is_string() {
|
|
|
|
return Self::String;
|
|
|
|
} else if v.is_array() {
|
|
|
|
return Self::Array;
|
2023-03-22 00:13:24 -04:00
|
|
|
} else if v.is_big_int() {
|
|
|
|
return Self::BigInt;
|
2022-01-20 09:11:09 -05:00
|
|
|
} else if v.is_array_buffer() {
|
|
|
|
return Self::ArrayBuffer;
|
2021-10-20 09:40:20 -04:00
|
|
|
} else if v.is_array_buffer_view() {
|
|
|
|
return Self::ArrayBufferView;
|
2021-10-19 18:00:45 -04:00
|
|
|
} else if v.is_object() {
|
|
|
|
return Self::Object;
|
|
|
|
} else if v.is_null_or_undefined() {
|
|
|
|
return Self::Null;
|
|
|
|
}
|
|
|
|
panic!("serde_v8: unknown ValueType for v8::Value")
|
|
|
|
}
|
|
|
|
}
|