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
22pub use self::{concept_document::ConceptDocument, concept_row::ConceptRow, json::JSON};
23use crate::{
24 BoxStream, Result,
25 answer::{concept_document::ConceptDocumentHeader, concept_row::ConceptRowHeader},
26};
27
28pub mod concept_document;
29pub mod concept_row;
30mod json;
31
32pub enum QueryAnswer {
33 Ok(QueryType),
34 ConceptRowStream(Arc<ConceptRowHeader>, BoxStream<'static, Result<ConceptRow>>),
35 ConceptDocumentStream(Arc<ConceptDocumentHeader>, BoxStream<'static, Result<ConceptDocument>>),
36}
37
38impl QueryAnswer {
39 /// Retrieves the executed query's type (shared by all elements in this stream).
40 ///
41 /// # Examples
42 ///
43 /// ```rust
44 /// query_answer.get_query_type()
45 /// ```
46 pub fn get_query_type(&self) -> QueryType {
47 match self {
48 &QueryAnswer::Ok(query_type) => query_type,
49 QueryAnswer::ConceptRowStream(header, _) => header.query_type,
50 QueryAnswer::ConceptDocumentStream(header, _) => header.query_type,
51 }
52 }
53
54 /// Checks if the <code>QueryAnswer</code> is an <code>Ok</code> response.
55 ///
56 /// # Examples
57 ///
58 /// ```rust
59 /// query_answer.is_ok()
60 /// ```
61 pub fn is_ok(&self) -> bool {
62 matches!(self, Self::Ok(_))
63 }
64
65 /// Checks if the <code>QueryAnswer</code> is a <code>ConceptRowStream</code>.
66 ///
67 /// # Examples
68 ///
69 /// ```rust
70 /// query_answer.is_row_stream()
71 /// ```
72 pub fn is_row_stream(&self) -> bool {
73 matches!(self, Self::ConceptRowStream(_, _))
74 }
75
76 /// Checks if the <code>QueryAnswer</code> is a <code>ConceptDocumentStream</code>.
77 ///
78 /// # Examples
79 ///
80 /// ```rust
81 /// query_answer.is_document_stream()
82 /// ```
83 pub fn is_document_stream(&self) -> bool {
84 matches!(self, Self::ConceptDocumentStream(_, _))
85 }
86
87 /// Unwraps the <code>QueryAnswer</code> into a <code>ConceptRowStream</code>.
88 /// Panics if it is not a <code>ConceptRowStream</code>.
89 ///
90 /// # Examples
91 ///
92 /// ```rust
93 /// query_answer.into_rows()
94 /// ```
95 pub fn into_rows(self) -> BoxStream<'static, Result<ConceptRow>> {
96 let Self::ConceptRowStream(_, stream) = self else { panic!("Query answer is not a rows stream.") };
97 stream
98 }
99
100 /// Unwraps the <code>QueryAnswer</code> into a <code>ConceptDocumentStream</code>.
101 /// Panics if it is not a <code>ConceptDocumentStream</code>.
102 ///
103 /// # Examples
104 ///
105 /// ```rust
106 /// query_answer.into_documents()
107 /// ```
108 pub fn into_documents(self) -> BoxStream<'static, Result<ConceptDocument>> {
109 if let Self::ConceptDocumentStream(_, stream) = self {
110 stream
111 } else {
112 panic!("Query answer is not a documents stream.")
113 }
114 }
115}
116
117impl fmt::Debug for QueryAnswer {
118 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119 match self {
120 QueryAnswer::Ok(_) => write!(f, "QueryAnswer::Ok"),
121 QueryAnswer::ConceptRowStream(_, _) => write!(f, "QueryAnswer::ConceptRowStream(<stream>)"),
122 QueryAnswer::ConceptDocumentStream(_, _) => write!(f, "QueryAnswer::ConceptDocumentStream(<stream>)"),
123 }
124 }
125}
126
127/// This enum is used to specify the type of the query resulted in this answer.
128///
129/// # Examples
130///
131/// ```rust
132/// concept_row.get_query_type()
133/// ```
134#[repr(C)]
135#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
136pub enum QueryType {
137 /// A read-only query (e.g. <code>match</code>).
138 ReadQuery,
139 /// A data-modifying query (e.g. <code>insert</code>, <code>delete</code>, <code>update</code>).
140 WriteQuery,
141 /// A schema-modifying query (e.g. <code>define</code>, <code>undefine</code>, <code>redefine</code>).
142 SchemaQuery,
143}