1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::{RegulatoryGraph, VariableId};
use std::fmt::{Display, Error, Formatter};
impl From<VariableId> for usize {
fn from(value: VariableId) -> Self {
value.0
}
}
impl VariableId {
pub fn try_from_usize(context: &RegulatoryGraph, value: usize) -> Option<VariableId> {
if value < context.variables.len() {
Some(VariableId(value))
} else {
None
}
}
}
impl Display for VariableId {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "BnVariable({})", self.0)
}
}
#[cfg(test)]
mod tests {
use crate::{RegulatoryGraph, VariableId};
use std::convert::TryFrom;
#[test]
fn variable_id_conversion() {
let rg = RegulatoryGraph::try_from("a -> b").unwrap();
let a = rg.find_variable("a").unwrap();
assert_eq!(Some(a), VariableId::try_from_usize(&rg, a.into()));
assert_eq!(None, VariableId::try_from_usize(&rg, 10));
}
}