[oden] Grab my incomplete QuickJS wrapper
This commit is contained in:
parent
aa70df41a3
commit
898b1fe129
114 changed files with 244181 additions and 0 deletions
107
oden-js/src/conversion/from.rs
Normal file
107
oden-js/src/conversion/from.rs
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
use crate::{ContextRef, Error, Result, ValueRef};
|
||||
use std::num::TryFromIntError;
|
||||
|
||||
pub trait TryFromValue: Sized {
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self>;
|
||||
}
|
||||
|
||||
impl TryFromValue for u8 {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
let v = value.to_u32(&ctx)?;
|
||||
v.try_into()
|
||||
.map_err(|e: TryFromIntError| Error::ConversionError(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromValue for u16 {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
let v = value.to_u32(&ctx)?;
|
||||
v.try_into()
|
||||
.map_err(|e: TryFromIntError| Error::ConversionError(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromValue for u32 {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
value.to_u32(&ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// impl<'c,'d> TryFrom<&'c ValueRef<'d>> for u64 {
|
||||
// #[inline]
|
||||
// fn try_from_value<'r>(value: &ValueRef<'r>, ctx:&ContextRef<'r>) -> Result<'r, Self> {
|
||||
// value.to_u64()
|
||||
// }
|
||||
// }
|
||||
|
||||
impl TryFromValue for i8 {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
let v = value.to_i32(&ctx)?;
|
||||
v.try_into()
|
||||
.map_err(|e: TryFromIntError| Error::ConversionError(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromValue for i16 {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
let v = value.to_i32(&ctx)?;
|
||||
v.try_into()
|
||||
.map_err(|e: TryFromIntError| Error::ConversionError(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromValue for i32 {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
value.to_i32(&ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromValue for i64 {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
value.to_i64(&ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromValue for f32 {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
let v = value.to_float64(&ctx)?;
|
||||
Ok(v as f32)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromValue for f64 {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
value.to_float64(&ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromValue for bool {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, _ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
value.to_bool()
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFromValue for String {
|
||||
#[inline]
|
||||
fn try_from_value<'r>(value: &ValueRef<'r>, ctx: &ContextRef<'r>) -> Result<'r, Self> {
|
||||
value.to_string(&ctx)
|
||||
}
|
||||
}
|
||||
|
||||
// impl<'c, T: Class> TryFrom<&'c ValueRef<'_>> for T {
|
||||
//
|
||||
// #[inline]
|
||||
// fn try_from_value<'r>(value: &ValueRef<'r>, ctx:&ContextRef<'r>) -> Result<'r, Self> {
|
||||
// T::from_value(value)
|
||||
// }
|
||||
// }
|
||||
Loading…
Add table
Add a link
Reference in a new issue