// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. use deno_core::v8; use deno_core::FromV8; use deno_core::ToV8; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] /// A wrapper type for `Option` that (de)serializes `None` as `null` #[repr(transparent)] pub struct OptionNull(pub Option); impl From> for OptionNull { fn from(option: Option) -> Self { Self(option) } } impl From> for Option { fn from(value: OptionNull) -> Self { value.0 } } impl<'a, T> ToV8<'a> for OptionNull where T: ToV8<'a>, { type Error = T::Error; fn to_v8( self, scope: &mut v8::HandleScope<'a>, ) -> Result, Self::Error> { match self.0 { Some(value) => value.to_v8(scope), None => Ok(v8::null(scope).into()), } } } impl<'a, T> FromV8<'a> for OptionNull where T: FromV8<'a>, { type Error = T::Error; fn from_v8( scope: &mut v8::HandleScope<'a>, value: v8::Local<'a, v8::Value>, ) -> Result { if value.is_null() { Ok(OptionNull(None)) } else { T::from_v8(scope, value).map(|v| OptionNull(Some(v))) } } }