Skip to main content

couchbase_core/queryx/
query_result.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use std::fmt::{Display, Formatter};
20use std::time::Duration;
21
22use serde::Deserialize;
23use serde_json::value::RawValue;
24
25#[derive(Debug, Clone, Copy, Deserialize, Ord, PartialOrd, Eq, PartialEq, Hash)]
26#[serde(rename_all = "lowercase")]
27#[non_exhaustive]
28pub enum Status {
29    Running,
30    Success,
31    Errors,
32    Completed,
33    Stopped,
34    Timeout,
35    Closed,
36    Fatal,
37    Aborted,
38    Unknown,
39}
40
41impl Display for Status {
42    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
43        match self {
44            Status::Running => write!(f, "running"),
45            Status::Success => write!(f, "success"),
46            Status::Errors => write!(f, "errors"),
47            Status::Completed => write!(f, "completed"),
48            Status::Stopped => write!(f, "stopped"),
49            Status::Timeout => write!(f, "timeout"),
50            Status::Closed => write!(f, "closed"),
51            Status::Fatal => write!(f, "fatal"),
52            Status::Aborted => write!(f, "aborted"),
53            Status::Unknown => write!(f, "unknown"),
54        }
55    }
56}
57
58#[derive(Debug, Clone)]
59pub struct EarlyMetaData {
60    pub prepared: Option<String>,
61}
62
63#[derive(Debug, Clone)]
64pub struct MetaData {
65    pub prepared: Option<String>,
66    pub request_id: String,
67    pub client_context_id: String,
68    pub status: Status,
69    pub metrics: Option<Metrics>,
70    pub signature: Option<Box<RawValue>>,
71    pub warnings: Vec<Warning>,
72    pub profile: Option<Box<RawValue>>,
73}
74
75impl Display for MetaData {
76    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77        write!(
78            f,
79            "prepared: {:?}, request_id: {}, client_context_id: {}, status: {}, metrics: {:?}, signature: {:?}, warnings: {:?}, profile: {:?}",
80            self.prepared,
81            self.request_id,
82            self.client_context_id,
83            self.status,
84            self.metrics,
85            self.signature,
86            self.warnings,
87            self.profile
88        )
89    }
90}
91
92#[derive(Debug, Clone)]
93pub struct Warning {
94    pub code: u32,
95    pub message: String,
96}
97
98impl Display for Warning {
99    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
100        write!(f, "code: {}, message: {}", self.code, self.message)
101    }
102}
103
104#[derive(Default, Debug, Clone)]
105pub struct Metrics {
106    pub elapsed_time: Duration,
107    pub execution_time: Duration,
108    pub result_count: u64,
109    pub result_size: u64,
110    pub mutation_count: u64,
111    pub sort_count: u64,
112    pub error_count: u64,
113    pub warning_count: u64,
114}
115
116impl Display for Metrics {
117    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
118        write!(
119            f,
120            "elapsed_time: {:?}, execution_time: {:?}, result_count: {}, result_size: {}, mutation_count: {}, sort_count: {}, error_count: {}, warning_count: {}",
121            self.elapsed_time,
122            self.execution_time,
123            self.result_count,
124            self.result_size,
125            self.mutation_count,
126            self.sort_count,
127            self.error_count,
128            self.warning_count
129        )
130    }
131}