mysql_common/value/convert/
uuid.rs

1// Copyright (c) 2021 Anatoly Ikorsky
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9//! This module implements conversion from/to `Value` for `Uuid`.
10
11use 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}