clean up serde code during blog post writing

This commit is contained in:
Joe Ardent 2023-06-28 09:51:15 -07:00
parent b89d463e50
commit fc2d575e18
2 changed files with 8 additions and 60 deletions

View file

@ -35,6 +35,10 @@ impl DbId {
let id = Ulid::from_string(s)?; let id = Ulid::from_string(s)?;
Ok(id.into()) Ok(id.into())
} }
pub fn as_string(&self) -> String {
self.0.to_string()
}
} }
//-************************************************************************ //-************************************************************************
@ -76,30 +80,14 @@ impl sqlx::Type<sqlx::Sqlite> for DbId {
} }
impl<'q> Encode<'q, Sqlite> for DbId { impl<'q> Encode<'q, Sqlite> for DbId {
fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
args.push(SqliteArgumentValue::Blob(Cow::Owned(self.bytes().to_vec())));
IsNull::No
}
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull { fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
args.push(SqliteArgumentValue::Blob(Cow::Owned(self.bytes().to_vec()))); args.push(SqliteArgumentValue::Blob(Cow::Owned(self.bytes().to_vec())));
IsNull::No IsNull::No
} }
fn produces(&self) -> Option<<Sqlite as sqlx::Database>::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 { impl Decode<'_, Sqlite> for DbId {
std::mem::size_of_val(self) fn decode(value: SqliteValueRef<'_>) -> Result<Self, sqlx::error::BoxDynError> {
}
}
impl<'r> Decode<'r, Sqlite> for DbId {
fn decode(value: SqliteValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
let bytes = <&[u8] as Decode<Sqlite>>::decode(value)?; let bytes = <&[u8] as Decode<Sqlite>>::decode(value)?;
let bytes: [u8; 16] = bytes.try_into().unwrap_or_default(); let bytes: [u8; 16] = bytes.try_into().unwrap_or_default();
Ok(u128::from_be_bytes(bytes).into()) Ok(u128::from_be_bytes(bytes).into())
@ -124,21 +112,7 @@ impl<'de> Visitor<'de> for DbIdVisitor {
type Value = DbId; type Value = DbId;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a 128-bit number") formatter.write_str("16 bytes")
}
fn visit_i128<E>(self, v: i128) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok((v as u128).into())
}
fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(v.into())
} }
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
@ -162,32 +136,6 @@ impl<'de> Visitor<'de> for DbIdVisitor {
} }
} }
fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
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<E>(self, v: &str) -> Result<Self::Value, E>
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<A>(self, mut seq: A) -> Result<Self::Value, A::Error> fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where where
A: serde::de::SeqAccess<'de>, A: serde::de::SeqAccess<'de>,

View file

@ -111,7 +111,7 @@ pub async fn post_create_user(
let user = create_user(username, &displayname, &email, password, &pool, id).await?; let user = create_user(username, &displayname, &email, password, &pool, id).await?;
tracing::debug!("created {user:?}"); tracing::debug!("created {user:?}");
let id = user.id.0.to_string(); let id = user.id.as_string();
let location = format!("/signup_success/{id}"); let location = format!("/signup_success/{id}");
let resp = axum::response::Redirect::to(&location); let resp = axum::response::Redirect::to(&location);