diff --git a/Cargo.toml b/Cargo.toml index 2e9d3f31..ee76c25c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,9 +48,10 @@ ndarray = { version = "^0.16", features = ["serde"] } ndarray-stats = "^0.6" num-complex = "^0.4" # the same version as used by fftw num-traits = "^0.2" -ordered-float = { version = "5", features = ["serde", "schemars"] } +# Using git dependency until ordered-float releases schemars 1.x support (see https://github.com/reem/rust-ordered-float/pull/174) +ordered-float = { git = "https://github.com/mdepta42/rust-ordered-float", rev = "3e80f4219129ae98b2d29ec58286f754cd17a254", features = ["serde", "schemars"] } paste = "1" -schemars = "^0.8" +schemars = "^1" serde = { version = "1", features = ["derive"] } thiserror = "2" thread_local = "1.1" diff --git a/src/extractor.rs b/src/extractor.rs index db379ac6..f45ecf29 100644 --- a/src/extractor.rs +++ b/src/extractor.rs @@ -17,12 +17,13 @@ Bulk feature extractor } #[doc = DOC!()] -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] #[serde( into = "FeatureExtractorParameters", from = "FeatureExtractorParameters", bound = "T: Float, F: FeatureEvaluator" )] +#[schemars(with = "FeatureExtractorParameters::", bound = "F: JsonSchema")] pub struct FeatureExtractor { features: Vec, info: Box, @@ -169,13 +170,6 @@ where } } -impl JsonSchema for FeatureExtractor -where - F: JsonSchema, -{ - json_schema!(FeatureExtractorParameters, true); -} - #[cfg(test)] mod tests { use super::*; diff --git a/src/features/beyond_n_std.rs b/src/features/beyond_n_std.rs index 6d2edf56..ffd0735c 100644 --- a/src/features/beyond_n_std.rs +++ b/src/features/beyond_n_std.rs @@ -39,12 +39,13 @@ D’Isanto et al. 2016 [DOI:10.1093/mnras/stw157](https://doi.org/10.1093/mnras/ /// assert!((1.0 - ts.m.get_std()).abs() < 1e-15); /// assert_eq!(vec![4.0 / 21.0, 2.0 / 21.0], fe.eval(&mut ts).unwrap()); /// ``` -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] #[serde( from = "BeyondNStdParameters", into = "BeyondNStdParameters", bound(deserialize = "T: Float") )] +#[schemars(with = "BeyondNStdParameters")] pub struct BeyondNStd where T: Float, @@ -173,13 +174,6 @@ where } } -impl JsonSchema for BeyondNStd -where - T: Float, -{ - json_schema!(BeyondNStdParameters, false); -} - #[cfg(test)] #[allow(clippy::unreadable_literal)] #[allow(clippy::excessive_precision)] diff --git a/src/features/bins.rs b/src/features/bins.rs index 5ab883ea..16a17199 100644 --- a/src/features/bins.rs +++ b/src/features/bins.rs @@ -33,12 +33,16 @@ considering bin. Bins takes any other feature evaluators to extract features fro } #[doc = DOC!()] -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] #[serde( into = "BinsParameters", from = "BinsParameters", bound(deserialize = "T: Float, F: FeatureEvaluator") )] +#[schemars( + with = "BinsParameters::", + bound = "T: Float, F: FeatureEvaluator" +)] pub struct Bins where T: Float, @@ -277,14 +281,6 @@ where } } -impl JsonSchema for Bins -where - T: Float, - F: FeatureEvaluator, -{ - json_schema!(BinsParameters, false); -} - #[cfg(test)] #[allow(clippy::unreadable_literal)] #[allow(clippy::excessive_precision)] diff --git a/src/features/inter_percentile_range.rs b/src/features/inter_percentile_range.rs index 81daf0da..d92db30e 100644 --- a/src/features/inter_percentile_range.rs +++ b/src/features/inter_percentile_range.rs @@ -23,11 +23,12 @@ inter-percentile range for $p = 0.1$. } #[doc = DOC!()] -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash)] #[serde( from = "InterPercentileRangeParameters", into = "InterPercentileRangeParameters" )] +#[schemars(with = "InterPercentileRangeParameters")] pub struct InterPercentileRange { quantile: NotNan, name: String, @@ -126,10 +127,6 @@ impl From for InterPercentileRange { } } -impl JsonSchema for InterPercentileRange { - json_schema!(InterPercentileRangeParameters, false); -} - #[cfg(test)] #[allow(clippy::unreadable_literal)] #[allow(clippy::excessive_precision)] diff --git a/src/features/median_buffer_range_percentage.rs b/src/features/median_buffer_range_percentage.rs index 2917decc..7693b597 100644 --- a/src/features/median_buffer_range_percentage.rs +++ b/src/features/median_buffer_range_percentage.rs @@ -16,12 +16,13 @@ D’Isanto et al. 2016 [DOI:10.1093/mnras/stw157](https://doi.org/10.1093/mnras/ } #[doc = DOC!()] -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] #[serde( into = "MedianBufferRangePercentageParameters", from = "MedianBufferRangePercentageParameters", bound(deserialize = "T: Float") )] +#[schemars(with = "MedianBufferRangePercentageParameters")] pub struct MedianBufferRangePercentage where T: Float, @@ -154,13 +155,6 @@ where } } -impl JsonSchema for MedianBufferRangePercentage -where - T: Float, -{ - json_schema!(MedianBufferRangePercentageParameters, false); -} - #[cfg(test)] #[allow(clippy::unreadable_literal)] #[allow(clippy::excessive_precision)] diff --git a/src/features/percent_difference_magnitude_percentile.rs b/src/features/percent_difference_magnitude_percentile.rs index f99fe5cb..e0a675ea 100644 --- a/src/features/percent_difference_magnitude_percentile.rs +++ b/src/features/percent_difference_magnitude_percentile.rs @@ -19,11 +19,12 @@ D’Isanto et al. 2016 [DOI:10.1093/mnras/stw157](https://doi.org/10.1093/mnras/ } #[doc = DOC!()] -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash)] #[serde( into = "PercentDifferenceMagnitudePercentileParameters", from = "PercentDifferenceMagnitudePercentileParameters" )] +#[schemars(with = "PercentDifferenceMagnitudePercentileParameters")] pub struct PercentDifferenceMagnitudePercentile { quantile: NotNan, name: String, @@ -129,10 +130,6 @@ impl From for PercentDifferenceM } } -impl JsonSchema for PercentDifferenceMagnitudePercentile { - json_schema!(PercentDifferenceMagnitudePercentileParameters, false); -} - #[cfg(test)] #[allow(clippy::unreadable_literal)] #[allow(clippy::excessive_precision)] diff --git a/src/features/periodogram.rs b/src/features/periodogram.rs index 61e16589..7be308ab 100644 --- a/src/features/periodogram.rs +++ b/src/features/periodogram.rs @@ -32,11 +32,12 @@ Peak evaluator for [Periodogram] #[doc(hidden)] #[doc = PERIODOGRAM_PEAK_DOC!()] -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)] #[serde( from = "PeriodogramPeaksParameters", into = "PeriodogramPeaksParameters" )] +#[schemars(with = "PeriodogramPeaksParameters")] pub struct PeriodogramPeaks { peaks: usize, properties: Box, @@ -156,10 +157,6 @@ impl From for PeriodogramPeaks { } } -impl JsonSchema for PeriodogramPeaks { - json_schema!(PeriodogramPeaksParameters, false); -} - macro_const! { const DOC: &str = r#" Peaks of Lomb–Scargle periodogram and periodogram as a meta-feature @@ -183,12 +180,16 @@ series without observation errors (unity weights are used if required). You can } #[doc = DOC!()] -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema, PartialEq)] #[serde( bound = "T: Float, F: FeatureEvaluator + From + TryInto, >::Error: Debug,", from = "PeriodogramParameters", into = "PeriodogramParameters" )] +#[schemars( + with = "PeriodogramParameters::", + bound = "T: Float, F: FeatureEvaluator" +)] pub struct Periodogram where T: Float, @@ -521,14 +522,6 @@ where } } -impl JsonSchema for Periodogram -where - T: Float, - F: FeatureEvaluator, -{ - json_schema!(PeriodogramParameters, false); -} - #[cfg(test)] #[allow(clippy::unreadable_literal)] #[allow(clippy::excessive_precision)] diff --git a/src/features/transformed.rs b/src/features/transformed.rs index 978b1a8b..638c8817 100644 --- a/src/features/transformed.rs +++ b/src/features/transformed.rs @@ -156,7 +156,17 @@ where F: FeatureEvaluator, Tr: TransformerTrait, { - json_schema!(TransformedParameters, false); + fn inline_schema() -> bool { + true + } + + fn schema_name() -> std::borrow::Cow<'static, str> { + >::schema_name() + } + + fn json_schema(r#gen: &mut schemars::SchemaGenerator) -> schemars::Schema { + >::json_schema(r#gen) + } } #[cfg(test)] diff --git a/src/macros.rs b/src/macros.rs index 89046f67..a757e938 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -97,23 +97,6 @@ macro_rules! transformer_eval { }; } -/// Helper implementing JsonSchema crate -macro_rules! json_schema { - ($parameters: ty, $is_referenceable: expr) => { - fn is_referenceable() -> bool { - $is_referenceable - } - - fn schema_name() -> String { - <$parameters>::schema_name() - } - - fn json_schema(r#gen: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema { - <$parameters>::json_schema(r#gen) - } - }; -} - /// Helper implementing *Fit feature evaluators /// You must: /// - implement all traits of [nl_fit::evaluator] diff --git a/src/nl_fit/evaluator.rs b/src/nl_fit/evaluator.rs index 7c30e4ab..14e0d05b 100644 --- a/src/nl_fit/evaluator.rs +++ b/src/nl_fit/evaluator.rs @@ -35,31 +35,15 @@ pub trait FitDerivalivesTrait { fn derivatives(t: T, param: &[T; NPARAMS], jac: &mut [T; NPARAMS]); } -#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] +#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, PartialEq)] #[serde( into = "FitArraySerde", try_from = "FitArraySerde", bound = "T: Debug + Clone + Serialize + DeserializeOwned + JsonSchema" )] +#[schemars(with = "FitArraySerde::", bound = "T: JsonSchema")] pub struct FitArray(pub [T; NPARAMS]); -impl JsonSchema for FitArray -where - T: schemars::JsonSchema, -{ - fn is_referenceable() -> bool { - false - } - - fn schema_name() -> String { - FitArraySerde::::schema_name() - } - - fn json_schema(r#gen: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema { - FitArraySerde::::json_schema(r#gen) - } -} - impl From<[T; NPARAMS]> for FitArray { fn from(item: [T; NPARAMS]) -> Self { Self(item) diff --git a/src/nl_fit/prior/ln_prior.rs b/src/nl_fit/prior/ln_prior.rs index 8946dd7f..449b08d9 100644 --- a/src/nl_fit/prior/ln_prior.rs +++ b/src/nl_fit/prior/ln_prior.rs @@ -67,11 +67,12 @@ impl LnPriorTrait for NoneLnPrior { } } -#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq, Hash)] #[serde( into = "IndComponentsLnPriorSerde", try_from = "IndComponentsLnPriorSerde" )] +#[schemars(with = "IndComponentsLnPriorSerde")] pub struct IndComponentsLnPrior { pub components: [LnPrior1D; NPARAMS], } @@ -86,20 +87,6 @@ impl LnPriorTrait for IndComponentsLnPrior JsonSchema for IndComponentsLnPrior { - fn is_referenceable() -> bool { - false - } - - fn schema_name() -> String { - IndComponentsLnPriorSerde::schema_name() - } - - fn json_schema(r#gen: &mut schemars::r#gen::SchemaGenerator) -> schemars::schema::Schema { - IndComponentsLnPriorSerde::json_schema(r#gen) - } -} - #[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)] #[serde(rename = "IndComponentsLnPrior")] struct IndComponentsLnPriorSerde { diff --git a/src/periodogram/power_fft.rs b/src/periodogram/power_fft.rs index 9cfa8148..6f0c5046 100644 --- a/src/periodogram/power_fft.rs +++ b/src/periodogram/power_fft.rs @@ -25,12 +25,13 @@ use thread_local::ThreadLocal; /// especially for large grids. /// /// The implementation is inspired by Numerical Recipes, Press et al., 1997, Section 13.8 -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize, JsonSchema)] #[serde( into = "PeriodogramPowerFftParameters", from = "PeriodogramPowerFftParameters", bound = "T: Float" )] +#[schemars(with = "PeriodogramPowerFftParameters")] pub struct PeriodogramPowerFft where T: Float, @@ -197,13 +198,6 @@ where } } -impl JsonSchema for PeriodogramPowerFft -where - T: Float, -{ - json_schema!(PeriodogramPowerFftParameters, false); -} - struct PeriodogramArrays where T: Float, diff --git a/src/sorted_array.rs b/src/sorted_array.rs index c73a4518..55c40fb6 100644 --- a/src/sorted_array.rs +++ b/src/sorted_array.rs @@ -2,9 +2,9 @@ use crate::error::SortedArrayError; use crate::float_trait::Float; use conv::prelude::*; use ndarray::{Array1, ArrayView1}; -use schemars::schema::Schema; -use schemars::{JsonSchema, r#gen::SchemaGenerator}; +use schemars::{JsonSchema, Schema, SchemaGenerator}; use serde::{Deserialize, Serialize}; +use std::borrow::Cow; use std::ops::Deref; // Underlying array is guaranteed to be sorted and contiguous @@ -115,12 +115,12 @@ impl JsonSchema for SortedArray where T: JsonSchema, { - fn is_referenceable() -> bool { - false + fn inline_schema() -> bool { + true } - fn schema_name() -> String { - "SortedArray".to_string() + fn schema_name() -> Cow<'static, str> { + Cow::Borrowed("SortedArray") } fn json_schema(generator: &mut SchemaGenerator) -> Schema {