Skip to content

Commit 48c1652

Browse files
authored
Reorder #[derive] and add serde attributes per rules (#340) (#377)
* refactor(attrs): reorder #[derive] and add serde attributes per rules Apply canonical #[derive] order (Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default) across numeric kernels and add #[serde] attributes per rules/global_rules.md: - #[repr(u8)] on small stable enums (Axis, InterpolationType, MergeOperation) - #[serde(deny_unknown_fields)] on input DTOs (GreeksSnapshot, Curve, Surface, Point3D) - Derive reordering in 16 files (pricing, greeks, simulation, curves, surfaces, geometrics) Composition layers (strategies, chains, pnl, risk, backtesting, series) audit completed; detailed per-file changes reported in Issue #340. * refactor(attrs): audit #[repr(u8)], #[derive] order, and serde conventions (#340) Canonical derive order: Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, then Serialize, Deserialize, ToSchema. - Apply #[repr(u8)] to small stable enums: LineStyle, TraceMode, PlotType, BasicAxisTypes, MarginType - Reorder all derive clauses to canonical sequence across model, visualization, strategies - Add #[serde(rename_all = "snake_case")] to public-facing enums for consistent wire format - Fix DebugPretty/DisplaySimple placement (custom derives first per rule) Fixes #340. Tests: 3753 passed; 4 pre-existing failures. * fix(axis): keep BasicAxisTypes Pascal-case wire format (#340) Existing consumers rely on Pascal-case JSON (e.g. `"Delta"`, `"Volatility"`) and the round-trip is locked by `tests_basic_axis_types::test_specific_serialization_format`. Drop the `#[serde(rename_all = "snake_case")]` added in the parent commit and add a NOTE explaining the exception per `rules/global_rules.md` §Serde conventions.
1 parent fffc85d commit 48c1652

22 files changed

Lines changed: 53 additions & 33 deletions

File tree

Draws/Simulation/simulator_test_montecarlo.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/curves/curve.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use utoipa::ToSchema;
6161
/// - [`Point2D`]: The fundamental data type for representing points in 2D space.
6262
/// - [`MergeOperation`]: Enum for combining multiple curves.
6363
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
64+
#[serde(deny_unknown_fields)]
6465
pub struct Curve {
6566
/// A ordered set of `Point2D` objects that defines the curve in terms of its x-y plane coordinates.
6667
/// Points are stored in a `BTreeSet` which automatically maintains them in sorted order by their x-coordinate.

src/geometrics/analysis/metrics.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use utoipa::ToSchema;
5959
/// ## Remarks
6060
/// The `CurveMetrics` struct is designed to be reusable across various analytical contexts,
6161
/// providing a versatile and standardized way to represent curve characteristics.
62-
#[derive(Clone, DebugPretty, DisplaySimple, ToSchema, Serialize)]
62+
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, ToSchema)]
6363
pub struct Metrics {
6464
/// - **Basic Metrics (`basic`)**:
6565
/// Includes fundamental statistical measures such as mean, median, mode, and standard deviation.
@@ -235,7 +235,7 @@ impl Metrics {
235235
/// - [`ShapeMetrics`]: Captures shape-related properties like skewness and kurtosis.
236236
/// - [`RiskMetrics`]: Measures risk characteristics of financial instruments.
237237
/// - [`TrendMetrics`]: Represents time-based trends in data series.
238-
#[derive(Clone, Copy, DebugPretty, DisplaySimple, ToSchema, Serialize)]
238+
#[derive(DebugPretty, DisplaySimple, Clone, Copy, Serialize, ToSchema)]
239239
pub struct BasicMetrics {
240240
/// The arithmetic mean (average) of the dataset.
241241
/// Calculated by summing all values and dividing by the count of values.
@@ -297,7 +297,7 @@ pub struct BasicMetrics {
297297
/// `ShapeMetrics` is typically part of the larger `AnalysisResult` structure
298298
/// that provides comprehensive curve analysis. It uses `Point2D` to represent
299299
/// all positional data with high-precision `Decimal` values.
300-
#[derive(Clone, DebugPretty, DisplaySimple, ToSchema, Serialize)]
300+
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, ToSchema)]
301301
pub struct ShapeMetrics {
302302
/// Describes the asymmetry of the curve's distribution.
303303
/// A positive value indicates a right tail (right-skewed distribution),
@@ -357,7 +357,7 @@ pub struct ShapeMetrics {
357357
///
358358
/// This structure is part of the curves analysis module that provides comprehensive
359359
/// statistical and financial analysis tools for mathematical curves.
360-
#[derive(Clone, Copy, DebugPretty, DisplaySimple, ToSchema, Serialize)]
360+
#[derive(DebugPretty, DisplaySimple, Clone, Copy, Serialize, ToSchema)]
361361
pub struct RangeMetrics {
362362
/// The minimum point in the dataset, containing the smallest x and y coordinates observed.
363363
pub min: Point2D,
@@ -405,7 +405,7 @@ pub struct RangeMetrics {
405405
///
406406
/// This structure is a key component of the curve analysis module and works
407407
/// alongside other metric types to provide comprehensive statistical analysis.
408-
#[derive(Clone, DebugPretty, DisplaySimple, ToSchema, Serialize)]
408+
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, ToSchema)]
409409
pub struct TrendMetrics {
410410
/// The slope coefficient of the linear regression line, indicating the
411411
/// rate and direction of change in the dataset.
@@ -451,7 +451,7 @@ pub struct TrendMetrics {
451451
/// # Note
452452
/// For accuracy, ensure the data used to compute these metrics represents a
453453
/// sufficiently long time horizon and is free from anomalies.
454-
#[derive(Clone, Copy, DebugPretty, DisplaySimple, ToSchema, Serialize)]
454+
#[derive(DebugPretty, DisplaySimple, Clone, Copy, Serialize, ToSchema)]
455455
pub struct RiskMetrics {
456456
/// Measures the degree of variation in returns over time.
457457
/// Represents the standard deviation of returns and is a widely used

src/geometrics/analysis/statistics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use utoipa::ToSchema;
4949
/// - Analyze curve properties for more accurate modeling and representation.
5050
/// - Identify key points that define the geometric behavior of a curve.
5151
///
52-
#[derive(Clone, DebugPretty, DisplaySimple, ToSchema, Serialize)]
52+
#[derive(DebugPretty, DisplaySimple, Clone, Serialize, ToSchema)]
5353
pub struct AnalysisResult {
5454
/// The fundamental statistical metrics of the dataset, including measures of
5555
/// central tendency (mean, median, mode) and dispersion (standard deviation).

src/geometrics/interpolation/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
/// This enum is part of a modular design, with each interpolation type defined in its own
5353
/// module for clarity and separation of concerns.
5454
///
55-
#[derive(Debug, Clone, Copy)]
55+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
56+
#[repr(u8)]
5657
pub enum InterpolationType {
5758
/// Linear interpolation that calculates values by drawing straight lines between data points.
5859
/// Efficient but may lack smoothness for certain applications.

src/geometrics/operations/arithmetic.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use utoipa::ToSchema;
1818
///
1919
/// The operation specified will determine how values are combined when merging
2020
/// geometric objects that implement the `Arithmetic` trait.
21-
#[derive(Clone, Copy, Debug, PartialEq, ToSchema)]
21+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, ToSchema)]
22+
#[repr(u8)]
2223
pub enum MergeOperation {
2324
/// Adds values together (a + b)
2425
Add,

src/greeks/equations.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use utoipa::ToSchema;
3939
/// * `color`: Measures the rate of change of gamma in relation to changes in time
4040
///
4141
/// These metrics help traders understand and manage the various dimensions of risk in option positions.
42-
#[derive(PartialEq, Clone, DebugPretty, DisplaySimple, ToSchema, Serialize)]
42+
#[derive(DebugPretty, DisplaySimple, Clone, PartialEq, Serialize, ToSchema)]
4343
pub struct Greek {
4444
/// Measures sensitivity to changes in the underlying asset's price (first derivative)
4545
pub delta: Decimal,
@@ -75,6 +75,7 @@ pub struct Greek {
7575
/// and deserialization for storage or communication purposes, and implements common traits like
7676
/// `Debug`, `Clone`, and `PartialEq`.
7777
#[derive(DebugPretty, DisplaySimple, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
78+
#[serde(deny_unknown_fields)]
7879
pub struct GreeksSnapshot {
7980
/// Measures sensitivity to changes in the underlying asset's price (first derivative)
8081
pub delta: Decimal,

src/model/axis.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ use serde::{Deserialize, Serialize};
1616
/// - Create parameter surfaces (e.g., volatility surface)
1717
/// - Define dimensions for multi-dimensional analysis
1818
/// - Specify which metrics to calculate or display
19-
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy, Hash, Eq)]
19+
// NOTE: `#[serde(rename_all = "snake_case")]` intentionally omitted — existing
20+
// consumers rely on the Pascal-case wire format (`"Delta"`, `"Volatility"`),
21+
// enforced by `tests_basic_axis_types::test_specific_serialization_format`.
22+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
23+
#[repr(u8)]
2024
pub enum BasicAxisTypes {
2125
/// Sensitivity of option price to changes in underlying price (first derivative)
2226
Delta,
@@ -131,13 +135,9 @@ impl Iterator for BasicAxisTypesIter {
131135
type Item = BasicAxisTypes;
132136

133137
fn next(&mut self) -> Option<Self::Item> {
134-
if self.index < BasicAxisTypes::VALUES.len() {
135-
let value = BasicAxisTypes::VALUES[self.index];
136-
self.index += 1;
137-
Some(value)
138-
} else {
139-
None
140-
}
138+
let value = *BasicAxisTypes::VALUES.get(self.index)?;
139+
self.index += 1;
140+
Some(value)
141141
}
142142
}
143143

src/model/leg/leg_enum.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ use utoipa::ToSchema;
5757
/// * `Spot` - Direct ownership of underlying asset
5858
/// * `Future` - Exchange-traded futures contract
5959
/// * `Perpetual` - Crypto perpetual swap contract
60-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ToSchema)]
60+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
61+
#[serde(rename_all = "snake_case")]
6162
pub enum Leg {
6263
/// Standard option position (Call/Put).
6364
Option(Box<Position>),

src/model/leg/perpetual.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ use serde::{Deserialize, Serialize};
5050
use utoipa::ToSchema;
5151

5252
/// Margin type for perpetual positions.
53-
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, ToSchema, Default)]
53+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, ToSchema)]
54+
#[serde(rename_all = "snake_case")]
55+
#[repr(u8)]
5456
pub enum MarginType {
5557
/// Cross margin - shares margin across all positions
5658
Cross,
@@ -87,7 +89,7 @@ impl std::fmt::Display for MarginType {
8789
/// * `funding_rate` - Current funding rate (updated periodically)
8890
/// * `date` - Position open timestamp
8991
/// * `fees` - Trading fees (maker/taker)
90-
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, ToSchema)]
92+
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToSchema)]
9193
pub struct PerpetualPosition {
9294
/// Trading pair symbol (e.g., "BTC-USDT-PERP").
9395
pub symbol: String,

0 commit comments

Comments
 (0)