qdrant_client/
error.rs

1#![allow(deprecated)]
2
3use std::error::Error;
4use std::fmt::{Debug, Display, Formatter};
5use std::marker::PhantomData;
6
7use crate::qdrant::value::Kind::*;
8use crate::qdrant::{ListValue, Struct, Value};
9
10/// An error for failed conversions (e.g. calling [`String::try_from(v)`](String::try_from) on an
11/// integer [`Value`])
12#[deprecated(since = "1.10.0", note = "new functions don't use this type anymore")]
13pub struct NotA<T> {
14    marker: PhantomData<T>,
15}
16
17impl<T> Default for NotA<T> {
18    fn default() -> Self {
19        NotA {
20            marker: PhantomData,
21        }
22    }
23}
24impl Error for NotA<Struct> {}
25
26impl Debug for NotA<Struct> {
27    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
28        write!(f, "{self}")
29    }
30}
31
32impl Display for NotA<Struct> {
33    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
34        f.write_str("not a Struct")
35    }
36}
37
38impl Error for NotA<ListValue> {}
39
40impl Debug for NotA<ListValue> {
41    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42        write!(f, "{self}")
43    }
44}
45
46impl Display for NotA<ListValue> {
47    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48        f.write_str("not a ListValue")
49    }
50}
51
52// Error + Conversion impl for bool
53impl Error for NotA<bool> {}
54
55impl Debug for NotA<bool> {
56    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
57        write!(f, "{self}")
58    }
59}
60
61impl Display for NotA<bool> {
62    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
63        f.write_str(concat!("not a bool"))
64    }
65}
66
67impl TryFrom<Value> for bool {
68    type Error = NotA<bool>;
69
70    fn try_from(v: Value) -> Result<Self, NotA<bool>> {
71        if let Some(BoolValue(t)) = v.kind {
72            Ok(t)
73        } else {
74            Err(NotA::default())
75        }
76    }
77}
78
79// Error + Conversion impl for i64
80impl Error for NotA<i64> {}
81
82impl Debug for NotA<i64> {
83    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
84        write!(f, "{self}")
85    }
86}
87
88impl Display for NotA<i64> {
89    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
90        f.write_str(concat!("not an i64"))
91    }
92}
93
94impl TryFrom<Value> for i64 {
95    type Error = NotA<i64>;
96
97    fn try_from(v: Value) -> Result<Self, NotA<i64>> {
98        if let Some(IntegerValue(t)) = v.kind {
99            Ok(t)
100        } else {
101            Err(NotA::default())
102        }
103    }
104}
105
106// Error + Conversion impl for f64
107impl Error for NotA<f64> {}
108
109impl Debug for NotA<f64> {
110    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
111        write!(f, "{self}")
112    }
113}
114
115impl Display for NotA<f64> {
116    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
117        f.write_str(concat!("not a f64"))
118    }
119}
120
121impl TryFrom<Value> for f64 {
122    type Error = NotA<f64>;
123
124    fn try_from(v: Value) -> Result<Self, NotA<f64>> {
125        if let Some(DoubleValue(t)) = v.kind {
126            Ok(t)
127        } else {
128            Err(NotA::default())
129        }
130    }
131}
132
133// Error + Conversion impl for string
134impl Error for NotA<String> {}
135
136impl Debug for NotA<String> {
137    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
138        write!(f, "{self}")
139    }
140}
141
142impl Display for NotA<String> {
143    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
144        f.write_str(concat!("not a String"))
145    }
146}
147
148impl TryFrom<Value> for String {
149    type Error = NotA<String>;
150
151    fn try_from(v: Value) -> Result<Self, NotA<String>> {
152        if let Some(StringValue(t)) = v.kind {
153            Ok(t)
154        } else {
155            Err(NotA::default())
156        }
157    }
158}