feophantlib/constants/
nullable.rs1use std::fmt;
4
5#[derive(Clone, Copy, Debug, PartialEq)]
6pub enum Nullable {
7 Null,
8 NotNull,
9}
10
11impl From<bool> for Nullable {
12 fn from(b: bool) -> Self {
13 if b {
14 Nullable::Null
15 } else {
16 Nullable::NotNull
17 }
18 }
19}
20
21impl From<u8> for Nullable {
22 fn from(u: u8) -> Self {
23 if u == 0x0 {
24 Nullable::Null
25 } else {
26 Nullable::NotNull
27 }
28 }
29}
30
31impl fmt::Display for Nullable {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 match self {
34 Nullable::NotNull => write!(f, "NotNull"),
35 Nullable::Null => write!(f, "Null"),
36 }
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
45 fn test_nullable_display() -> Result<(), Box<dyn std::error::Error>> {
46 assert_eq!(Nullable::NotNull.to_string(), "NotNull".to_string());
47 assert_eq!(Nullable::Null.to_string(), "Null".to_string());
48 Ok(())
49 }
50
51 #[test]
52 fn test_nullable_from() -> Result<(), Box<dyn std::error::Error>> {
53 assert_eq!(Nullable::from(false), Nullable::NotNull);
54 assert_eq!(Nullable::from(true), Nullable::Null);
55
56 assert_eq!(Nullable::from(0), Nullable::Null);
57 for u in 1..u8::MAX {
58 assert_eq!(Nullable::from(u), Nullable::NotNull);
59 }
60
61 Ok(())
62 }
63}