Skip to content

Commit

Permalink
fix: signed integer into unsigned integer
Browse files Browse the repository at this point in the history
  • Loading branch information
laysakura committed Jun 20, 2022
1 parent f4245cb commit 7f97a5e
Showing 1 changed file with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of https://github.com/SpringQL/SpringQL which is licensed under MIT OR Apache-2.0. See file LICENSE-MIT or LICENSE-APACHE for full license details.

use anyhow::Context;
use anyhow::{anyhow, Context};

use crate::{
api::error::{Result, SpringError},
Expand Down Expand Up @@ -85,6 +85,39 @@ impl ToNnSqlValue for i64 {
}

impl SpringValue for u32 {
fn try_from_i16(v: &i16) -> Result<Self> {
if 0 <= *v {
Ok(*v as u32)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i16 value ({}) into u32",
v
)))
}
}

fn try_from_i32(v: &i32) -> Result<Self> {
if 0 <= *v {
Ok(*v as u32)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i32 value ({}) into u32",
v
)))
}
}

fn try_from_i64(v: &i64) -> Result<Self> {
if 0 <= *v && *v <= u32::MAX as i64 {
Ok(*v as u32)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i64 value ({}) into u32",
v
)))
}
}

fn try_from_u16(v: &u16) -> Result<Self> {
Ok(*v as u32)
}
Expand All @@ -106,6 +139,39 @@ impl ToNnSqlValue for u32 {
}

impl SpringValue for u64 {
fn try_from_i16(v: &i16) -> Result<Self> {
if 0 <= *v {
Ok(*v as u64)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i16 value ({}) into u64",
v
)))
}
}

fn try_from_i32(v: &i32) -> Result<Self> {
if 0 <= *v {
Ok(*v as u64)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i32 value ({}) into u64",
v
)))
}
}

fn try_from_i64(v: &i64) -> Result<Self> {
if 0 <= *v {
Ok(*v as u64)
} else {
Err(SpringError::Sql(anyhow!(
"cannot convert i32 value ({}) into u64",
v
)))
}
}

fn try_from_u16(v: &u16) -> Result<Self> {
Ok(*v as u64)
}
Expand Down

0 comments on commit 7f97a5e

Please sign in to comment.