forked from elastic/elasticsearch-specification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
65 lines (59 loc) · 2.66 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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use anyhow::bail;
use clients_schema::{Availabilities, Visibility};
use wasm_bindgen::prelude::*;
use clients_schema::transform::ExpandConfig;
#[cfg(all(not(target_arch = "wasm32"), not(feature = "cargo-clippy")))]
compile_error!("To build this crate use `make compiler-wasm-lib`");
#[wasm_bindgen]
pub fn convert_schema_to_openapi(json: &str, flavor: &str) -> Result<String, String> {
set_panic_hook();
convert0(json, flavor).map_err(|err| err.to_string())
}
fn convert0(json: &str, flavor: &str) -> anyhow::Result<String> {
let filter: Option<fn(&Option<Availabilities>) -> bool> = match flavor {
"all" => None,
"stack" => Some(|a| {
// Generate public and private items for Stack
clients_schema::Flavor::Stack.available(a)
}),
"serverless" => Some(|a| {
// Generate only public items for Serverless
clients_schema::Flavor::Serverless.visibility(a) == Some(Visibility::Public)
}),
_ => bail!("Unknown flavor {}", flavor),
};
let mut schema = clients_schema::IndexedModel::from_reader(json.as_bytes())?;
schema = clients_schema::transform::expand_generics(schema, ExpandConfig::default())?;
if let Some(filter) = filter {
schema = clients_schema::transform::filter_availability(schema, filter)?;
}
let openapi = clients_schema_to_openapi::convert_schema(&schema)?;
let result = serde_json::to_string_pretty(&openapi)?;
Ok(result)
}
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}