tidy the db_ids a bit
This commit is contained in:
parent
656e6dceed
commit
4211ead59e
1 changed files with 10 additions and 16 deletions
26
src/db_id.rs
26
src/db_id.rs
|
@ -28,8 +28,7 @@ impl DbId {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let id = Ulid::new();
|
Self(Ulid::new())
|
||||||
Self(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_str(s: &str) -> Result<Self, ulid::DecodeError> {
|
pub fn from_str(s: &str) -> Result<Self, ulid::DecodeError> {
|
||||||
|
@ -76,7 +75,6 @@ impl sqlx::Type<sqlx::Sqlite> for DbId {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sqlx traits for marshalling in and out
|
|
||||||
impl<'q> Encode<'q, Sqlite> for DbId {
|
impl<'q> Encode<'q, Sqlite> for DbId {
|
||||||
fn encode(self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
|
fn encode(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())));
|
||||||
|
@ -104,8 +102,7 @@ impl<'r> Decode<'r, Sqlite> for DbId {
|
||||||
fn decode(value: SqliteValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
|
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();
|
||||||
let id: Ulid = u128::from_ne_bytes(bytes).into();
|
Ok(u128::from_ne_bytes(bytes).into())
|
||||||
Ok(id.into())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,14 +131,14 @@ impl<'de> Visitor<'de> for DbIdVisitor {
|
||||||
where
|
where
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
Ok(DbId(Ulid(v as u128)))
|
Ok((v as u128).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
|
fn visit_u128<E>(self, v: u128) -> Result<Self::Value, E>
|
||||||
where
|
where
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
Ok(DbId(Ulid(v)))
|
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>
|
||||||
|
@ -149,7 +146,7 @@ impl<'de> Visitor<'de> for DbIdVisitor {
|
||||||
E: serde::de::Error,
|
E: serde::de::Error,
|
||||||
{
|
{
|
||||||
match std::convert::TryInto::<[u8; 16]>::try_into(v) {
|
match std::convert::TryInto::<[u8; 16]>::try_into(v) {
|
||||||
Ok(v) => Ok(DbId(Ulid(u128::from_be_bytes(v)))),
|
Ok(v) => Ok(u128::from_be_bytes(v).into()),
|
||||||
Err(_) => Err(serde::de::Error::invalid_length(v.len(), &self)),
|
Err(_) => Err(serde::de::Error::invalid_length(v.len(), &self)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -160,7 +157,7 @@ impl<'de> Visitor<'de> for DbIdVisitor {
|
||||||
{
|
{
|
||||||
let len = v.len();
|
let len = v.len();
|
||||||
match std::convert::TryInto::<[u8; 16]>::try_into(v) {
|
match std::convert::TryInto::<[u8; 16]>::try_into(v) {
|
||||||
Ok(v) => Ok(DbId(Ulid(u128::from_be_bytes(v)))),
|
Ok(v) => Ok(u128::from_be_bytes(v).into()),
|
||||||
Err(_) => Err(serde::de::Error::invalid_length(len, &self)),
|
Err(_) => Err(serde::de::Error::invalid_length(len, &self)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -199,7 +196,7 @@ impl<'de> Visitor<'de> for DbIdVisitor {
|
||||||
let size = seq.size_hint().unwrap_or(0);
|
let size = seq.size_hint().unwrap_or(0);
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
while let Some(val) = seq.next_element()? {
|
while let Some(val) = seq.next_element()? {
|
||||||
if count >= 16 {
|
if count > 15 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
bytes[count] = val;
|
bytes[count] = val;
|
||||||
|
@ -209,8 +206,7 @@ impl<'de> Visitor<'de> for DbIdVisitor {
|
||||||
let sz = if count < 16 { count } else { size };
|
let sz = if count < 16 { count } else { size };
|
||||||
Err(serde::de::Error::invalid_length(sz, &self))
|
Err(serde::de::Error::invalid_length(sz, &self))
|
||||||
} else {
|
} else {
|
||||||
let id = u128::from_ne_bytes(bytes);
|
Ok(u128::from_ne_bytes(bytes).into())
|
||||||
Ok(id.into())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,12 +233,10 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ser_de() {
|
fn test_ser_de() {
|
||||||
let bytes: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
let bytes: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
|
||||||
let be_num = u128::from_be_bytes(bytes);
|
let id: DbId = u128::from_be_bytes(bytes).into();
|
||||||
let le_ulid = Ulid(be_num);
|
|
||||||
let le_id = DbId(le_ulid);
|
|
||||||
|
|
||||||
assert_tokens(
|
assert_tokens(
|
||||||
&le_id,
|
&id,
|
||||||
&[Token::Bytes(&[
|
&[Token::Bytes(&[
|
||||||
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
||||||
])],
|
])],
|
||||||
|
|
Loading…
Reference in a new issue