1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
extern crate rustc_serialize;
extern crate uuid;
use rustc_serialize::base64::ToBase64;
use rustc_serialize::base64::FromBase64;
use rustc_serialize::base64::URL_SAFE;
use rustc_serialize::base64::FromBase64Error;
use uuid::ParseError;


use uuid::Uuid;

#[derive(Debug)]
pub enum ConvertError{
    ParseError(ParseError),
    FromBase64Error(FromBase64Error)
}

pub trait ToBlob{
    fn to_blob(&self) -> String;
}

impl ToBlob for Uuid{
    fn to_blob(&self) -> String{
        self.as_bytes().to_base64(URL_SAFE)
    }
}

pub trait FromBlob{
    fn to_uuid(&self) -> Result<Uuid, ConvertError>;
}

impl FromBlob for str{
    fn to_uuid(&self) -> Result<Uuid, ConvertError>{
        match self.from_base64(){
            Ok(bytes) => {
                match Uuid::from_bytes(&bytes){
                    Ok(uuid) => Ok(uuid),
                    Err(e) => Err(ConvertError::ParseError(e))
                }
            },
            Err(e) => {
                Err(ConvertError::FromBase64Error(e))
            }
        }
    }
}

#[test]
fn test_blobs() {
    let uuid_str = "557c8018-5e21-4b74-8bb0-9040e2e8ead1";
    println!("uuid_str: {}", uuid_str);
    let blob = "VXyAGF4hS3SLsJBA4ujq0Q";
    println!("blob: {}", blob);
    let uuid = Uuid::parse_str(uuid_str).unwrap();
    assert_eq!(uuid, blob.to_uuid().unwrap());
    assert_eq!(blob, uuid.to_blob());
}


#[test]
fn test_more_blobs(){
    for _ in 0..10{
        let uuid = Uuid::new_v4();
        let blob = uuid.to_blob();
        assert_eq!(blob.chars().count(), 22);
        println!("{}   |   {}",uuid,blob);
    }
    //panic!();
}

pub fn uuid_to_blob(uuid: &Uuid) -> String{
    uuid.to_blob()
}

pub fn blob_to_uuid(blob: &str) -> Result<Uuid, ConvertError>{
    blob.to_uuid()
}