From fc2d575e18fb532b87fe036b2f414c7808a63db2 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Wed, 28 Jun 2023 09:51:15 -0700 Subject: [PATCH] clean up serde code during blog post writing --- src/db_id.rs | 66 ++++++--------------------------------------------- src/signup.rs | 2 +- 2 files changed, 8 insertions(+), 60 deletions(-) diff --git a/src/db_id.rs b/src/db_id.rs index f5e51a8..305c678 100644 --- a/src/db_id.rs +++ b/src/db_id.rs @@ -35,6 +35,10 @@ impl DbId { let id = Ulid::from_string(s)?; Ok(id.into()) } + + pub fn as_string(&self) -> String { + self.0.to_string() + } } //-************************************************************************ @@ -76,30 +80,14 @@ impl sqlx::Type for DbId { } impl<'q> Encode<'q, Sqlite> for DbId { - fn encode(self, args: &mut Vec>) -> IsNull { - args.push(SqliteArgumentValue::Blob(Cow::Owned(self.bytes().to_vec()))); - IsNull::No - } - fn encode_by_ref(&self, args: &mut Vec>) -> IsNull { args.push(SqliteArgumentValue::Blob(Cow::Owned(self.bytes().to_vec()))); IsNull::No } - - fn produces(&self) -> Option<::TypeInfo> { - // `produces` is inherently a hook to allow database drivers to produce - // value-dependent type information; if the driver doesn't need this, it - // can leave this as `None` - None - } - - fn size_hint(&self) -> usize { - std::mem::size_of_val(self) - } } -impl<'r> Decode<'r, Sqlite> for DbId { - fn decode(value: SqliteValueRef<'r>) -> Result { +impl Decode<'_, Sqlite> for DbId { + fn decode(value: SqliteValueRef<'_>) -> Result { let bytes = <&[u8] as Decode>::decode(value)?; let bytes: [u8; 16] = bytes.try_into().unwrap_or_default(); Ok(u128::from_be_bytes(bytes).into()) @@ -124,21 +112,7 @@ impl<'de> Visitor<'de> for DbIdVisitor { type Value = DbId; fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { - formatter.write_str("a 128-bit number") - } - - fn visit_i128(self, v: i128) -> Result - where - E: serde::de::Error, - { - Ok((v as u128).into()) - } - - fn visit_u128(self, v: u128) -> Result - where - E: serde::de::Error, - { - Ok(v.into()) + formatter.write_str("16 bytes") } fn visit_bytes(self, v: &[u8]) -> Result @@ -162,32 +136,6 @@ impl<'de> Visitor<'de> for DbIdVisitor { } } - fn visit_string(self, v: String) -> Result - where - E: serde::de::Error, - { - match Ulid::from_string(&v) { - Ok(v) => Ok(DbId(v)), - Err(_) => Err(serde::de::Error::invalid_value( - serde::de::Unexpected::Str(&format!("could not convert {v} to a ULID")), - &self, - )), - } - } - - fn visit_str(self, v: &str) -> Result - where - E: serde::de::Error, - { - match Ulid::from_string(v) { - Ok(v) => Ok(DbId(v)), - Err(_) => Err(serde::de::Error::invalid_value( - serde::de::Unexpected::Str(&format!("could not convert {v} to a ULID")), - &self, - )), - } - } - fn visit_seq(self, mut seq: A) -> Result where A: serde::de::SeqAccess<'de>, diff --git a/src/signup.rs b/src/signup.rs index 4b9d96a..81943b5 100644 --- a/src/signup.rs +++ b/src/signup.rs @@ -111,7 +111,7 @@ pub async fn post_create_user( let user = create_user(username, &displayname, &email, password, &pool, id).await?; tracing::debug!("created {user:?}"); - let id = user.id.0.to_string(); + let id = user.id.as_string(); let location = format!("/signup_success/{id}"); let resp = axum::response::Redirect::to(&location);