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
//! [`Mapping`] `enum` for different JSON [`Response`](crate::Response)
pub use Associative;
pub use Empty;
pub use Error;
pub use Execute;
pub use Standard;
pub use Timed;
/// [`Mapping`] is used in [`response::Result`](super::Result) and
/// [`Query::results()`](super::query::Query::results())
///
/// # Examples
///
/// Match your [`response::Result`](super::Result) to a concrete [`Response`](super::Response)
///
/// ```
/// use rqlite_client::response::{self, Result};
/// use rqlite_client::{Mapping, Response};
///
/// // // for description of the data structure
/// // let response_result: Result =
/// // Ok(Response::Query(response::query::Query {
/// // results: vec![Mapping::Error(response::mapping::Error {
/// // error: String::new(),
/// // })],
/// // sequence_number: None,
/// // time: None,
/// // }));
///
/// let response_result: Option<Result> = None;
///
/// if let Some(Ok(response_result)) = response_result {
/// if let Ok(query) = response::Query::try_from(response_result) {
/// for result in query.results() {
/// match result {
/// Mapping::Error(err) => err.to_string(),
/// _ => "no error".to_string(),
/// };
/// }
/// }
/// }
/// ```
///
/// Convert [`Result`] to an own data struct
///
/// ```no_run
/// use rqlite_client::response::{self, Query, Result};
/// use rqlite_client::response::mapping::Timed;
/// use rqlite_client::{Mapping, Response};
///
/// struct MyData {
/// timing: std::time::Duration,
/// }
///
/// impl TryFrom<&Mapping> for MyData {
/// type Error = rqlite_client::Error;
///
/// fn try_from(r: &Mapping) -> std::result::Result<Self, Self::Error> {
/// match r {
/// Mapping::Associative(r) => Ok(MyData {
/// timing: r.duration().unwrap(),
/// }),
/// Mapping::Standard(r) => Ok(MyData {
/// timing: r.duration().unwrap(),
/// }),
/// _ => Err(rqlite_client::Error::from("not expected MyData")),
/// }
/// }
/// }
///
/// let response_result: Option<Result> = None;
///
/// if let Some(Ok(response_result)) = response_result {
/// if let Ok(query) = response::Query::try_from(response_result) {
/// let my_data = query
/// .results()
/// .filter_map(|r| MyData::try_from(r).ok())
/// .collect::<Vec<MyData>>();
/// }
/// }
/// ```
///