Skip to main content

extension_schema

Function extension_schema 

Source
pub fn extension_schema<T>() -> Result<Value, SigmfError>
where T: SigmfExtension + JsonSchema,
Expand description

Generates the JSON Schema for extension type T.

The schema describes the JSON object T serializes to, keyed by its namespaced wire names.

§Errors

Returns SigmfError::Serde if the generated schema cannot be converted to a Value.

§Example

use serde::{Deserialize, Serialize};
use serde_json::json;
use sigmf::extensions::{
    extension_schema, validate_json_schema, ExtensionScope, SigmfExtension,
};

#[derive(Serialize, Deserialize, schemars::JsonSchema)]
struct Lab {
    #[serde(rename = "lab:gain_db")]
    gain_db: f64,
}

impl SigmfExtension for Lab {
    const NAMESPACE: &'static str = "lab";
    const VERSION: &'static str = "1.0.0";
    const SCOPE: ExtensionScope = ExtensionScope::Global;
    const FIELDS: &'static [&'static str] = &["lab:gain_db"];
}

let schema = extension_schema::<Lab>()?;
validate_json_schema(&schema, &json!({ "lab:gain_db": 30.0 }))?;
assert!(validate_json_schema(&schema, &json!({ "lab:gain_db": "high" })).is_err());