indradb-lib 5.0.0

A graph database library
Documentation
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
use std::str::FromStr;

use crate::{errors, Edge, Identifier, Json};

use uuid::Uuid;

macro_rules! into_query {
    ($name:ident, $variant:ident) => {
        // we don't want to impl From since the reverse operation isn't allowed
        #[allow(clippy::from_over_into)]
        impl Into<Query> for $name {
            fn into(self) -> Query {
                Query::$variant(self)
            }
        }
    };
}

macro_rules! nestable_query {
    ($name:ident, $variant:ident) => {
        impl QueryExt for $name {}
        impl CountQueryExt for $name {}
        into_query!($name, $variant);
    };
}

/// Specifies what kind of items should be piped from one type of query to
/// another.
///
/// Edge and vertex queries can build off of one another via pipes - e.g. you
/// can get the outbound edges of a set of vertices by piping from a vertex
/// query to an edge query. `EdgeDirection`s are used to specify which
/// end of things you want to pipe - either the outbound items or the inbound
/// items.
#[derive(Eq, PartialEq, Clone, Debug, Hash, Copy)]
pub enum EdgeDirection {
    /// Outbound direction.
    Outbound,
    /// Inbound direction.
    Inbound,
}

impl FromStr for EdgeDirection {
    type Err = errors::ValidationError;

    fn from_str(s: &str) -> Result<EdgeDirection, Self::Err> {
        match s {
            "outbound" => Ok(EdgeDirection::Outbound),
            "inbound" => Ok(EdgeDirection::Inbound),
            _ => Err(errors::ValidationError::InvalidValue),
        }
    }
}

impl From<EdgeDirection> for String {
    fn from(d: EdgeDirection) -> Self {
        match d {
            EdgeDirection::Outbound => "outbound".to_string(),
            EdgeDirection::Inbound => "inbound".to_string(),
        }
    }
}

/// A query to get a set of values from the database.
#[derive(Eq, PartialEq, Clone, Debug)]
pub enum Query {
    /// Gets all vertices.
    AllVertex,
    /// Gets a range of vertices.
    RangeVertex(RangeVertexQuery),
    /// Gets a specific set of vertices.
    SpecificVertex(SpecificVertexQuery),
    /// Gets vertices with or without a given property.
    VertexWithPropertyPresence(VertexWithPropertyPresenceQuery),
    /// Gets vertices with a property equal to a given value.
    VertexWithPropertyValue(VertexWithPropertyValueQuery),

    /// Gets all edges.
    AllEdge,
    /// Gets a specific set of edges.
    SpecificEdge(SpecificEdgeQuery),
    /// Gets edges with or without a given property.
    EdgeWithPropertyPresence(EdgeWithPropertyPresenceQuery),
    /// Gets edges with a property equal to a given value.
    EdgeWithPropertyValue(EdgeWithPropertyValueQuery),

    /// Gets the vertices associated with edges, or edges associated with
    /// vertices.
    Pipe(PipeQuery),
    /// Returns the properties associated with a vertex or edge.
    PipeProperty(PipePropertyQuery),
    /// Gets vertices or edges with or without a property.
    PipeWithPropertyPresence(PipeWithPropertyPresenceQuery),
    /// Gets vertices or edges with a property equal to a given value.
    PipeWithPropertyValue(PipeWithPropertyValueQuery),

    /// Includes the results of a query in output.
    Include(IncludeQuery),
    /// Counts the number of items returned from a query.
    Count(CountQuery),
}

impl Query {
    /// Determines the number of output values the query will produce without
    /// running it, so we can allocate a `Vec` with the correct capacity
    /// ahead-of-time.
    pub(crate) fn output_len(&self) -> usize {
        match self {
            Query::AllVertex
            | Query::RangeVertex(_)
            | Query::SpecificVertex(_)
            | Query::VertexWithPropertyPresence(_)
            | Query::VertexWithPropertyValue(_)
            | Query::AllEdge
            | Query::SpecificEdge(_)
            | Query::EdgeWithPropertyPresence(_)
            | Query::EdgeWithPropertyValue(_)
            | Query::Count(_) => 1,
            Query::Pipe(q) => q.inner.output_len(),
            Query::PipeProperty(q) => q.inner.output_len(),
            Query::PipeWithPropertyPresence(q) => q.inner.output_len(),
            Query::PipeWithPropertyValue(q) => q.inner.output_len(),
            Query::Include(q) => 1 + q.inner.output_len(),
        }
    }

    /// Determines the `QueryOutputValue` variant that will be produced
    /// without running the query, which can help validate the query
    /// ahead-of-time.
    pub(crate) fn output_type(&self) -> errors::ValidationResult<QueryOutputValue> {
        match self {
            Query::AllVertex
            | Query::RangeVertex(_)
            | Query::SpecificVertex(_)
            | Query::VertexWithPropertyPresence(_)
            | Query::VertexWithPropertyValue(_) => Ok(QueryOutputValue::Vertices(Vec::default())),
            Query::AllEdge
            | Query::SpecificEdge(_)
            | Query::EdgeWithPropertyPresence(_)
            | Query::EdgeWithPropertyValue(_) => Ok(QueryOutputValue::Edges(Vec::default())),
            Query::Count(_) => Ok(QueryOutputValue::Count(0)),
            Query::Pipe(q) => q.inner.output_type(),
            Query::PipeProperty(q) => match q.inner.output_type()? {
                QueryOutputValue::Vertices(_) => Ok(QueryOutputValue::VertexProperties(Vec::default())),
                QueryOutputValue::Edges(_) => Ok(QueryOutputValue::EdgeProperties(Vec::default())),
                _ => Err(errors::ValidationError::InnerQuery),
            },
            Query::PipeWithPropertyPresence(q) => q.inner.output_type(),
            Query::PipeWithPropertyValue(q) => q.inner.output_type(),
            Query::Include(q) => q.inner.output_type(),
        }
    }
}

/// Extension trait containing common functions for all query structs.
pub trait QueryExt: Into<Query> {
    /// Gets the outbound vertices or edges associated with this query.
    fn outbound(self) -> errors::ValidationResult<PipeQuery> {
        PipeQuery::new(Box::new(self.into()), EdgeDirection::Outbound)
    }

    /// Gets the inbound vertices or edges associated with this query.
    fn inbound(self) -> errors::ValidationResult<PipeQuery> {
        PipeQuery::new(Box::new(self.into()), EdgeDirection::Inbound)
    }

    /// Gets values with a property.
    ///
    /// # Arguments
    /// * `name`: The name of the property.
    fn with_property<T: Into<Identifier>>(self, name: T) -> errors::ValidationResult<PipeWithPropertyPresenceQuery> {
        PipeWithPropertyPresenceQuery::new(Box::new(self.into()), name, true)
    }

    /// Gets values without a property.
    ///
    /// # Arguments
    /// * `name`: The name of the property.
    fn without_property<T: Into<Identifier>>(self, name: T) -> errors::ValidationResult<PipeWithPropertyPresenceQuery> {
        PipeWithPropertyPresenceQuery::new(Box::new(self.into()), name, false)
    }

    /// Gets values with a property equal to a given value.
    ///
    /// # Arguments
    /// * `name`: The name of the property.
    /// * `value`: The value of the property.
    fn with_property_equal_to<T: Into<Identifier>>(
        self,
        name: T,
        value: Json,
    ) -> errors::ValidationResult<PipeWithPropertyValueQuery> {
        PipeWithPropertyValueQuery::new(Box::new(self.into()), name, value, true)
    }

    /// Gets values with a property not equal to a given value.
    ///
    /// # Arguments
    /// * `name`: The name of the property.
    /// * `value`: The value of the property.
    fn with_property_not_equal_to<T: Into<Identifier>>(
        self,
        name: T,
        value: Json,
    ) -> errors::ValidationResult<PipeWithPropertyValueQuery> {
        PipeWithPropertyValueQuery::new(Box::new(self.into()), name, value, false)
    }

    /// Gets the properties associated with the query results.
    fn properties(self) -> errors::ValidationResult<PipePropertyQuery> {
        PipePropertyQuery::new(Box::new(self.into()))
    }

    /// Include this query's output, even if it is an intermediate result.
    fn include(self) -> IncludeQuery {
        IncludeQuery::new(Box::new(self.into()))
    }
}

pub trait CountQueryExt: Into<Query> {
    /// Gets the count from this query.
    fn count(self) -> errors::ValidationResult<CountQuery> {
        CountQuery::new(Box::new(self.into()))
    }
}

/// Gets all vertices.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct AllVertexQuery;

impl QueryExt for AllVertexQuery {}
impl CountQueryExt for AllVertexQuery {}

// we don't want to impl From since the reverse operation isn't allowed
#[allow(clippy::from_over_into)]
impl Into<Query> for AllVertexQuery {
    fn into(self) -> Query {
        Query::AllVertex
    }
}

/// Gets a range of vertices.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct RangeVertexQuery {
    /// Limits the number of vertices to get.
    pub limit: u32,

    /// Filters the type of vertices returned.
    pub t: Option<Identifier>,

    /// Sets the lowest vertex ID to return.
    pub start_id: Option<Uuid>,
}

nestable_query!(RangeVertexQuery, RangeVertex);

impl Default for RangeVertexQuery {
    fn default() -> Self {
        Self::new()
    }
}

impl RangeVertexQuery {
    /// Creates a new vertex range query.
    pub fn new() -> Self {
        Self {
            limit: u32::MAX,
            t: None,
            start_id: None,
        }
    }

    /// Sets the limit.
    ///
    /// # Arguments
    /// * `limit`: Limits the number of returned results.
    pub fn limit(self, limit: u32) -> Self {
        Self {
            limit,
            t: self.t,
            start_id: self.start_id,
        }
    }

    /// Filter the type of vertices returned.
    ///
    /// # Arguments
    /// * `t`: Sets the type filter.
    pub fn t(self, t: Identifier) -> Self {
        Self {
            limit: self.limit,
            t: Some(t),
            start_id: self.start_id,
        }
    }

    /// Sets the lowest vertex ID to return.
    ///
    /// # Arguments
    /// * `start_id`: The lowest vertex ID to return.
    pub fn start_id(self, start_id: Uuid) -> Self {
        Self {
            limit: self.limit,
            t: self.t,
            start_id: Some(start_id),
        }
    }
}

/// Gets a specific set of vertices.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct SpecificVertexQuery {
    /// The IDs of the vertices to get.
    pub ids: Vec<Uuid>,
}

nestable_query!(SpecificVertexQuery, SpecificVertex);

impl SpecificVertexQuery {
    /// Creates a new vertex query for getting a list of vertices by their
    /// IDs.
    ///
    /// Arguments
    /// * `ids`: The IDs of the vertices to get.
    pub fn new(ids: Vec<Uuid>) -> Self {
        Self { ids }
    }

    /// Creates a new vertex query for getting a single vertex.
    ///
    /// Arguments
    /// * `id`: The ID of the vertex to get.
    pub fn single(id: Uuid) -> Self {
        Self { ids: vec![id] }
    }
}

/// Gets vertices with or without a given property.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct VertexWithPropertyPresenceQuery {
    /// The name of the property.
    pub name: Identifier,
}

nestable_query!(VertexWithPropertyPresenceQuery, VertexWithPropertyPresence);

impl VertexWithPropertyPresenceQuery {
    /// Creates a new vertex with property presence query.
    ///
    /// # Arguments
    /// * `name`: The property name.
    pub fn new<T: Into<Identifier>>(name: T) -> Self {
        Self { name: name.into() }
    }
}

/// Gets vertices with a property equal to a given value.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct VertexWithPropertyValueQuery {
    /// The name of the property.
    pub name: Identifier,
    /// The value of the property.
    pub value: Json,
}

nestable_query!(VertexWithPropertyValueQuery, VertexWithPropertyValue);

impl VertexWithPropertyValueQuery {
    /// Creates a new vertex with property value query.
    ///
    /// # Arguments
    /// * `name`: The property name.
    /// * `value`: The property value.
    pub fn new<T: Into<Identifier>>(name: T, value: Json) -> Self {
        Self {
            name: name.into(),
            value,
        }
    }
}

/// Gets all edges.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct AllEdgeQuery;

impl QueryExt for AllEdgeQuery {}
impl CountQueryExt for AllEdgeQuery {}

// we don't want to impl From since the reverse operation isn't allowed
#[allow(clippy::from_over_into)]
impl Into<Query> for AllEdgeQuery {
    fn into(self) -> Query {
        Query::AllEdge
    }
}

/// Gets a specific set of edges.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct SpecificEdgeQuery {
    /// The edges to get.
    pub edges: Vec<Edge>,
}

nestable_query!(SpecificEdgeQuery, SpecificEdge);

impl SpecificEdgeQuery {
    /// Creates a new edge query for getting a list of specific edges.
    ///
    /// Arguments
    /// * `edges`: The edges to get.
    pub fn new(edges: Vec<Edge>) -> Self {
        Self { edges }
    }

    /// Creates a new edge query for getting a single edge.
    ///
    /// Arguments
    /// * `edge`: The edge to get.
    pub fn single(edge: Edge) -> Self {
        Self { edges: vec![edge] }
    }
}

/// Gets edges with or without a given property.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct EdgeWithPropertyPresenceQuery {
    /// The name of the property.
    pub name: Identifier,
}

nestable_query!(EdgeWithPropertyPresenceQuery, EdgeWithPropertyPresence);

impl EdgeWithPropertyPresenceQuery {
    /// Creates a new edge with property presence query.
    ///
    /// # Arguments
    /// * `name`: The property name.
    pub fn new<T: Into<Identifier>>(name: T) -> Self {
        Self { name: name.into() }
    }
}

/// Gets edges with a property equal to a given value.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct EdgeWithPropertyValueQuery {
    /// The name of the property.
    pub name: Identifier,
    /// The value of the property.
    pub value: Json,
}

nestable_query!(EdgeWithPropertyValueQuery, EdgeWithPropertyValue);

impl EdgeWithPropertyValueQuery {
    /// Creates a new edge with property value query.
    ///
    /// # Arguments
    /// * `name`: The property name.
    /// * `value`: The property value.
    pub fn new<T: Into<Identifier>>(name: T, value: Json) -> Self {
        Self {
            name: name.into(),
            value,
        }
    }
}

/// Gets the vertices associated with edges, or edges associated with
/// vertices.
///
/// Generally, you shouldn't need to construct this directly, but rather call
/// `.outbound()` or `.inbound()`.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct PipeQuery {
    /// The edge query to build off of.
    pub inner: Box<Query>,

    /// Whether to get outbound or inbound values.
    pub direction: EdgeDirection,

    /// Limits the number of values to get.
    pub limit: u32,

    /// Filters the type of values returned.
    pub t: Option<Identifier>,
}

nestable_query!(PipeQuery, Pipe);

impl PipeQuery {
    /// Constructs a new pipe query.
    ///
    /// # Arguments
    /// * `inner`: The inner query.
    /// * `direction`: Which direction to pipe from.
    pub fn new(inner: Box<Query>, direction: EdgeDirection) -> errors::ValidationResult<Self> {
        match inner.output_type()? {
            QueryOutputValue::Vertices(_) | QueryOutputValue::Edges(_) => {}
            _ => return Err(errors::ValidationError::InnerQuery),
        }

        Ok(Self {
            inner,
            direction,
            limit: u32::MAX,
            t: None,
        })
    }

    /// Sets the limit.
    ///
    /// # Arguments
    /// * `limit`: Limits the number of returned results.
    pub fn limit(self, limit: u32) -> Self {
        Self {
            inner: self.inner,
            direction: self.direction,
            limit,
            t: self.t,
        }
    }

    /// Filter the type of values returned.
    ///
    /// # Arguments
    /// * `t`: Sets the type filter.
    pub fn t(self, t: Identifier) -> Self {
        Self {
            inner: self.inner,
            direction: self.direction,
            limit: self.limit,
            t: Some(t),
        }
    }
}

/// Returns the properties associated with a vertex or edge.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct PipePropertyQuery {
    /// The inner query.
    pub inner: Box<Query>,
    /// The property name to get. If `None`, all properties will be fetched.
    pub name: Option<Identifier>,
}

into_query!(PipePropertyQuery, PipeProperty);
impl CountQueryExt for PipePropertyQuery {}

impl PipePropertyQuery {
    /// Creates a new pipe property query.
    ///
    /// # Arguments
    /// * `inner`: The query to pipe.
    pub fn new(inner: Box<Query>) -> errors::ValidationResult<Self> {
        match inner.output_type()? {
            QueryOutputValue::Vertices(_) | QueryOutputValue::Edges(_) => {}
            _ => return Err(errors::ValidationError::InnerQuery),
        }
        Ok(Self { inner, name: None })
    }

    /// Only include properties with a given name.
    ///
    /// # Arguments
    /// * `name`: The name filter.
    pub fn name(self, name: Identifier) -> Self {
        Self {
            inner: self.inner,
            name: Some(name),
        }
    }
}

/// Gets vertices or edges with or without a property.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct PipeWithPropertyPresenceQuery {
    /// The query to filter.
    pub inner: Box<Query>,
    /// The name of the property.
    pub name: Identifier,
    /// Whether we should look for property presence or lack thereof.
    pub exists: bool,
}

nestable_query!(PipeWithPropertyPresenceQuery, PipeWithPropertyPresence);

impl PipeWithPropertyPresenceQuery {
    /// Gets vertices with a property.
    ///
    /// Arguments
    /// * `inner`: The query to filter.
    /// * `name`: The name of the property.
    /// * `exists`: Whether we should look for property presence or lack thereof.
    pub fn new<T: Into<Identifier>>(inner: Box<Query>, name: T, exists: bool) -> errors::ValidationResult<Self> {
        match inner.output_type()? {
            QueryOutputValue::Vertices(_) | QueryOutputValue::Edges(_) => {}
            _ => return Err(errors::ValidationError::InnerQuery),
        }
        Ok(Self {
            inner,
            name: name.into(),
            exists,
        })
    }
}

/// Gets vertices or edges with a property equal to a given value.
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct PipeWithPropertyValueQuery {
    /// The query to filter.
    pub inner: Box<Query>,
    /// The name of the property.
    pub name: Identifier,
    /// The value of the property.
    pub value: Json,
    /// Whether we should look for property equality or non-equality.
    pub equal: bool,
}

nestable_query!(PipeWithPropertyValueQuery, PipeWithPropertyValue);

impl PipeWithPropertyValueQuery {
    /// Constructs a new pipe with property value query.
    ///
    /// # Arguments
    /// * `inner`: The inner query.
    /// * `name`: The property name to filter.
    /// * `value`: The property value to filter.
    /// * `equal`: Whether the value should be equal, or not equal.
    pub fn new<T: Into<Identifier>>(
        inner: Box<Query>,
        name: T,
        value: Json,
        equal: bool,
    ) -> errors::ValidationResult<Self> {
        match inner.output_type()? {
            QueryOutputValue::Vertices(_) | QueryOutputValue::Edges(_) => {}
            _ => return Err(errors::ValidationError::InnerQuery),
        }
        Ok(Self {
            inner,
            name: name.into(),
            value,
            equal,
        })
    }
}

/// Includes the results of a query in output.
///
/// The outermost part of a query will always be explicitly included. This
/// allows you to also output an intermediate result.
///
/// # Examples
/// ```
/// use indradb::{AllVertexQuery, QueryExt};
/// // A query to return all edges in the database, which are implicitly
/// // included as the outermost results.
/// let q = AllVertexQuery.outbound();
/// // A query to return all vertices and all edges in the database, because
/// // vertices are explicitly included as intermediate results.
/// let q = AllVertexQuery.include().outbound();
/// ```
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct IncludeQuery {
    /// The query to export.
    pub inner: Box<Query>,
}

nestable_query!(IncludeQuery, Include);

impl IncludeQuery {
    /// Marks a query as exported.
    ///
    /// Arguments
    /// * `inner`: The query to export.
    pub fn new(inner: Box<Query>) -> Self {
        Self { inner }
    }
}

/// Counts the number of items returned from a query.
///
/// # Examples
/// ```
/// use indradb::{AllVertexQuery, CountQueryExt};
/// // A query to return the total number of vertices in the database.
/// let q = AllVertexQuery.count();
/// ```
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct CountQuery {
    /// The query to export.
    pub inner: Box<Query>,
}

into_query!(CountQuery, Count);

impl CountQuery {
    /// Marks a query as exported.
    ///
    /// Arguments
    /// * `inner`: The query to export.
    pub fn new(inner: Box<Query>) -> errors::ValidationResult<Self> {
        match inner.output_type()? {
            QueryOutputValue::Vertices(_)
            | QueryOutputValue::Edges(_)
            | QueryOutputValue::VertexProperties(_)
            | QueryOutputValue::EdgeProperties(_) => {}
            _ => return Err(errors::ValidationError::InnerQuery),
        }
        Ok(Self { inner })
    }
}

/// Value(s) returned from a query.
#[derive(Clone, Debug, PartialEq)]
pub enum QueryOutputValue {
    /// Vertices.
    Vertices(Vec<crate::Vertex>),
    /// Edges.
    Edges(Vec<crate::Edge>),
    /// A Count.
    Count(u64),
    /// Vertex properties.
    VertexProperties(Vec<crate::VertexProperties>),
    /// Edge properties.
    EdgeProperties(Vec<crate::EdgeProperties>),
}

#[cfg(test)]
mod tests {
    use crate::{
        ijson, AllVertexQuery, CountQuery, CountQueryExt, EdgeDirection, Identifier, PipePropertyQuery, PipeQuery,
        PipeWithPropertyPresenceQuery, PipeWithPropertyValueQuery, Query, ValidationError,
    };
    use std::str::FromStr;

    fn expect_inner_query_err<T: core::fmt::Debug>(result: Result<T, ValidationError>) {
        match result {
            Err(ValidationError::InnerQuery) => (),
            _ => assert!(false, "unexpected result: {:?}", result),
        }
    }

    #[test]
    fn should_convert_str_to_edge_direction() {
        assert_eq!(EdgeDirection::from_str("outbound").unwrap(), EdgeDirection::Outbound);
        assert_eq!(EdgeDirection::from_str("inbound").unwrap(), EdgeDirection::Inbound);
        assert!(EdgeDirection::from_str("foo").is_err());
    }

    #[test]
    fn should_convert_edge_direction_to_string() {
        let s: String = EdgeDirection::Outbound.into();
        assert_eq!(s, "outbound".to_string());
        let s: String = EdgeDirection::Inbound.into();
        assert_eq!(s, "inbound".to_string());
    }

    #[test]
    fn should_fail_for_nested_count_queries() {
        let q: Query = AllVertexQuery.count().unwrap().into();
        expect_inner_query_err(CountQuery::new(Box::new(q.clone())));
        expect_inner_query_err(PipeQuery::new(Box::new(q.clone()), EdgeDirection::Outbound));
        expect_inner_query_err(PipePropertyQuery::new(Box::new(q.clone())));
        expect_inner_query_err(PipeWithPropertyPresenceQuery::new(
            Box::new(q.clone()),
            Identifier::new("foo").unwrap(),
            true,
        ));
        expect_inner_query_err(PipeWithPropertyValueQuery::new(
            Box::new(q.clone()),
            Identifier::new("foo").unwrap(),
            ijson!("bar"),
            true,
        ));
    }
}