Skip to main content

argumentation_weighted_bipolar/
types.rs

1//! Edge types for weighted bipolar frameworks.
2//!
3//! Re-exports `WeightedAttack` from `argumentation-weighted` and adds
4//! `WeightedSupport`, the support-relation counterpart.
5
6use crate::error::Error;
7pub use argumentation_weighted::types::{AttackWeight, Budget, WeightedAttack};
8
9/// A weighted directed support edge: `supporter` supports `supported`
10/// with the given weight under necessary-support semantics.
11// Eq is not derived because AttackWeight wraps f64, which violates
12// Eq's reflexivity requirement (NaN ≠ NaN). All constructed weights
13// are finite non-NaN by AttackWeight::new validation, but the trait
14// bound is unavailable.
15#[derive(Debug, Clone, PartialEq)]
16pub struct WeightedSupport<A: Clone + Eq> {
17    /// The supporter argument.
18    pub supporter: A,
19    /// The supported argument.
20    pub supported: A,
21    /// The support weight. Higher weights are harder to tolerate.
22    pub weight: AttackWeight,
23}
24
25impl<A: Clone + Eq> WeightedSupport<A> {
26    /// Construct a weighted support, rejecting self-support and
27    /// invalid weights.
28    pub fn new(supporter: A, supported: A, weight: f64) -> Result<Self, Error> {
29        if supporter == supported {
30            return Err(Error::IllegalSelfSupport);
31        }
32        let w = AttackWeight::new(weight).map_err(|_| Error::InvalidWeight { weight })?;
33        Ok(Self { supporter, supported, weight: w })
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn weighted_support_new_validates_weight() {
43        assert!(WeightedSupport::new("a", "b", 0.5).is_ok());
44        assert!(WeightedSupport::new("a", "b", -1.0).is_err());
45        assert!(WeightedSupport::new("a", "b", f64::NAN).is_err());
46    }
47
48    #[test]
49    fn weighted_support_rejects_self_support() {
50        let err = WeightedSupport::new("a", "a", 0.5).unwrap_err();
51        assert!(matches!(err, Error::IllegalSelfSupport));
52    }
53}