Skip to content

Commit

Permalink
Merge pull request #1018 from BastiDood/str-to-sql-refactor
Browse files Browse the repository at this point in the history
refactor(types): improve readability of `impl ToSql for &str`
  • Loading branch information
sfackler authored Apr 7, 2023
2 parents 7dd6813 + 98abdf9 commit 8b9b5d0
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions postgres-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,28 +1012,20 @@ impl ToSql for Vec<u8> {

impl<'a> ToSql for &'a str {
fn to_sql(&self, ty: &Type, w: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Sync + Send>> {
match *ty {
ref ty if ty.name() == "ltree" => types::ltree_to_sql(self, w),
ref ty if ty.name() == "lquery" => types::lquery_to_sql(self, w),
ref ty if ty.name() == "ltxtquery" => types::ltxtquery_to_sql(self, w),
match ty.name() {
"ltree" => types::ltree_to_sql(self, w),
"lquery" => types::lquery_to_sql(self, w),
"ltxtquery" => types::ltxtquery_to_sql(self, w),
_ => types::text_to_sql(self, w),
}
Ok(IsNull::No)
}

fn accepts(ty: &Type) -> bool {
match *ty {
Type::VARCHAR | Type::TEXT | Type::BPCHAR | Type::NAME | Type::UNKNOWN => true,
ref ty
if (ty.name() == "citext"
|| ty.name() == "ltree"
|| ty.name() == "lquery"
|| ty.name() == "ltxtquery") =>
{
true
}
_ => false,
}
matches!(
*ty,
Type::VARCHAR | Type::TEXT | Type::BPCHAR | Type::NAME | Type::UNKNOWN
) || matches!(ty.name(), "citext" | "ltree" | "lquery" | "ltxtquery")
}

to_sql_checked!();
Expand Down

0 comments on commit 8b9b5d0

Please sign in to comment.