calckey/packages/backend/native-utils/src/model/schema.rs

36 lines
1.1 KiB
Rust
Raw Normal View History

2023-05-25 06:34:48 +00:00
pub mod antenna;
pub mod app;
2023-06-02 10:22:09 +00:00
use cfg_if::cfg_if;
2023-05-25 06:34:48 +00:00
use jsonschema::JSONSchema;
use schemars::{schema_for, JsonSchema};
2023-06-02 10:22:09 +00:00
cfg_if! {
if #[cfg(feature = "napi")] {
mod napi;
2023-06-02 11:08:58 +00:00
pub use self::napi::antenna::Antenna;
pub use self::napi::antenna::AntennaSrc;
2023-06-02 10:22:09 +00:00
} else {
pub use antenna::Antenna;
pub use antenna::AntennaSrc;
2023-06-02 11:08:58 +00:00
pub use app::App;
pub use app::AppPermission;
2023-06-02 10:22:09 +00:00
}
}
2023-05-25 06:34:48 +00:00
/// Structs of schema defitions implement this trait in order to
/// provide the JSON Schema validator [`jsonschema::JSONSchema`].
2023-06-02 08:34:49 +00:00
pub trait Schema<T: JsonSchema> {
2023-05-25 06:34:48 +00:00
/// Returns the validator of [JSON Schema Draft
/// 7](https://json-schema.org/specification-links.html#draft-7) with the
/// default settings of [`schemars::gen::SchemaSettings`].
fn validator() -> JSONSchema {
let root = schema_for!(T);
let schema = serde_json::to_value(&root).expect("Schema definition invalid");
JSONSchema::options()
.with_draft(jsonschema::Draft::Draft7)
.compile(&schema)
.expect("Unable to compile schema")
}
}