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

34 lines
1.0 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;
pub use napi::antenna::Antenna;
pub use napi::antenna::AntennaSrc;
} else {
pub use antenna::Antenna;
pub use antenna::AntennaSrc;
}
}
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")
}
}