mysql_common/value/convert/
uuid.rs
1use std::convert::TryFrom;
12
13use uuid::Uuid;
14
15use crate::value::Value;
16
17use super::{FromValue, FromValueError, ParseIr};
18
19impl From<Uuid> for Value {
20 fn from(uuid: Uuid) -> Value {
21 Value::Bytes(uuid.as_bytes().to_vec())
22 }
23}
24
25impl TryFrom<Value> for ParseIr<Uuid> {
26 type Error = FromValueError;
27
28 fn try_from(v: Value) -> Result<Self, Self::Error> {
29 match v {
30 Value::Bytes(ref bytes) => match Uuid::from_slice(bytes.as_slice()) {
31 Ok(val) => Ok(ParseIr(val, v)),
32 Err(_) => Err(FromValueError(v)),
33 },
34 v => Err(FromValueError(v)),
35 }
36 }
37}
38
39impl From<ParseIr<Uuid>> for Uuid {
40 fn from(value: ParseIr<Uuid>) -> Self {
41 value.commit()
42 }
43}
44
45impl From<ParseIr<Uuid>> for Value {
46 fn from(value: ParseIr<Uuid>) -> Self {
47 value.rollback()
48 }
49}
50
51impl FromValue for Uuid {
52 type Intermediate = ParseIr<Uuid>;
53}