Skip to main content

powerio_pkg/
model.rs

1//! The model kind and the single typed IR payload.
2//!
3//! The two IR families never merge. [`ModelKind`] is stored explicitly on the
4//! package; [`ModelPayload`] is the tagged wrapper around exactly one payload.
5//! The payload's `kind()` must agree with the package's `model_kind` (the
6//! package asserts this), but the authoritative kind is the standalone field, so
7//! a reader never infers the kind from which payload field is present.
8
9use serde::{Deserialize, Serialize};
10
11use powerio::BalancedNetwork;
12use powerio_dist::MulticonductorNetwork;
13
14/// Which concrete static-grid IR family the payload is.
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
16#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
17#[serde(rename_all = "snake_case")]
18#[non_exhaustive]
19pub enum ModelKind {
20    /// Scalar positive-sequence transmission model ([`powerio::BalancedNetwork`]).
21    Balanced,
22    /// Wire-coordinate distribution model ([`powerio_dist::MulticonductorNetwork`]).
23    Multiconductor,
24}
25
26/// The one IR payload a package carries, tagged by `kind` in JSON so the payload
27/// is self-describing in addition to the top-level `model_kind`.
28///
29/// The payload is the serde snapshot of the PowerIO Rust IR
30/// ([`powerio::Network`] / [`powerio_dist::DistNetwork`]), declared by the
31/// package's `payload_schema` / `payload_schema_version` fields and versioned
32/// independently of the envelope: additive IR growth bumps the payload minor
33/// with no envelope version change. See `docs/src/pio-json-schema.md`.
34#[derive(Clone, Debug, Serialize, Deserialize)]
35#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
36#[serde(tag = "kind", rename_all = "snake_case")]
37pub enum ModelPayload {
38    Balanced {
39        balanced_network: Box<BalancedNetwork>,
40    },
41    Multiconductor {
42        multiconductor_network: Box<MulticonductorNetwork>,
43    },
44}
45
46impl ModelPayload {
47    pub fn balanced(net: BalancedNetwork) -> Self {
48        Self::Balanced {
49            balanced_network: Box::new(net),
50        }
51    }
52
53    pub fn multiconductor(net: MulticonductorNetwork) -> Self {
54        Self::Multiconductor {
55            multiconductor_network: Box::new(net),
56        }
57    }
58
59    pub fn kind(&self) -> ModelKind {
60        match self {
61            ModelPayload::Balanced { .. } => ModelKind::Balanced,
62            ModelPayload::Multiconductor { .. } => ModelKind::Multiconductor,
63        }
64    }
65
66    /// The balanced payload, if this is one.
67    pub fn as_balanced(&self) -> Option<&BalancedNetwork> {
68        match self {
69            ModelPayload::Balanced { balanced_network } => Some(balanced_network),
70            ModelPayload::Multiconductor { .. } => None,
71        }
72    }
73
74    /// The multiconductor payload, if this is one.
75    pub fn as_multiconductor(&self) -> Option<&MulticonductorNetwork> {
76        match self {
77            ModelPayload::Multiconductor {
78                multiconductor_network,
79            } => Some(multiconductor_network),
80            ModelPayload::Balanced { .. } => None,
81        }
82    }
83}