typedb_driver/answer/
mod.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20use std::{fmt, sync::Arc};
21
22use futures::StreamExt;
23
24pub use self::{concept_document::ConceptDocument, concept_row::ConceptRow, json::JSON};
25use crate::{
26    answer::{concept_document::ConceptDocumentHeader, concept_row::ConceptRowHeader},
27    BoxStream, Result,
28};
29
30pub mod concept_document;
31pub mod concept_row;
32mod json;
33
34pub enum QueryAnswer {
35    Ok(QueryType),
36    ConceptRowStream(Arc<ConceptRowHeader>, BoxStream<'static, Result<ConceptRow>>),
37    ConceptDocumentStream(Arc<ConceptDocumentHeader>, BoxStream<'static, Result<ConceptDocument>>),
38}
39
40impl QueryAnswer {
41    /// Retrieve the executed query's type (shared by all elements in this stream).
42    ///
43    /// # Examples
44    ///
45    /// ```rust
46    /// query_answer.get_query_type()
47    /// ```
48    pub fn get_query_type(&self) -> QueryType {
49        match &self {
50            QueryAnswer::Ok(query_type) => query_type.clone(),
51            QueryAnswer::ConceptRowStream(header, _) => header.query_type,
52            QueryAnswer::ConceptDocumentStream(header, _) => header.query_type,
53        }
54    }
55
56    /// Check if the <code>QueryAnswer</code> is an <code>Ok</code> response.
57    ///
58    /// # Examples
59    ///
60    /// ```rust
61    /// query_answer.is_ok()
62    /// ```
63    pub fn is_ok(&self) -> bool {
64        matches!(self, Self::Ok(_))
65    }
66
67    /// Check if the <code>QueryAnswer</code> is a <code>ConceptRowStream</code>.
68    ///
69    /// # Examples
70    ///
71    /// ```rust
72    /// query_answer.is_row_stream()
73    /// ```
74    pub fn is_row_stream(&self) -> bool {
75        matches!(self, Self::ConceptRowStream(_, _))
76    }
77
78    /// Check if the <code>QueryAnswer</code> is a <code>ConceptDocumentStream</code>.
79    ///
80    /// # Examples
81    ///
82    /// ```rust
83    /// query_answer.is_document_stream()
84    /// ```
85    pub fn is_document_stream(&self) -> bool {
86        matches!(self, Self::ConceptDocumentStream(_, _))
87    }
88
89    /// Unwraps the <code>QueryAnswer</code> into a <code>ConceptRowStream</code>.
90    /// Panics if it is not a <code>ConceptRowStream</code>.
91    ///
92    /// # Examples
93    ///
94    /// ```rust
95    /// query_answer.into_rows()
96    /// ```
97    pub fn into_rows(self) -> BoxStream<'static, Result<ConceptRow>> {
98        if let Self::ConceptRowStream(_, stream) = self {
99            stream
100        } else {
101            panic!("Query answer is not a rows stream.")
102        }
103    }
104
105    /// Unwraps the <code>QueryAnswer</code> into a <code>ConceptDocumentStream</code>.
106    /// Panics if it is not a <code>ConceptDocumentStream</code>.
107    ///
108    /// # Examples
109    ///
110    /// ```rust
111    /// query_answer.into_documents()
112    /// ```
113    pub fn into_documents(self) -> BoxStream<'static, Result<ConceptDocument>> {
114        if let Self::ConceptDocumentStream(_, stream) = self {
115            stream
116        } else {
117            panic!("Query answer is not a documents stream.")
118        }
119    }
120}
121
122impl fmt::Debug for QueryAnswer {
123    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124        match self {
125            QueryAnswer::Ok(_) => write!(f, "QueryAnswer::Ok"),
126            QueryAnswer::ConceptRowStream(_, _) => write!(f, "QueryAnswer::ConceptRowStream(<stream>)"),
127            QueryAnswer::ConceptDocumentStream(_, _) => write!(f, "QueryAnswer::ConceptDocumentStream(<stream>)"),
128        }
129    }
130}
131
132/// This enum is used to specify the type of the query resulted in this answer.
133///
134/// # Examples
135///
136/// ```rust
137/// concept_row.get_query_type()
138/// ```
139#[repr(C)]
140#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
141pub enum QueryType {
142    ReadQuery,
143    WriteQuery,
144    SchemaQuery,
145}