labview_interop/types/
boolean.rs

1//! Handling for boolean types to and from LabVIEW.
2
3/// The LVBool type is used by LabVIEW for boolean types
4/// on the block diagram.
5///
6/// When you pass data to and from LabVIEW as boolean
7/// types, this is the equivalent.
8///
9/// You can use `.into()` to convert between this and
10/// rust [`bool`] types.
11///
12/// NOTE: This does not seem to work if the boolean is
13/// used as a parameter in a call library function node.
14/// In that case you should convert to numeric first.
15#[repr(transparent)]
16#[derive(PartialEq, Eq, Clone, Copy, Debug)]
17pub struct LVBool(u8);
18
19/// A false constant in the LVBool format.
20pub const LV_FALSE: LVBool = LVBool(0);
21/// A true constant in the LVBool format.
22pub const LV_TRUE: LVBool = LVBool(1);
23
24impl From<bool> for LVBool {
25    fn from(value: bool) -> Self {
26        match value {
27            true => LV_TRUE,
28            false => LV_FALSE,
29        }
30    }
31}
32
33impl From<LVBool> for bool {
34    fn from(value: LVBool) -> Self {
35        !matches!(value.0, 0)
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_boolean_false_to_lv_bool() {
45        let value: LVBool = false.into();
46        assert_eq!(value, LV_FALSE)
47    }
48
49    #[test]
50    fn test_boolean_true_to_lv_bool() {
51        let value: LVBool = true.into();
52        assert_eq!(value, LV_TRUE)
53    }
54
55    #[test]
56    fn test_boolean_lvfalse_to_bool() {
57        let value: bool = LV_FALSE.into();
58        assert!(!value)
59    }
60
61    #[test]
62    fn test_boolean_lvtrue_to_bool() {
63        let value: bool = LV_TRUE.into();
64        assert!(value)
65    }
66
67    #[test]
68    fn test_any_non_zero_to_bool() {
69        let value: bool = LVBool(23).into();
70        assert!(value)
71    }
72
73    #[test]
74    fn lv_bool_in_if_statement() {
75        let true_bool: bool = LV_TRUE.into();
76        let false_bool: bool = LV_FALSE.into();
77        assert!(true_bool);
78        assert!(!false_bool);
79    }
80}