Skip to main content

hydr_structures/structures/controlled_pump/
snapshot.rs

1//! Complete response assumptions and control continuation with atomic restoration.
2
3use super::{ControlledPumpBatch, PumpResponseDefinition};
4use crate::structures::{OperatingControlSnapshotData, StructureError, StructureInputErrorKind};
5use hydr_core::contract::package_identity;
6use numr::dtype::DType;
7use numr::runtime::Runtime;
8
9/// Schema identity of the controlled pump snapshot package.
10pub const CONTROLLED_PUMP_SNAPSHOT_SCHEMA: &str = "hydr-controlled-pump-checkpoint-1";
11/// Numerical contract identity of the controlled pump snapshot package.
12pub const CONTROLLED_PUMP_CONTRACT_VERSION: &str = "hydr-controlled-pump-numr-cpu-1";
13
14/// Complete caller-owned response assumptions and the existing controller continuation.
15///
16/// Preserve every original control F64 bit during encoding. No independent response state exists.
17#[derive(Clone, Debug, PartialEq)]
18pub struct ControlledPumpSnapshotData {
19    /// Package schema identity.
20    pub schema: String,
21    /// Numerical contract identity.
22    pub contract: String,
23    /// Complete ordered original response models and explicit applicability evidence.
24    pub definitions: Vec<PumpResponseDefinition>,
25    /// Complete original controller model, accepted memory, calendar, and bounded receipts.
26    pub controls: OperatingControlSnapshotData,
27}
28/// Trusted complete owner clone without numerical projection or prior-owner links.
29#[derive(Clone, Debug)]
30pub struct ControlledPumpSnapshot<R: Runtime<DType = DType>> {
31    batch: ControlledPumpBatch<R>,
32}
33impl<R: Runtime<DType = DType>> ControlledPumpSnapshot<R> {
34    /// Accepted original snapshot time in seconds.
35    pub fn time(&self) -> f64 {
36        self.batch.time()
37    }
38    /// Explicitly project complete response assumptions and controller continuation.
39    pub fn data(&self) -> Result<ControlledPumpSnapshotData, StructureError> {
40        self.batch.snapshot_data()
41    }
42}
43fn require(condition: bool, message: &str) -> Result<(), StructureError> {
44    if condition {
45        Ok(())
46    } else {
47        Err(StructureError::input(
48            StructureInputErrorKind::Definition,
49            message,
50        ))
51    }
52}
53impl<R: Runtime<DType = DType>> ControlledPumpBatch<R> {
54    /// Clone complete accepted continuation without derived-state array readback.
55    pub fn snapshot(&self) -> ControlledPumpSnapshot<R> {
56        ControlledPumpSnapshot {
57            batch: self.clone(),
58        }
59    }
60    /// Explicitly project response assumptions and complete original control continuation.
61    pub fn snapshot_data(&self) -> Result<ControlledPumpSnapshotData, StructureError> {
62        Ok(ControlledPumpSnapshotData {
63            schema: CONTROLLED_PUMP_SNAPSHOT_SCHEMA.into(),
64            contract: CONTROLLED_PUMP_CONTRACT_VERSION.into(),
65            definitions: self.definitions().to_vec(),
66            controls: self.controls.snapshot_data()?,
67        })
68    }
69    /// Restore trusted control continuation while retaining the complete destination response context.
70    pub fn restore(&mut self, snapshot: &ControlledPumpSnapshot<R>) -> Result<(), StructureError> {
71        require(
72            super::compatibility::same_context(&self.context, &snapshot.batch.context),
73            "controlled pump snapshot response assumptions or immutable controller model differ",
74        )?;
75        let mut controls = self.controls.clone();
76        controls.restore(&snapshot.batch.controls.snapshot())?;
77        self.controls = controls;
78        Ok(())
79    }
80    /// Check response assumptions and complete caller-owned child continuation before atomic replacement.
81    ///
82    /// Child bounded consistency checks establish no complete historical authenticity.
83    pub fn restore_data(&mut self, data: &ControlledPumpSnapshotData) -> Result<(), StructureError>
84    where
85        R::Client: numr::ops::BinaryOps<R>
86            + numr::ops::CompareOps<R>
87            + numr::ops::ConditionalOps<R>
88            + numr::ops::ReduceOps<R>
89            + numr::ops::ScalarOps<R>
90            + numr::ops::TwoSumOps<R>
91            + numr::ops::TypeConversionOps<R>
92            + numr::ops::UnaryOps<R>,
93    {
94        package_identity(
95            "controlled pump",
96            &data.schema,
97            CONTROLLED_PUMP_SNAPSHOT_SCHEMA,
98            &data.contract,
99            CONTROLLED_PUMP_CONTRACT_VERSION,
100        )
101        .map_err(StructureError::definition)?;
102        require(
103            self.definitions() == data.definitions.as_slice(),
104            "controlled pump snapshot original response assumptions differ",
105        )?;
106        let mut controls = self.controls.clone();
107        controls.restore_data(&data.controls)?;
108        self.controls = controls;
109        Ok(())
110    }
111}