|
1 |
| - |
2 | 1 | wit_bindgen_rust::import!("../../wit/ephemeral/outbound-pg.wit");
|
3 | 2 |
|
4 | 3 | /// Exports the generated outbound Pg items.
|
5 | 4 | pub use outbound_pg::*;
|
6 | 5 |
|
7 |
| -impl TryFrom<&DbValue> for i32 { |
8 |
| - type Error = anyhow::Error; |
| 6 | +pub trait Decode: Sized { |
| 7 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error>; |
| 8 | +} |
| 9 | + |
| 10 | +impl<T> Decode for Option<T> |
| 11 | +where |
| 12 | + T: Decode, |
| 13 | +{ |
| 14 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error> { |
| 15 | + match value { |
| 16 | + DbValue::DbNull => Ok(None), |
| 17 | + v => Ok(Some(T::decode(v)?)), |
| 18 | + } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl Decode for bool { |
| 23 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error> { |
| 24 | + match value { |
| 25 | + DbValue::Boolean(boolean) => Ok(*boolean), |
| 26 | + _ => Err(anyhow::anyhow!( |
| 27 | + "Expected BOOL from the DB but got {:?}", |
| 28 | + value |
| 29 | + )), |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl Decode for i16 { |
| 35 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error> { |
| 36 | + match value { |
| 37 | + DbValue::Int16(n) => Ok(*n), |
| 38 | + _ => Err(anyhow::anyhow!( |
| 39 | + "Expected SMALLINT from the DB but got {:?}", |
| 40 | + value |
| 41 | + )), |
| 42 | + } |
| 43 | + } |
| 44 | +} |
9 | 45 |
|
10 |
| - fn try_from(value: &DbValue) -> Result<Self, Self::Error> { |
| 46 | +impl Decode for i32 { |
| 47 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error> { |
11 | 48 | match value {
|
12 | 49 | DbValue::Int32(n) => Ok(*n),
|
13 | 50 | _ => Err(anyhow::anyhow!(
|
14 |
| - "Expected integer from database but got {:?}", |
| 51 | + "Expected INT from the DB but got {:?}", |
15 | 52 | value
|
16 | 53 | )),
|
17 | 54 | }
|
18 | 55 | }
|
19 | 56 | }
|
20 | 57 |
|
21 |
| -impl TryFrom<&DbValue> for String { |
22 |
| - type Error = anyhow::Error; |
| 58 | +impl Decode for i64 { |
| 59 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error> { |
| 60 | + match value { |
| 61 | + DbValue::Int64(n) => Ok(*n), |
| 62 | + _ => Err(anyhow::anyhow!( |
| 63 | + "Expected BIGINT from the DB but got {:?}", |
| 64 | + value |
| 65 | + )), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
23 | 69 |
|
24 |
| - fn try_from(value: &DbValue) -> Result<Self, Self::Error> { |
| 70 | +impl Decode for f32 { |
| 71 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error> { |
25 | 72 | match value {
|
26 |
| - DbValue::Str(s) => Ok(s.to_owned()), |
| 73 | + DbValue::Floating32(n) => Ok(*n), |
| 74 | + _ => Err(anyhow::anyhow!( |
| 75 | + "Expected REAL from the DB but got {:?}", |
| 76 | + value |
| 77 | + )), |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl Decode for f64 { |
| 83 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error> { |
| 84 | + match value { |
| 85 | + DbValue::Floating64(n) => Ok(*n), |
27 | 86 | _ => Err(anyhow::anyhow!(
|
28 |
| - "Expected string from the DB but got {:?}", |
| 87 | + "Expected DOUBLE PRECISION from the DB but got {:?}", |
29 | 88 | value
|
30 | 89 | )),
|
31 | 90 | }
|
32 | 91 | }
|
33 | 92 | }
|
34 | 93 |
|
35 |
| -impl TryFrom<&DbValue> for i64 { |
36 |
| - type Error = anyhow::Error; |
| 94 | +impl Decode for Vec<u8> { |
| 95 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error> { |
| 96 | + match value { |
| 97 | + DbValue::Binary(n) => Ok(n.to_owned()), |
| 98 | + _ => Err(anyhow::anyhow!( |
| 99 | + "Expected BYTEA from the DB but got {:?}", |
| 100 | + value |
| 101 | + )), |
| 102 | + } |
| 103 | + } |
| 104 | +} |
37 | 105 |
|
38 |
| - fn try_from(value: &DbValue) -> Result<Self, Self::Error> { |
| 106 | +impl Decode for String { |
| 107 | + fn decode(value: &DbValue) -> Result<Self, anyhow::Error> { |
39 | 108 | match value {
|
40 |
| - DbValue::Int64(n) => Ok(*n), |
| 109 | + DbValue::Str(s) => Ok(s.to_owned()), |
41 | 110 | _ => Err(anyhow::anyhow!(
|
42 |
| - "Expected integer from the DB but got {:?}", |
| 111 | + "Expected CHAR, VARCHAR, TEXT from the DB but got {:?}", |
43 | 112 | value
|
44 | 113 | )),
|
45 | 114 | }
|
46 | 115 | }
|
47 | 116 | }
|
| 117 | + |
| 118 | +#[cfg(test)] |
| 119 | +mod tests { |
| 120 | + use super::*; |
| 121 | + |
| 122 | + #[test] |
| 123 | + fn boolean() { |
| 124 | + assert!(bool::decode(&DbValue::Boolean(true)).unwrap()); |
| 125 | + assert!(bool::decode(&DbValue::Int32(0)).is_err()); |
| 126 | + assert!(Option::<bool>::decode(&DbValue::DbNull).unwrap().is_none()); |
| 127 | + } |
| 128 | + |
| 129 | + #[test] |
| 130 | + fn int16() { |
| 131 | + assert_eq!(i16::decode(&DbValue::Int16(0)).unwrap(), 0); |
| 132 | + assert!(i16::decode(&DbValue::Int32(0)).is_err()); |
| 133 | + assert!(Option::<i16>::decode(&DbValue::DbNull).unwrap().is_none()); |
| 134 | + } |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn int32() { |
| 138 | + assert_eq!(i32::decode(&DbValue::Int32(0)).unwrap(), 0); |
| 139 | + assert!(i32::decode(&DbValue::Boolean(false)).is_err()); |
| 140 | + assert!(Option::<i32>::decode(&DbValue::DbNull).unwrap().is_none()); |
| 141 | + } |
| 142 | + |
| 143 | + #[test] |
| 144 | + fn int64() { |
| 145 | + assert_eq!(i64::decode(&DbValue::Int64(0)).unwrap(), 0); |
| 146 | + assert!(i64::decode(&DbValue::Boolean(false)).is_err()); |
| 147 | + assert!(Option::<i64>::decode(&DbValue::DbNull).unwrap().is_none()); |
| 148 | + } |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn floating32() { |
| 152 | + assert!(f32::decode(&DbValue::Floating32(0.0)).is_ok()); |
| 153 | + assert!(f32::decode(&DbValue::Boolean(false)).is_err()); |
| 154 | + assert!(Option::<f32>::decode(&DbValue::DbNull).unwrap().is_none()); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn floating64() { |
| 159 | + assert!(f64::decode(&DbValue::Floating64(0.0)).is_ok()); |
| 160 | + assert!(f64::decode(&DbValue::Boolean(false)).is_err()); |
| 161 | + assert!(Option::<f64>::decode(&DbValue::DbNull).unwrap().is_none()); |
| 162 | + } |
| 163 | + |
| 164 | + #[test] |
| 165 | + fn str() { |
| 166 | + assert_eq!( |
| 167 | + String::decode(&DbValue::Str(String::from("foo"))).unwrap(), |
| 168 | + String::from("foo") |
| 169 | + ); |
| 170 | + |
| 171 | + assert!(String::decode(&DbValue::Int32(0)).is_err()); |
| 172 | + assert!(Option::<String>::decode(&DbValue::DbNull) |
| 173 | + .unwrap() |
| 174 | + .is_none()); |
| 175 | + } |
| 176 | + |
| 177 | + #[test] |
| 178 | + fn binary() { |
| 179 | + assert!(Vec::<u8>::decode(&DbValue::Binary(vec![0, 0])).is_ok()); |
| 180 | + assert!(Vec::<u8>::decode(&DbValue::Boolean(false)).is_err()); |
| 181 | + assert!(Option::<Vec<u8>>::decode(&DbValue::DbNull) |
| 182 | + .unwrap() |
| 183 | + .is_none()); |
| 184 | + } |
| 185 | +} |
0 commit comments