[oden-js] Convert Option<>

This commit is contained in:
John Doty 2023-06-23 05:48:26 -07:00
parent af12dccd5d
commit c7903382a0
2 changed files with 21 additions and 0 deletions

View file

@ -104,3 +104,14 @@ impl TryFromValue for Value {
Ok(value.dup(ctx))
}
}
impl<T: TryFromValue> TryFromValue for Option<T> {
#[inline]
fn try_from_value(value: &ValueRef, ctx: &ContextRef) -> Result<Self> {
if value.is_undefined() {
Ok(None)
} else {
Ok(Some(T::try_from_value(value, ctx)?))
}
}
}

View file

@ -128,3 +128,13 @@ impl<T: Class> TryIntoValue for T {
self.into_value(ctx)
}
}
impl<T: TryIntoValue> TryIntoValue for Option<T> {
#[inline]
fn try_into_value(self, ctx: &ContextRef) -> ValueResult {
match self {
None => Ok(ctx.undefined()),
Some(v) => v.try_into_value(ctx),
}
}
}