Skip to content

Commit dea49cc

Browse files
authored
Merge pull request #134 from thed0ct0r/features/support_full_int_range_for_bool
added: parse integers to bool
2 parents b2b7b01 + 227c7f2 commit dea49cc

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/value/convert/mod.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ impl TryFrom<Value> for ParseIrOpt<bool> {
233233

234234
fn try_from(v: Value) -> Result<Self, Self::Error> {
235235
match v {
236-
Value::Int(0) => Ok(ParseIrOpt::Ready(false)),
237-
Value::Int(1) => Ok(ParseIrOpt::Ready(true)),
236+
Value::Int(0) | Value::UInt(0) => Ok(ParseIrOpt::Ready(false)),
237+
Value::Int(_) | Value::UInt(_) => Ok(ParseIrOpt::Ready(true)),
238238
Value::Bytes(ref bytes) => match bytes.as_slice() {
239239
[b'0'] => Ok(ParseIrOpt::Parsed(false, v)),
240240
[b'1'] => Ok(ParseIrOpt::Parsed(true, v)),
@@ -1012,6 +1012,26 @@ mod tests {
10121012
let _ = super::time02::parse_mysql_datetime_string_with_time(s.as_bytes());
10131013
}
10141014

1015+
#[test]
1016+
fn parse_int_as_bool(n: i64) {
1017+
let val = Value::Int(n);
1018+
if n == 0 {
1019+
assert_eq!(from_value::<bool>(val), false);
1020+
} else {
1021+
assert_eq!(from_value::<bool>(val), true);
1022+
}
1023+
}
1024+
1025+
#[test]
1026+
fn parse_uint_as_bool(n: u64) {
1027+
let val = Value::UInt(n);
1028+
if n == 0 {
1029+
assert_eq!(from_value::<bool>(val), false);
1030+
} else {
1031+
assert_eq!(from_value::<bool>(val), true);
1032+
}
1033+
}
1034+
10151035
#[test]
10161036
fn i128_roundtrip(
10171037
bytes_pos in r"16[0-9]{37}",

0 commit comments

Comments
 (0)