use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case", tag = "select", content = "identities")]
#[non_exhaustive]
pub enum ConstraintSelection {
#[default]
All,
None,
Only(Vec<String>),
}
impl ConstraintSelection {
#[must_use]
pub fn selects(&self, identity: &str) -> bool {
match self {
Self::All => true,
Self::None => false,
Self::Only(identities) => identities.iter().any(|selected| selected == identity),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct ActiveConstraints {
pub generator_capability: ConstraintSelection,
pub voltage_bounds: ConstraintSelection,
pub thermal_limits: ConstraintSelection,
pub angle_bounds: ConstraintSelection,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct MulticonductorActiveConstraints {
pub terminal_voltage_bounds: ConstraintSelection,
pub conductor_limits: ConstraintSelection,
pub generator_capability: ConstraintSelection,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn selections_answer_by_identity() {
assert!(ConstraintSelection::All.selects("branches:3"));
assert!(!ConstraintSelection::None.selects("branches:3"));
let only = ConstraintSelection::Only(vec!["branches:3".to_owned()]);
assert!(only.selects("branches:3"));
assert!(!only.selects("branches:4"));
}
}