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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use super::*;

#[derive(Debug, Clone)]
pub enum Value {
  Null,
  Integer(i64),
  UInteger(u64),
  Real(f64),
  Text(String),
  Blob(Vec<u8>),
#[cfg(feature = "mysql")]
  MysqlValue(::mysql::Value),
}


#[cfg(feature = "mysql")]
impl std::convert::TryFrom<::mysql::Value> for Value {
  type Error = Error;
  fn try_from(v: ::mysql::Value) -> Result<Self> {
    Ok(Value::MysqlValue(v))
  }
}

pub trait TryFromValue {
  fn try_from(v: Value) -> Result<Self> where Self: Sized;
}

impl TryFromValue for Vec<u8> {
  fn try_from(v: Value) -> Result<Self> { 
    match v {
      Value::Blob(v) => Ok(v),
#[cfg(feature = "mysql")]
      Value::MysqlValue(v) => Ok(::mysql::from_value_opt(v)?),
      _ => Err(Error::InvalidTypeFor("Vec<u8>".to_string())),
    } 
  }
}

impl TryFromValue for bool {
  fn try_from(v: Value) -> Result<Self> { 
    match v {
      Value::Integer(v) => Ok(v != 0),
#[cfg(feature = "mysql")]
      Value::MysqlValue(v) => Ok(::mysql::from_value_opt(v)?),
      _ => Err(Error::InvalidTypeFor("bool".to_string())),
    }
  }
} 

impl TryFromValue for u32 {
  fn try_from(v: Value) -> Result<Self> { 
    match v {
      Value::UInteger(v) => Ok(v.try_into()?),
      Value::Integer(v)  => Ok(v.try_into()?),
#[cfg(feature = "mysql")]
      Value::MysqlValue(v) => Ok(::mysql::from_value_opt(v)?),
      _ => Err(Error::InvalidTypeForFrom("u32".to_string(), format!("{v:?}"))),
    }
  }
} 

impl TryFromValue for i64 {
  fn try_from(v: Value) -> Result<Self> { 
    match v {
      Value::Integer(v) => Ok(v),
#[cfg(feature = "mysql")]
      Value::MysqlValue(v) => Ok(::mysql::from_value_opt(v)?),
      _ => Err(Error::InvalidTypeFor("i64".to_string())),
    }
  }
}

impl TryFromValue for f64 {
  fn try_from(v: Value) -> Result<Self> { 
    match v {
      Value::Real(v) => Ok(v),
#[cfg(feature = "mysql")]
      Value::MysqlValue(v) => Ok(::mysql::from_value_opt(v)?),
      _ => Err(Error::InvalidTypeFor("f64".to_string())),
    }
  }
}

impl TryFromValue for String {
  fn try_from(v: Value) -> Result<Self> { 
    match v {
      Value::Text(v) => Ok(v),
      Value::Blob(v) => Ok(String::from_utf8(v)?),
#[cfg(feature = "mysql")]
      Value::MysqlValue(v) => Ok(::mysql::from_value_opt(v)?),
      _ => Err(Error::InvalidTypeForFrom("String".to_string(), format!("{v:?}"))),
    }
  }
} 

impl TryFromValue for chrono::naive::NaiveDate {
  fn try_from(v: Value) -> Result<Self> {
    match v {
      Value::Text(v)      => Ok(chrono::naive::NaiveDate::parse_from_str(v.as_str(), "%Y-%m-%d")?),
#[cfg(feature = "mysql")]
      Value::MysqlValue(v) => Ok(::mysql::from_value_opt(v)?),
      _ => Err(Error::InvalidTypeForFrom("NaiveDate".to_string(), format!("{v:?}"))),
    }
  }
}

impl TryFromValue for chrono::naive::NaiveDateTime {
  fn try_from(v: Value) -> Result<Self> {
    match v {
#[cfg(feature = "mysql")]
      Value::MysqlValue(v) => Ok(::mysql::from_value_opt(v)?),
      _ => Err(Error::InvalidTypeForFrom("NaiveDateTime".to_string(), format!("{v:?}"))),
    }
  }
}


impl<T> TryFromValue for Option<T> 
where T: TryFromValue,
{
  fn try_from(v: Value) -> Result<Self> { 
    match v {
      Value::Null    => Ok(None),
#[cfg(feature = "mysql")]
      Value::MysqlValue(::mysql::Value::NULL) => Ok(None),
      _              => Ok(Some(TryFromValue::try_from(v)?)),
    }
  }
} 

/// Return a row of results that can be queried to be converted into objects
pub trait Row {
  fn get_value(&self, i: usize)  -> Option<Result<Value>>;

  fn get<T>(&self, i: usize) -> Option<Result<T>>
  where T: TryFromValue,
  {
    self.get_value(i)
    .map(|v| v.and_then(|r| TryFromValue::try_from(r) ) )
  }
}

pub trait TryFromRefRow<R> 
where R: Row,
{
  fn try_from(r: &R) -> Result<Self> where Self: Sized;
}