base/
info.rs

1use std::{borrow::Cow, fmt::Display};
2
3use crate::opinion::{MyFloat, Phi, Psi, B, H, O};
4use subjective_logic::{
5    mul::labeled::{OpinionD1, SimplexD1},
6    multi_array::labeled::MArrD1,
7};
8
9#[derive(Debug, Clone)]
10pub enum InfoContent<'a, V: Clone> {
11    Misinfo {
12        op: Cow<'a, OpinionD1<Psi, V>>,
13    },
14    Correction {
15        op: Cow<'a, OpinionD1<Psi, V>>,
16        misinfo: Cow<'a, OpinionD1<Psi, V>>,
17    },
18    Observation {
19        op: Cow<'a, OpinionD1<O, V>>,
20    },
21    Inhibition {
22        op1: Cow<'a, OpinionD1<Phi, V>>,
23        op2: Cow<'a, MArrD1<Psi, SimplexD1<H, V>>>,
24        op3: Cow<'a, MArrD1<B, SimplexD1<H, V>>>,
25    },
26}
27
28impl<'a, V: MyFloat> From<&InfoContent<'a, V>> for InfoLabel {
29    fn from(value: &InfoContent<'a, V>) -> Self {
30        match value {
31            InfoContent::Misinfo { .. } => InfoLabel::Misinfo,
32            InfoContent::Correction { .. } => InfoLabel::Corrective,
33            InfoContent::Observation { .. } => InfoLabel::Observed,
34            InfoContent::Inhibition { .. } => InfoLabel::Inhibitive,
35        }
36    }
37}
38
39#[derive(Debug)]
40pub struct Info<'a, V: Clone> {
41    pub idx: usize,
42    num_shared: usize,
43    num_viewed: usize,
44    label: InfoLabel,
45    p: InfoContent<'a, V>,
46}
47
48impl<'a, V: MyFloat> Info<'a, V> {
49    pub fn new(idx: usize, p: InfoContent<'a, V>) -> Self {
50        Self {
51            idx,
52            label: (&p).into(),
53            num_shared: 0,
54            num_viewed: 0,
55            p,
56        }
57    }
58
59    pub fn content(&self) -> &InfoContent<'a, V> {
60        &self.p
61    }
62
63    pub fn label(&self) -> &InfoLabel {
64        &self.label
65    }
66
67    #[inline]
68    pub fn viewed(&mut self) {
69        self.num_viewed += 1;
70    }
71
72    #[inline]
73    pub fn shared(&mut self) {
74        self.num_shared += 1;
75    }
76
77    #[inline]
78    pub fn num_shared(&self) -> usize {
79        self.num_shared
80    }
81}
82
83#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Copy)]
84pub enum InfoLabel {
85    Misinfo,
86    Corrective,
87    Observed,
88    Inhibitive,
89}
90
91impl From<&InfoLabel> for u8 {
92    fn from(value: &InfoLabel) -> Self {
93        match value {
94            InfoLabel::Misinfo => 0,
95            InfoLabel::Corrective => 1,
96            InfoLabel::Observed => 2,
97            InfoLabel::Inhibitive => 3,
98        }
99    }
100}
101
102impl Display for InfoLabel {
103    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104        match self {
105            InfoLabel::Misinfo => write!(f, "misinfo"),
106            InfoLabel::Corrective => write!(f, "corrective"),
107            InfoLabel::Observed => write!(f, "observed"),
108            InfoLabel::Inhibitive => write!(f, "inhivitive"),
109        }
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::InfoLabel;
116
117    #[test]
118    fn test_info_type() {
119        println!("{}", InfoLabel::Misinfo);
120    }
121}