Add Simple format for Uuid for MySQL & SQLite. (#2469)

* Add Simple format for Uuid for MySQL & SQLite.

Copy pasted the implementation for `Hyphenated` and adapt.

* Add uuid Simple docs for SQLite
This commit is contained in:
Midas Lambrichts
2023-05-04 22:14:06 +02:00
committed by GitHub
parent 003878698e
commit cbf8dd37e9
6 changed files with 79 additions and 2 deletions

View File

@@ -62,6 +62,7 @@
//! |---------------------------------------|------------------------------------------------------|
//! | `uuid::Uuid` | BLOB, TEXT |
//! | `uuid::fmt::Hyphenated` | TEXT |
//! | `uuid::fmt::Simple` | TEXT |
//!
//! ### [`json`](https://crates.io/crates/serde_json)
//!

View File

@@ -5,7 +5,10 @@ use crate::type_info::DataType;
use crate::types::Type;
use crate::{Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef};
use std::borrow::Cow;
use uuid::{fmt::Hyphenated, Uuid};
use uuid::{
fmt::{Hyphenated, Simple},
Uuid,
};
impl Type<Sqlite> for Uuid {
fn type_info() -> SqliteTypeInfo {
@@ -56,3 +59,26 @@ impl Decode<'_, Sqlite> for Hyphenated {
Ok(uuid?.hyphenated())
}
}
impl Type<Sqlite> for Simple {
fn type_info() -> SqliteTypeInfo {
SqliteTypeInfo(DataType::Text)
}
}
impl<'q> Encode<'q, Sqlite> for Simple {
fn encode_by_ref(&self, args: &mut Vec<SqliteArgumentValue<'q>>) -> IsNull {
args.push(SqliteArgumentValue::Text(Cow::Owned(self.to_string())));
IsNull::No
}
}
impl Decode<'_, Sqlite> for Simple {
fn decode(value: SqliteValueRef<'_>) -> Result<Self, BoxDynError> {
let uuid: Result<Uuid, BoxDynError> =
Uuid::parse_str(&value.text().map(ToOwned::to_owned)?).map_err(Into::into);
Ok(uuid?.simple())
}
}