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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use std::convert::TryFrom;

use super::Primitive;
use crate::ast::CastType;

pub struct Error {
    val: Primitive,
    cast_type: CastType,
}

impl Error {
    pub fn message(&self) -> String {
        format!("No cast from {} to {}", self.val, self.cast_type)
    }
}

impl From<&Primitive> for String {
    fn from(p: &Primitive) -> Self {
        match p {
            Primitive::Int(i) => format!("{}", i),
            Primitive::Float(f) => format!("{}", f),
            Primitive::Str(s) => format!("{}", s),
            Primitive::Bool(b) => format!("{}", b),
            Primitive::Empty => "NULL".to_owned(),
        }
    }
}

impl TryFrom<&Primitive> for i64 {
    type Error = Error;

    fn try_from(p: &Primitive) -> Result<Self, Self::Error> {
        match p {
            Primitive::Bool(_) | Primitive::Empty => Err(Error {
                val: p.clone(),
                cast_type: CastType::Int,
            }),
            Primitive::Str(s) => match s.parse::<i64>() {
                Ok(i) => Ok(i),
                Err(_) => Err(Error {
                    val: Primitive::Str(s.clone()),
                    cast_type: CastType::Int,
                }),
            },
            Primitive::Float(f) => Ok(*f as i64),
            Primitive::Int(i) => Ok(i.clone()),
        }
    }
}

impl TryFrom<&Primitive> for f64 {
    type Error = Error;

    fn try_from(p: &Primitive) -> Result<Self, Self::Error> {
        match p {
            Primitive::Bool(_) | Primitive::Empty => Err(Error {
                val: p.clone(),
                cast_type: CastType::Int,
            }),
            Primitive::Str(s) => match s.parse::<f64>() {
                Ok(f) => Ok(f),
                Err(_) => Err(Error {
                    val: Primitive::Str(s.clone()),
                    cast_type: CastType::Int,
                }),
            },
            Primitive::Int(i) => Ok(*i as f64),
            Primitive::Float(f) => Ok(f.clone()),
        }
    }
}

impl TryFrom<&Primitive> for bool {
    type Error = Error;

    fn try_from(p: &Primitive) -> Result<Self, Self::Error> {
        match p {
            Primitive::Empty | Primitive::Int(_) | Primitive::Float(_) => Err(Error {
                val: p.clone(),
                cast_type: CastType::Int,
            }),
            Primitive::Bool(b) => Ok(*b),
            Primitive::Str(s) => match s.as_str() {
                "true" => Ok(true),
                "false" => Ok(false),
                _ => Err(Error {
                    val: Primitive::Str(s.clone()),
                    cast_type: CastType::Int,
                }),
            },
        }
    }
}