neon_serde3/
lib.rs

1#![allow(unknown_lints)]
2#![deny(unused_variables)]
3#![deny(unused_mut)]
4#![deny(clippy)]
5#![deny(clippy::pedantic)]
6#![allow(stutter)]
7#![recursion_limit = "128"]
8
9//!
10//! Neon-serde
11//! ==========
12//!
13//! This crate is a utility to easily convert values between
14//!
15//! A `Handle<JsValue>` from the `neon` crate
16//! and any value implementing `serde::{Serialize, Deserialize}`
17//!
18//! ## Usage
19//!
20//! #### `neon_serde::from_value`
21//! Convert a `Handle<js::JsValue>` to
22//! a type implementing `serde::Deserialize`
23//!
24//! #### `neon_serde::to_value`
25//! Convert a value implementing `serde::Serialize` to
26//! a `Handle<JsValue>`
27//!
28//!
29//! ## Example
30//!
31//! ```rust,no_run
32//! # #![allow(dead_code)]
33//! use serde::{Serialize, Deserialize};
34//! use neon_serde2 as neon_serde;
35//!
36//! use neon::prelude::*;
37//!
38//! #[derive(Serialize, Debug, Deserialize)]
39//! struct AnObject {
40//!     a: u32,
41//!     b: Vec<f64>,
42//!     c: String,
43//! }
44//!
45//! fn deserialize_something(mut cx: FunctionContext) -> JsResult<JsValue> {
46//!     let arg0 = cx.argument::<JsValue>(0)?;
47//!
48//!     let arg0_value :AnObject = neon_serde::from_value(&mut cx, arg0)
49//!         .or_else(|e| cx.throw_error(e.to_string()))
50//!         .unwrap();
51//!     println!("{:?}", arg0_value);
52//!
53//!     Ok(JsUndefined::new(&mut cx).upcast())
54//! }
55//!
56//! fn serialize_something(mut cx: FunctionContext) -> JsResult<JsValue> {
57//!     let value = AnObject {
58//!         a: 1,
59//!         b: vec![2f64, 3f64, 4f64],
60//!         c: "a string".into()
61//!     };
62//!
63//!     let js_value = neon_serde::to_value(&mut cx, &value)
64//!         .or_else(|e| cx.throw_error(e.to_string()))
65//!         .unwrap();
66//!     Ok(js_value)
67//! }
68//!
69//! # fn main () {
70//! # }
71//!
72//! ```
73//!
74
75pub mod de;
76pub mod errors;
77pub mod ser;
78
79mod macros;
80
81pub use de::from_value;
82pub use de::from_value_opt;
83pub use ser::to_value;
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88    use neon::prelude::*;
89
90    #[test]
91    fn test_it_compiles() {
92        fn check<'j>(mut cx: FunctionContext<'j>) -> JsResult<'j, JsValue> {
93            let result: () = {
94                let arg: Handle<'j, JsValue> = cx.argument::<JsValue>(0)?;
95                let () = from_value(&mut cx, arg)
96                    .or_else(|e| cx.throw_error(e.to_string()))
97                    .unwrap();
98                ()
99            };
100            let result: Handle<'j, JsValue> = to_value(&mut cx, &result)
101                .or_else(|e| cx.throw_error(e.to_string()))
102                .unwrap();
103            Ok(result)
104        }
105
106        let _ = check;
107    }
108
109    #[test]
110    fn test_it_compiles_2() {
111        fn check<'j>(mut cx: FunctionContext<'j>) -> JsResult<'j, JsValue> {
112            let result: () = {
113                let arg: Option<Handle<'j, JsValue>> = cx.argument_opt(0);
114                let () = from_value_opt(&mut cx, arg)
115                    .or_else(|e| cx.throw_error(e.to_string()))
116                    .unwrap();
117            };
118            let result: Handle<'j, JsValue> = to_value(&mut cx, &result)
119                .or_else(|e| cx.throw_error(e.to_string()))
120                .unwrap();
121            Ok(result)
122        }
123
124        let _ = check;
125    }
126}