-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose query api to typescript (#761)
- Loading branch information
1 parent
2069126
commit 8a93ce8
Showing
26 changed files
with
471 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// NAPI-exposed functions have to accept owned values | ||
#![allow(clippy::needless_pass_by_value)] | ||
// The functions are meant to be definitions for export, so they're not really used | ||
#![allow(clippy::return_self_not_must_use)] | ||
|
||
use napi::{Env, JsObject}; | ||
use napi_derive::napi; | ||
|
||
use crate::napi_interface::cursor::Cursor; | ||
use crate::napi_interface::{RustQuery, RustQueryResult, RustQueryResultIterator}; | ||
|
||
#[napi(namespace = "query")] | ||
pub struct Query(RustQuery); | ||
|
||
impl From<RustQuery> for Query { | ||
fn from(value: RustQuery) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
#[napi(namespace = "query")] | ||
impl Query { | ||
#[napi(factory, catch_unwind)] | ||
pub fn parse(text: String) -> napi::Result<Query> { | ||
RustQuery::parse(text.as_str()).map_or_else( | ||
|err| Err(napi::Error::from_reason(err)), | ||
|query| Ok(query.into()), | ||
) | ||
} | ||
} | ||
|
||
#[napi(namespace = "query")] | ||
pub struct QueryResultIterator(RustQueryResultIterator); | ||
|
||
#[napi(object, namespace = "query")] | ||
pub struct QueryResult { | ||
pub query_number: u32, | ||
#[napi(ts_type = "{ [key: string]: cursor.Cursor[] }")] | ||
pub bindings: JsObject, | ||
} | ||
|
||
impl QueryResult { | ||
fn new(env: Env, result: RustQueryResult) -> napi::Result<Self> { | ||
#[allow(clippy::cast_possible_truncation)] | ||
let query_number = result.query_number as u32; | ||
let mut bindings = env.create_object()?; | ||
// transer all of the bindings eagerly on the assumption | ||
// that they've all been explicitly requested. | ||
for (key, value) in result.bindings { | ||
bindings.set_named_property( | ||
&key, | ||
value.into_iter().map(|x| x.into()).collect::<Vec<Cursor>>(), | ||
)?; | ||
} | ||
Ok(Self { | ||
query_number, | ||
bindings, | ||
}) | ||
} | ||
} | ||
|
||
impl From<RustQueryResultIterator> for QueryResultIterator { | ||
fn from(value: RustQueryResultIterator) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
#[napi(namespace = "query")] | ||
impl QueryResultIterator { | ||
#[napi(catch_unwind)] | ||
pub fn next(&mut self, env: Env) -> napi::Result<Option<QueryResult>> { | ||
match self.0.next() { | ||
Some(result) => Ok(Some(QueryResult::new(env, result)?)), | ||
None => Ok(None), | ||
} | ||
} | ||
} | ||
|
||
#[napi(namespace = "cursor")] | ||
impl Cursor { | ||
#[napi(ts_return_type = "query.QueryResultIterator", catch_unwind)] | ||
pub fn query( | ||
&self, | ||
#[napi(ts_arg_type = "Array<query.Query>")] queries: Vec<&Query>, | ||
) -> QueryResultIterator { | ||
self.0 | ||
.clone() | ||
.query(queries.into_iter().map(|x| x.0.clone()).collect()) | ||
.into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,6 @@ | ||
mod engine; | ||
pub mod model; | ||
mod model; | ||
mod parser; | ||
|
||
#[cfg(test)] | ||
mod engine_tests; | ||
|
||
#[cfg(test)] | ||
mod parser_tests; | ||
pub use engine::{QueryResult, QueryResultIterator}; | ||
pub use model::Query; |
3 changes: 0 additions & 3 deletions
3
crates/codegen/parser/runtime/src/query/mod_for_destination.rs
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/cursor.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/mod.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
crates/solidity/outputs/cargo/slang_solidity/src/generated/napi_interface/query.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 4 additions & 1 deletion
5
crates/solidity/outputs/cargo/slang_solidity/src/generated/query/mod.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
crates/solidity/outputs/npm/package/src/generated/index.d.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.