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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
pub const FIRST_BIT: u32 = 0x80000000; #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)] pub enum PathValue { Normal(u32), Hardened(u32) } impl PathValue { pub fn is_ok(value: u32) -> bool { value < FIRST_BIT } pub fn try_normal(value: u32) -> Result<PathValue, ()> { if !PathValue::is_ok(value) { Err(()) } else { Ok(PathValue::Normal(value)) } } pub fn normal(value: u32) -> PathValue { if let Ok(result) = PathValue::try_normal(value) { result } else { panic!("Raw hardened value passed") } } pub fn try_hardened(value: u32) -> Result<PathValue, ()> { if !PathValue::is_ok(value) { Err(()) } else { Ok(PathValue::Hardened(value)) } } pub fn hardened(value: u32) -> PathValue { if let Ok(result) = PathValue::try_hardened(value) { result } else { panic!("Raw hardened value passed") } } pub fn from_raw(value: u32) -> PathValue { if value >= FIRST_BIT { PathValue::Hardened(value - FIRST_BIT) } else { PathValue::Normal(value) } } pub fn as_number(&self) -> u32 { match &self { PathValue::Normal(n) => *n, PathValue::Hardened(n) => *n } } pub fn to_raw(&self) -> u32 { match &self { PathValue::Normal(n) => *n, PathValue::Hardened(n) => *n + FIRST_BIT } } } #[cfg(test)] mod tests { use super::*; #[test] fn ok_for_small_values() { let values = vec![ 0u32, 1, 2, 3, 100, 1000, 10000, 0x80000000 - 1 ]; for value in values { assert!(PathValue::is_ok(value), "value: {}", value); } } #[test] fn not_ok_for_large_values() { let values = vec![ 0x80000000, 0x80000001, 0xffffffff ]; for value in values { assert!(!PathValue::is_ok(value), "value: {}", value); } } #[test] fn create_normal() { assert_eq!(PathValue::Normal(0), PathValue::normal(0)); assert_eq!(PathValue::Normal(1), PathValue::normal(1)); assert_eq!(PathValue::Normal(101), PathValue::normal(101)); } #[test] fn create_hardened() { assert_eq!(PathValue::Hardened(0), PathValue::hardened(0)); assert_eq!(PathValue::Hardened(1), PathValue::hardened(1)); assert_eq!(PathValue::Hardened(101), PathValue::hardened(101)); } #[test] #[should_panic] fn panic_on_invalid_normal() { PathValue::normal(0x80000001); } #[test] #[should_panic] fn panic_on_invalid_hardened() { PathValue::hardened(0x80000001); } #[test] fn create_from_raw_normal() { assert_eq!(PathValue::Normal(0), PathValue::from_raw(0)); assert_eq!(PathValue::Normal(100), PathValue::from_raw(100)); assert_eq!(PathValue::Normal(0xffffff), PathValue::from_raw(0xffffff)); } #[test] fn create_from_raw_hardened() { assert_eq!(PathValue::hardened(0), PathValue::from_raw(0x80000000)); assert_eq!(PathValue::hardened(1), PathValue::from_raw(0x80000001)); assert_eq!(PathValue::hardened(44), PathValue::from_raw(0x8000002c)); } #[test] fn to_raw_normal() { assert_eq!(0, PathValue::Normal(0).to_raw()); assert_eq!(123, PathValue::Normal(123).to_raw()); } #[test] fn to_raw_hardened() { assert_eq!(0x80000000, PathValue::Hardened(0).to_raw()); assert_eq!(0x8000002c, PathValue::Hardened(44).to_raw()); } #[test] fn as_number_normal() { assert_eq!(0, PathValue::Normal(0).as_number()); assert_eq!(123, PathValue::Normal(123).as_number()); } #[test] fn as_number_hardened() { assert_eq!(0, PathValue::Hardened(0).as_number()); assert_eq!(123, PathValue::Hardened(123).as_number()); } }