Skip to content

Commit b906fdc

Browse files
committed
chore: encode return error instread of panic
1 parent 4dee859 commit b906fdc

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ pub trait MysqlShim<W: Read + Write> {
207207
}
208208

209209
/// Information about an authenticated user
210-
#[derive(Debug, Default, Clone, PartialEq, Eq)]
210+
#[allow(clippy::derive_partial_eq_without_eq)]
211+
#[derive(Debug, Default, Clone, PartialEq)]
211212
pub struct AuthenticationContext<'a> {
212213
/// The username exactly as passed by the client,
213214
pub username: Option<Vec<u8>>,

src/value/encode.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::myc::constants::{ColumnFlags, ColumnType};
22
use crate::myc::io::WriteMysqlExt;
33
use crate::Column;
44
use byteorder::{LittleEndian, WriteBytesExt};
5-
use std::io::{self, Write};
5+
use std::io::{self, ErrorKind::Other, Write};
66

77
/// Implementors of this trait can be sent as a single resultset value to a MySQL/MariaDB client.
88
pub trait ToMysqlValue {
@@ -564,17 +564,21 @@ impl ToMysqlValue for myc::value::Value {
564564
myc::value::Value::Double(f) => f.to_mysql_text(w),
565565
myc::value::Value::Date(y, mo, d, h, mi, s, us) => {
566566
NaiveDate::from_ymd_opt(i32::from(y), u32::from(mo), u32::from(d))
567-
.unwrap_or_else(|| panic!("invalid date: y {} mo {} d {}", y, mo, d))
567+
.ok_or_else(|| {
568+
io::Error::new(Other, format!("invalid date: y {} mo {} d {}", y, mo, d))
569+
})?
568570
.and_hms_micro_opt(u32::from(h), u32::from(mi), u32::from(s), us)
569-
.unwrap_or_else(|| panic!("invalid date: h {} mi {} s {} us {}", h, mi, s, us))
571+
.ok_or_else(|| {
572+
io::Error::new(
573+
Other,
574+
format!("invalid date: h {} mi {} s {} us {}", h, mi, s, us),
575+
)
576+
})?
570577
.to_mysql_text(w)
571578
}
572579
myc::value::Value::Time(neg, d, h, m, s, us) => {
573580
if neg {
574-
return Err(io::Error::new(
575-
io::ErrorKind::Other,
576-
"negative times not yet supported",
577-
));
581+
return Err(io::Error::new(Other, "negative times not yet supported"));
578582
}
579583
(chrono::Duration::days(i64::from(d))
580584
+ chrono::Duration::hours(i64::from(h))
@@ -631,17 +635,21 @@ impl ToMysqlValue for myc::value::Value {
631635
myc::value::Value::Double(f) => f.to_mysql_bin(w, c),
632636
myc::value::Value::Date(y, mo, d, h, mi, s, us) => {
633637
NaiveDate::from_ymd_opt(i32::from(y), u32::from(mo), u32::from(d))
634-
.unwrap_or_else(|| panic!("invalid date: y {} mo {} d {}", y, mo, d))
638+
.ok_or_else(|| {
639+
io::Error::new(Other, format!("invalid date: y {} mo {} d {}", y, mo, d))
640+
})?
635641
.and_hms_micro_opt(u32::from(h), u32::from(mi), u32::from(s), us)
636-
.unwrap_or_else(|| panic!("invalid date: h {} mi {} s {} us {}", h, mi, s, us))
642+
.ok_or_else(|| {
643+
io::Error::new(
644+
Other,
645+
format!("invalid date: h {} mi {} s {} us {}", h, mi, s, us),
646+
)
647+
})?
637648
.to_mysql_bin(w, c)
638649
}
639650
myc::value::Value::Time(neg, d, h, m, s, us) => {
640651
if neg {
641-
return Err(io::Error::new(
642-
io::ErrorKind::Other,
643-
"negative times not yet supported",
644-
));
652+
return Err(io::Error::new(Other, "negative times not yet supported"));
645653
}
646654
(chrono::Duration::days(i64::from(d))
647655
+ chrono::Duration::hours(i64::from(h))

0 commit comments

Comments
 (0)