clickhouse_client/value/ext/
x256.rs

1//! U256 and I256 integers
2
3use crate::{
4    error::Error,
5    value::{ChValue, Type, Value},
6};
7
8pub use ethnum::{I256, U256};
9
10// -- U256 --
11impl ChValue for U256 {
12    fn ch_type() -> Type {
13        Type::UInt256
14    }
15
16    fn into_ch_value(self) -> Value {
17        Value::UInt256(self.into_words().into())
18    }
19
20    fn from_ch_value(value: Value) -> Result<Self, Error> {
21        match value {
22            Value::UInt256(v) => Ok(U256::from_words(v[0], v[1])),
23            _ => Err(crate::error::Error::new(
24                "Cannot convert Value to base type",
25            )),
26        }
27    }
28}
29
30impl ChValue for Option<U256> {
31    fn ch_type() -> Type {
32        Type::NullableUInt256
33    }
34
35    fn into_ch_value(self) -> Value {
36        match self {
37            Some(v) => Value::NullableUInt256(Some(v.into_words().into())),
38            None => Value::NullableUInt256(None),
39        }
40    }
41
42    fn from_ch_value(value: Value) -> Result<Self, Error> {
43        match value {
44            Value::NullableUInt256(v) => Ok(v.map(|x| U256::from_words(x[0], x[1]))),
45            _ => Err(crate::error::Error::new(
46                "Cannot convert Value to base type",
47            )),
48        }
49    }
50}
51
52// -- I256 --
53impl ChValue for I256 {
54    fn ch_type() -> Type {
55        Type::Int256
56    }
57
58    fn into_ch_value(self) -> Value {
59        Value::Int256(self.into_words().into())
60    }
61
62    fn from_ch_value(value: Value) -> Result<Self, Error> {
63        match value {
64            Value::Int256(v) => Ok(I256::from_words(v[0], v[1])),
65            _ => Err(crate::error::Error::new(
66                "Cannot convert Value to base type",
67            )),
68        }
69    }
70}
71
72impl ChValue for Option<I256> {
73    fn ch_type() -> Type {
74        Type::NullableInt256
75    }
76
77    fn into_ch_value(self) -> Value {
78        match self {
79            Some(v) => Value::NullableInt256(Some(v.into_words().into())),
80            None => Value::NullableInt256(None),
81        }
82    }
83
84    fn from_ch_value(value: Value) -> Result<Self, Error> {
85        match value {
86            Value::NullableInt256(v) => Ok(v.map(|x| I256::from_words(x[0], x[1]))),
87            _ => Err(crate::error::Error::new(
88                "Cannot convert Value to base type",
89            )),
90        }
91    }
92}