Skip to content

Commit 227c7f2

Browse files
committed
added: parse integers to bool
the mysql specification at https://github.com/blackbeam/rust_mysql_common/issues/url states that all TinyInt values between -128 and 127 are valid as the boolean value true (with the exclusion of 0 being false). other clients have implemented the full range of their respective integer classes (e.g jdbc's Integer) conversions into boolean. this commit adds that simple logic with either Value::Int(0) and Value::Uint(0) being false and all other Value::Int's and Value::UInt's being true. proptests for both ranges were also added for sanity/regressions sake
1 parent b2b7b01 commit 227c7f2

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/value/convert/mod.rs

Lines changed: 22 additions & 2 deletions
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)