use rkyv::{Archive, Deserialize, Serialize};
#[derive(
Archive, Serialize, Deserialize, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize,
)]
#[rkyv(derive(Debug, PartialEq))]
pub enum PersistedFieldExtent {
Global,
Region {
min_x: f32,
min_y: f32,
max_x: f32,
max_y: f32,
},
AttachedToNode(String),
Polygon {
points: Vec<(f32, f32)>,
},
}
impl Default for PersistedFieldExtent {
fn default() -> Self {
PersistedFieldExtent::Global
}
}
#[derive(
Archive,
Serialize,
Deserialize,
Clone,
Copy,
Debug,
PartialEq,
Eq,
Default,
serde::Serialize,
serde::Deserialize,
)]
#[rkyv(derive(Debug, PartialEq))]
pub enum PersistedFieldLifecycle {
#[default]
Active,
Retired,
}
#[derive(
Archive, Serialize, Deserialize, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize,
)]
#[rkyv(derive(Debug))]
pub struct PersistedField {
pub id: String,
#[serde(default)]
pub name: Option<String>,
pub definition_json: String,
#[serde(default)]
pub extent: PersistedFieldExtent,
#[serde(default)]
pub lifecycle: PersistedFieldLifecycle,
}
#[derive(
Archive, Serialize, Deserialize, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize,
)]
#[rkyv(derive(Debug, PartialEq))]
pub enum PersistedNodeSelector {
All,
Tagged(String),
Kind(String),
NotTagged(String),
}
#[derive(
Archive, Serialize, Deserialize, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize,
)]
#[rkyv(derive(Debug, PartialEq))]
pub enum PersistedCouplingResponse {
AttractToMin,
RepelFromMax,
AlignVelocity,
FlowAdvect,
DampenInside { factor: f32 },
ContainmentWall,
Open { predicate: String },
}
#[derive(
Archive, Serialize, Deserialize, Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize,
)]
#[rkyv(derive(Debug))]
pub struct PersistedCoupling {
pub id: String,
pub field_id: String,
pub selector: PersistedNodeSelector,
pub response: PersistedCouplingResponse,
pub strength: f32,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn field_extent_and_lifecycle_serde_roundtrip() {
let extents = [
PersistedFieldExtent::Global,
PersistedFieldExtent::Region {
min_x: -1.0,
min_y: -2.0,
max_x: 3.0,
max_y: 4.0,
},
PersistedFieldExtent::AttachedToNode("abc".into()),
];
for e in &extents {
let j = serde_json::to_string(e).unwrap();
assert_eq!(
*e,
serde_json::from_str::<PersistedFieldExtent>(&j).unwrap()
);
}
for l in [
PersistedFieldLifecycle::Active,
PersistedFieldLifecycle::Retired,
] {
let j = serde_json::to_string(&l).unwrap();
assert_eq!(
l,
serde_json::from_str::<PersistedFieldLifecycle>(&j).unwrap()
);
}
}
#[test]
fn selector_and_response_serde_roundtrip() {
let selectors = [
PersistedNodeSelector::All,
PersistedNodeSelector::Tagged("t".into()),
PersistedNodeSelector::Kind("k".into()),
PersistedNodeSelector::NotTagged("n".into()),
];
for s in &selectors {
let j = serde_json::to_string(s).unwrap();
assert_eq!(
*s,
serde_json::from_str::<PersistedNodeSelector>(&j).unwrap()
);
}
let responses = [
PersistedCouplingResponse::AttractToMin,
PersistedCouplingResponse::RepelFromMax,
PersistedCouplingResponse::AlignVelocity,
PersistedCouplingResponse::FlowAdvect,
PersistedCouplingResponse::DampenInside { factor: 0.25 },
PersistedCouplingResponse::ContainmentWall,
];
for r in &responses {
let j = serde_json::to_string(r).unwrap();
assert_eq!(
*r,
serde_json::from_str::<PersistedCouplingResponse>(&j).unwrap()
);
}
}
#[test]
fn field_and_coupling_rkyv_roundtrip() {
let field = PersistedField {
id: "field-1".into(),
name: Some("focus".into()),
definition_json: "{\"Scalar\":{\"Const\":1.0}}".into(),
extent: PersistedFieldExtent::Region {
min_x: 0.0,
min_y: 0.0,
max_x: 10.0,
max_y: 10.0,
},
lifecycle: PersistedFieldLifecycle::Retired,
};
let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&field).unwrap();
let back: PersistedField =
rkyv::from_bytes::<PersistedField, rkyv::rancor::Error>(&bytes).unwrap();
assert_eq!(back.id, "field-1");
assert_eq!(back.lifecycle, PersistedFieldLifecycle::Retired);
assert_eq!(back.definition_json, field.definition_json);
let coupling = PersistedCoupling {
id: "c-1".into(),
field_id: "field-1".into(),
selector: PersistedNodeSelector::Tagged("paper".into()),
response: PersistedCouplingResponse::DampenInside { factor: 0.3 },
strength: 1.5,
};
let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&coupling).unwrap();
let back: PersistedCoupling =
rkyv::from_bytes::<PersistedCoupling, rkyv::rancor::Error>(&bytes).unwrap();
assert_eq!(back.field_id, "field-1");
assert_eq!(back.strength, 1.5);
}
}