-
Notifications
You must be signed in to change notification settings - Fork 222
/
lib.rs
121 lines (104 loc) · 3.06 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// https://github.com/rustwasm/wasm-bindgen/pull/2984
#![allow(clippy::drop_non_drop)]
mod utils;
use std::str::FromStr;
use prql_compiler::{sql::Dialect, Target};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn compile(prql_query: &str, options: Option<CompileOptions>) -> Option<String> {
return_or_throw(
prql_compiler::compile(prql_query, options.map(|x| x.into()).unwrap_or_default())
.map_err(|e| e.composed("", prql_query, false)),
)
}
#[wasm_bindgen]
pub fn prql_to_pl(prql_query: &str) -> Option<String> {
return_or_throw(
Ok(prql_query)
.and_then(prql_compiler::prql_to_pl)
.and_then(prql_compiler::json::from_pl),
)
}
#[wasm_bindgen]
pub fn pl_to_rq(pl_json: &str) -> Option<String> {
return_or_throw(
Ok(pl_json)
.and_then(prql_compiler::json::to_pl)
.and_then(prql_compiler::pl_to_rq)
.and_then(prql_compiler::json::from_rq),
)
}
#[wasm_bindgen]
pub fn rq_to_sql(rq_json: &str) -> Option<String> {
return_or_throw(
Ok(rq_json)
.and_then(prql_compiler::json::to_rq)
.and_then(|x| prql_compiler::rq_to_sql(x, prql_compiler::Options::default())),
)
}
/// Compilation options for SQL backend of the compiler.
#[wasm_bindgen]
#[derive(Clone)]
pub struct CompileOptions {
/// Pass generated SQL string trough a formatter that splits it
/// into multiple lines and prettifies indentation and spacing.
///
/// Defaults to true.
pub format: bool,
/// Target to compile to (e.g. sql.postgres)
///
/// If `None` is used, the `target` argument from the query header is used.
#[wasm_bindgen(skip)]
pub target: String,
/// Emits the compiler signature as a comment after generated SQL
///
/// Defaults to true.
pub signature_comment: bool,
}
#[wasm_bindgen]
pub fn get_targets() -> Vec<JsValue> {
prql_compiler::Target::names()
.iter()
.map(|t| JsValue::from_str(t))
.collect()
}
impl Default for CompileOptions {
fn default() -> Self {
Self {
format: true,
target: String::new(),
signature_comment: true,
}
}
}
#[wasm_bindgen]
impl CompileOptions {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self::default()
}
#[wasm_bindgen(getter)]
pub fn target(&self) -> String {
self.target.clone()
}
#[wasm_bindgen(setter)]
pub fn set_target(&mut self, target: String) {
self.target = target;
}
}
impl From<CompileOptions> for prql_compiler::Options {
fn from(o: CompileOptions) -> Self {
let target = Target::from_str(&o.target).unwrap_or(Target::Sql(Some(Dialect::Generic)));
prql_compiler::Options {
format: o.format,
target,
signature_comment: o.signature_comment,
}
}
}
fn return_or_throw(result: Result<String, prql_compiler::ErrorMessages>) -> Option<String> {
match result {
Ok(sql) => Some(sql),
Err(e) => wasm_bindgen::throw_str(&e.to_json()),
}
}