easy_qjs/
lib.rs

1#![warn(
2  clippy::all,
3  clippy::dbg_macro,
4  clippy::todo,
5  clippy::empty_enum,
6  clippy::enum_glob_use,
7  clippy::mem_forget,
8  clippy::unused_self,
9  clippy::filter_map_next,
10  clippy::needless_continue,
11  clippy::needless_borrow,
12  clippy::match_wildcard_for_single_variants,
13  clippy::if_let_mutex,
14  clippy::mismatched_target_os,
15  clippy::await_holding_lock,
16  clippy::match_on_vec_items,
17  clippy::imprecise_flops,
18  clippy::suboptimal_flops,
19  clippy::lossy_float_literal,
20  clippy::rest_pat_in_fully_bound_structs,
21  clippy::fn_params_excessive_bools,
22  clippy::exit,
23  clippy::inefficient_to_string,
24  clippy::linkedlist,
25  clippy::macro_use_imports,
26  clippy::option_option,
27  clippy::verbose_file_reads,
28  clippy::unnested_or_patterns,
29  clippy::str_to_string,
30  rust_2018_idioms,
31  future_incompatible,
32  nonstandard_style,
33  missing_debug_implementations,
34  clippy::unwrap_used,
35  // missing_docs
36)]
37#![deny(unreachable_pub, private_in_public)]
38#![allow(elided_lifetimes_in_paths, clippy::type_complexity)]
39#![forbid(unsafe_code)]
40#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
41#![cfg_attr(test, allow(clippy::float_cmp))]
42
43mod builtins;
44mod cancellation;
45mod engine;
46pub(crate) mod error;
47
48mod value;
49
50use std::{
51    sync::{atomic::AtomicBool, Arc},
52    time::Instant,
53};
54
55use async_trait::async_trait;
56use serde::{Deserialize, Serialize};
57
58pub use error::Error;
59// re-exports
60pub use js;
61
62#[async_trait]
63pub trait Processor: Send + Sync + 'static {
64    async fn call(&self, args: JsonValue) -> Result<JsonValue, String>;
65}
66
67#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
68pub struct JsonValue(serde_json::Value);
69
70pub struct JsEngine {
71    #[allow(dead_code)]
72    runtime: js::Runtime,
73    pub context: js::Context,
74    #[cfg(feature = "dispatcher")]
75    sender: flume::Sender<MsgChannel>,
76}
77
78#[derive(Debug, Clone, Default)]
79pub struct Cancellation {
80    deadline: Option<Instant>,
81    cancellation: Arc<AtomicBool>,
82}
83
84#[cfg(feature = "dispatcher")]
85pub use builtins::dispatcher::MsgChannel;
86
87#[cfg(feature = "dispatcher")]
88#[async_trait]
89impl<F> Processor for F
90where
91    F: Fn(JsonValue) -> Result<JsonValue, String> + Send + Sync + 'static,
92{
93    async fn call(&self, args: JsonValue) -> Result<JsonValue, String> {
94        (self)(args)
95    }
96}