Skip to content

Commit

Permalink
Merge pull request #134 from thed0ct0r/features/support_full_int_rang…
Browse files Browse the repository at this point in the history
…e_for_bool

added: parse integers to bool
  • Loading branch information
blackbeam authored Apr 28, 2024
2 parents b2b7b01 + 227c7f2 commit dea49cc
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/value/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ impl TryFrom<Value> for ParseIrOpt<bool> {

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

#[test]
fn parse_int_as_bool(n: i64) {
let val = Value::Int(n);
if n == 0 {
assert_eq!(from_value::<bool>(val), false);
} else {
assert_eq!(from_value::<bool>(val), true);
}
}

#[test]
fn parse_uint_as_bool(n: u64) {
let val = Value::UInt(n);
if n == 0 {
assert_eq!(from_value::<bool>(val), false);
} else {
assert_eq!(from_value::<bool>(val), true);
}
}

#[test]
fn i128_roundtrip(
bytes_pos in r"16[0-9]{37}",
Expand Down

0 comments on commit dea49cc

Please sign in to comment.